-
Notifications
You must be signed in to change notification settings - Fork 1
/
rotate_demo.py
89 lines (51 loc) · 1.7 KB
/
rotate_demo.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
from detviz import *
import math
print (''' The determinant is the area of a unit square that you see in the plot ''')
def draw_and_show(m, plt_name = "GRAPH"):
plot_setup(plt_name)
draw_matrix(m, "maroon", 0.5, "-", llgram = True)
draw_axes_for_matrix(m, "red", 0.1, "-")
plt.show()
def rotate_vector(v, rad):
assert (len(v) == 2)
return [v[0] * math.cos(rad) + v[1] * (-math.sin(rad)),
v[0] * math.sin(rad) + v[1] * math.cos(rad)]
def rotate(m, deg):
# sanity check
assert (len(m) == 2 or len(m) == 1)
assert (len(m[0]) == 2)
if len(m) == 2:
assert (len(m[1]) == 2)
ret = []
for i in range(0, len(m)):
ret.append(rotate_vector(m[i], math.radians(deg)))
return ret
def stick_to_x(m, vidx = 0):
# sanity check
assert (len(m) == 2 or len(m) == 1)
assert (len(m[0]) == 2)
if len(m) == 2:
assert (len(m[1]) == 2)
assert (vidx < len(m))
# calculate the degrees of the first vector off the x axis
deg = math.degrees(math.atan2(m[vidx][1], m[vidx][0]))
return rotate(m, -deg)
## main ########################################################################
# Write matrix as column vectors
I = [[1, 0], [0, 1]]
M = [[2, 3], [3, 1]]
plot_setup("ROTATE")
draw_matrix(I, "maroon", 0.5, "-", llgram = True)
Irot = rotate(I, 90.0)
draw_matrix(Irot, "red", 0.5, "-", llgram = True)
####
draw_matrix(M, "green", 0.5, "-", llgram = True)
Mrot = rotate(M, 90.0)
draw_matrix(Mrot, "purple", 0.5, "-", llgram = True)
plt.show()
###
plot_setup("STICK TO FLOOR")
draw_matrix(M, "green", 0.5, "-", llgram = True)
Mstickx = stick_to_x(M, 1)
draw_matrix(Mstickx, "purple", 0.5, "-", llgram = True)
plt.show()