-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.py
93 lines (77 loc) · 2.63 KB
/
Matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from math import sin, cos, radians
'''
#adjacent over hypotenuse is cos(theta)
#opposite over hypotenuse is sin(theta)
#xp = xcos(theta) - ysin(theta)
#yp = xsin(theta) + ycos(theta)
#x=1
#y=0
#xp = _x*cosang - _y*sinang
#yp = _x*sinang + _y*cosang
'''
class Matrix():
def __init__(self):
self.dummy=0
def identity(self):
return [ [1, 0, 0],
[0, 1, 0],
[0, 0, 1] ]
def multiply(self, p, m): #
return [
[ (p[0][0] * m[0][0])
+ (p[0][1] * m[1][0])
+ (p[0][2] * m[2][0]),
(p[0][0] * m[0][1])
+ (p[0][1] * m[1][1])
+ (p[0][2] * m[2][1]),
(p[0][0] * m[0][2])
+ (p[0][1] * m[1][2])
+ (p[0][2] * m[2][2])],
# ---
[ (p[1][0] * m[0][0])
+ (p[1][1] * m[1][0])
+ (p[1][2] * m[2][0]),
(p[1][0] * m[0][1])
+ (p[1][1] * m[1][1])
+ (p[1][2] * m[2][1]),
(p[1][0] * m[0][2])
+ (p[1][1] * m[1][2])
+ (p[1][2] * m[2][2])],
# ---
[ (p[2][0] * m[0][0])
+ (p[2][1] * m[1][0])
+ (p[2][2] * m[2][0]),
(p[2][0] * m[0][1])
+ (p[2][1] * m[1][1])
+ (p[2][2] * m[2][1]),
(p[2][0] * m[0][2])
+ (p[2][1] * m[1][2])
+ (p[2][2] * m[2][2])]]
def translate(self,_x,_y):
return [ [1 ,0 ,0],
[0 ,1 ,0],
[_x,_y,1] ]
def rotate(self,rad):
return [ [float(cos(rad)) , float(sin(rad)), float(0)],
[ float(-sin(rad)), float(cos(rad)), float(0)],
[ float(0) , float(0 ) , float(1)] ]
def toVectorAddOrigin(self, vec, p, _ox, _oy):
return [int(round( vec.x *p[0][0]
+vec.y *p[1][0]
+vec.w *p[2][0]))+_ox,
int(round( vec.x *p[0][1]
+vec.y *p[1][1]
+vec.w *p[2][1]))+_oy,
int(round( vec.x *p[0][2]
+vec.y *p[1][2]
+vec.w *p[2][2]))]
def toVector(self, vec, p):
return [int(round( vec.x *p[0][0]
+vec.y *p[1][0]
+vec.w *p[2][0])),
int(round( vec.x *p[0][1]
+vec.y *p[1][1]
+vec.w *p[2][1])),
int(round( vec.x *p[0][2]
+vec.y *p[1][2]
+vec.w *p[2][2]))]