-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
114 lines (98 loc) · 2.38 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import math
def make_bezier():
return [
[-1, 3, -3, 1],
[3, -6, 3, 0],
[-3, 3, 0, 0],
[1, 0, 0, 0]
]
def make_hermite():
return [
[2, -3, 0, 1],
[-2, 3, 0, 0],
[1, -2, 1, 0],
[1, -1, 0, 0]
]
def generate_curve_coefs( p0, p1, p2, p3, t ):
coefs = [[p0, p1, p2, p3]]
if t == 'hermite':
curve = make_hermite()
else:
curve = make_bezier()
matrix_mult(curve, coefs)
return coefs
def make_translate( x, y, z ):
t = new_matrix()
ident(t)
t[3][0] = x
t[3][1] = y
t[3][2] = z
return t
def make_scale( x, y, z ):
t = new_matrix()
ident(t)
t[0][0] = x
t[1][1] = y
t[2][2] = z
return t
def make_rotX( theta ):
t = new_matrix()
ident(t)
t[1][1] = math.cos(theta)
t[2][1] = -1 * math.sin(theta)
t[1][2] = math.sin(theta)
t[2][2] = math.cos(theta)
return t
def make_rotY( theta ):
t = new_matrix()
ident(t)
t[0][0] = math.cos(theta)
t[0][2] = -1 * math.sin(theta)
t[2][0] = math.sin(theta)
t[2][2] = math.cos(theta)
return t
def make_rotZ( theta ):
t = new_matrix()
ident(t)
t[0][0] = math.cos(theta)
t[1][0] = -1 * math.sin(theta)
t[0][1] = math.sin(theta)
t[1][1] = math.cos(theta)
return t
def print_matrix( matrix ):
s = ''
for r in range( len( matrix[0] ) ):
for c in range( len(matrix) ):
s+= str(matrix[c][r]) + ' '
s+= '\n'
print s
def ident( matrix ):
for r in range( len( matrix[0] ) ):
for c in range( len(matrix) ):
if r == c:
matrix[c][r] = 1
else:
matrix[c][r] = 0
def scalar_mult( matrix, s ):
for r in range( len( matrix[0] ) ):
for c in range( len(matrix) ):
matrix[c][r]*= s
#m1 * m2 -> m2
def matrix_mult( m1, m2 ):
point = 0
for row in m2:
#get a copy of the next point
tmp = row[:]
for r in range(4):
m2[point][r] = (m1[0][r] * tmp[0] +
m1[1][r] * tmp[1] +
m1[2][r] * tmp[2] +
m1[3][r] * tmp[3])
point+= 1
def new_matrix(rows = 4, cols = 4):
m = []
for c in range( cols ):
m.append( [] )
for r in range( rows ):
m[c].append( 0 )
return m