comparison pytouhou/utils/matrix.py @ 4:787d2eb13c2d

Add buggy stageviewer!
author Thibaut Girka <thib@sitedethib.com>
date Mon, 01 Aug 2011 17:17:22 +0200
parents
children f405b947624d
comparison
equal deleted inserted replaced
3:ffe0283c27de 4:787d2eb13c2d
1 #TODO: find/learn to use a proper lib
2
3 from math import sin, cos
4
5 class Matrix(object):
6 def __init__(self):
7 self.data = [[0] * 4 for i in xrange(4)]
8
9
10 def mult(self, other_matrix):
11 result = Matrix()
12 for i in xrange(4):
13 for j in xrange(4):
14 result.data[i][j] = sum(self.data[i][a] * other_matrix.data[a][j] for a in xrange(4))
15 return result
16
17
18 @classmethod
19 def get_translation_matrix(cls, x, y, z):
20 matrix = cls()
21 matrix.data[0][0] = 1
22 matrix.data[1][1] = 1
23 matrix.data[2][2] = 1
24 matrix.data[3][3] = 1
25 matrix.data[0][3] = x
26 matrix.data[1][3] = y
27 matrix.data[2][3] = z
28 return matrix
29
30
31 @classmethod
32 def get_scaling_matrix(cls, x, y, z):
33 matrix = cls()
34 matrix.data[0][0] = x
35 matrix.data[1][1] = y
36 matrix.data[2][2] = z
37 matrix.data[3][3] = 1
38 return matrix
39
40
41 @classmethod
42 def get_rotation_matrix(cls, angle, axis):
43 """Only handles axis = x, y or z."""
44 matrix = cls()
45 matrix.data[3][3] = 1
46 if axis == 'x':
47 matrix.data[0][0] = 1
48 matrix.data[1][1] = cos(angle)
49 matrix.data[1][2] = -sin(angle)
50 matrix.data[2][1] = sin(angle)
51 matrix.data[2][2] = cos(angle)
52 elif axis == 'y':
53 matrix.data[0][0] = cos(angle)
54 matrix.data[0][2] = sin(angle)
55 matrix.data[2][0] = -sin(angle)
56 matrix.data[2][2] = cos(angle)
57 matrix.data[1][1] = 1
58 elif axis == 'z':
59 matrix.data[0][0] = cos(angle)
60 matrix.data[0][1] = -sin(angle)
61 matrix.data[1][0] = sin(angle)
62 matrix.data[1][1] = cos(angle)
63 matrix.data[2][2] = 1
64 else:
65 raise Exception
66 return matrix