-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry_lab.py
175 lines (149 loc) · 5.53 KB
/
geometry_lab.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# version code 77ed2409f40d+
coursera = 1
# Please fill out this stencil and submit using the provided submission script.
# version code 05f5a0d767f0+
# Please fill out this stencil and submit using the provided submission script.
from mat import Mat
from vec import Vec
import math
from image_mat_util import file2mat
from image_mat_util import mat2display
import math
#(loc_mat, color_mat) = file2mat('pic.png')
#mat2display(loc_mat, color_mat)
## Task 1
def identity(labels = {'x','y','u'}):
'''
In case you have never seen this notation for a parameter before,
it defines the default value of labels to be {'x','y','u'}.
You should write your procedure as if
it were defined 'def identity(labels):'. However, if you want the labels of
your identity matrix to be {'x','y','u'}, you can just call
identity(). If you want {'r','g','b'}, or another set, to be the
labels of your matrix, you can call identity({'r','g','b'}).
>>> identity()==Mat(({'x','y','u'},{'x','y','u'}), {('x','x'):1, ('y','y'):1, ('u','u'):1})
True
>>> identity({'r','g','b'})==Mat(({'r','g','b'},{'r','g','b'}), {('r','r'):1, ('g','g'):1, ('b','b'):1})
True
'''
return Mat((labels, labels), {(a,b):1 for a in labels for b in labels if a==b})
## Task 2
def translation(x,y):
'''
Input: An x and y value by which to translate an image.
Output: Corresponding 3x3 translation matrix.
>>> translation(9,10)==Mat(({'x','y','u'},{'x','y','u'}), {('x','x'):1, ('y','y'):1, ('u','u'):1, ('y','u'):10, ('x','u'):9})
True
'''
ret = identity()
ret['x', 'u'] = x
ret['y', 'u'] = y
return ret
translation(9,10)==Mat(({'x','y','u'},{'x','y','u'}), {('x','x'):1, ('y','y'):1, ('u','u'):1, ('y','u'):10, ('x','u'):9})
## Task 3
def scale(a, b):
'''
Input: Scaling parameters for the x and y direction.
Output: Corresponding 3x3 scaling matrix.
>>> scale(3,4)*Vec({'x','y','u'}, {'x':1,'y':1,'u':1}) == Vec({'x','y','u'}, {'x':3, 'y':4, 'u':1})
True
>>> scale(0,0)*Vec({'x','y','u'}, {'x':1,'y':1,'u':1}) == Vec({'x','y','u'}, {'u':1})
True
'''
ret = identity()
ret['x', 'x'] = a
ret['y', 'y'] = b
return ret
## Task 4
def rotation(angle):
'''
Input: An angle in radians to rotate an image.
Output: Corresponding 3x3 rotation matrix.
Note that the math module is imported.
>>> def normsq(v): return v*v
>>> normsq(rotation(math.pi) * Vec({'u', 'x', 'y'},{'x':1,'y':2,'u':1}) - Vec({'u', 'x', 'y'},{'u': 1, 'x': -1, 'y': -2})) < 1e-15
True
>>> normsq(rotation(math.pi/2) * Vec({'u', 'x', 'y'},{'x':3,'y':1,'u':1}) - Vec({'u', 'x', 'y'},{'u': 1, 'x': -1, 'y': 3.0})) < 1e-15
True
'''
ret = identity()
ret['x','x'] = math.cos(angle)
ret['x','y'] = -1*math.sin(angle)
ret['y','x'] = math.sin(angle)
ret['y','y'] = math.cos(angle)
return ret
print(rotation(math.pi/2) * Vec({'u', 'x', 'y'},{'x':3,'y':1,'u':1}))
## Task 5
def rotate_about(x,y,angle):
'''
Input: An x and y coordinate to rotate about, and an angle
in radians to rotate about.
Output: Corresponding 3x3 rotation matrix.
It might be helpful to use procedures you already wrote.
'''
t1 = translation(x, y)
t2 = translation(-x,-y);
r = rotation(angle)
return t1*r*t2
## Task 6
def reflect_y():
'''
Input: None.
Output: 3x3 Y-reflection matrix.
>>> v = Vec({'x','y','u'}, {'x':1, 'y':1, 'u':1})
>>> reflect_y()*v == Vec({'x','y','u'}, {'x':-1, 'y':1, 'u':1})
True
>>> w = Vec({'x','y','u'}, {'u':1})
>>> reflect_y()*w == Vec({'x','y','u'},{'u':1})
True
'''
ret = identity()
ret['x','x'] = -1
return ret
## Task 7
def reflect_x():
'''
Inpute: None.
Output: 3x3 X-reflection matrix.
>>> v = Vec({'x','y','u'}, {'x':1, 'y':1, 'u':1})
>>> reflect_x()*v == Vec({'x','y','u'}, {'x':1, 'y':-1, 'u':1})
True
>>> w = Vec({'x','y','u'}, {'u':1})
>>> reflect_x()*w == Vec({'x','y','u'},{'u':1})
True
'''
ret = identity()
ret['y','y'] = -1
return ret
## Task 8
def scale_color(scale_r,scale_g,scale_b):
'''
Input: 3 scaling parameters for the colors of the image.
Output: Corresponding 3x3 color scaling matrix.
>>> scale_color(1,2,3)*Vec({'r','g','b'},{'r':1,'g':2,'b':3}) == Vec({'r','g','b'},{'r':1,'g':4,'b':9})
True
'''
return Mat(({'r','g','b'}, {'r', 'g', 'b'}), {('r','r'):scale_r, ('g','g'):scale_g, ('b','b'):scale_b})
## Task 9
def grayscale():
'''
Input: None
Output: 3x3 greyscale matrix.
'''
return Mat(({'r','g','b'}, {'r', 'g', 'b'}), {('r','r'):77/256, ('r','g'): 151/256, ('r','b'):28/256,
('g','r'):77/256, ('g','g'): 151/256, ('g','b'):28/256,
('b','r'):77/256, ('b','g'): 151/256, ('b','b'):28/256})
## Task 10
def reflect_about(x1, y1, x2, y2):
'''
Input: 2 points that define a line to reflect about.
Output: Corresponding 3x3 reflect about matrix.
>>> def normsq(v): return v*v
>>> normsq(reflect_about(0,1,1,1) * Vec({'x','y','u'}, {'u':1}) - Vec({'x', 'u', 'y'},{'x': 0.0, 'u': 1, 'y': 2.0})) < 10e-15
True
>>> normsq(reflect_about(0,0,1,1) * Vec({'x','y','u'}, {'x':1, 'u':1}) - Vec({'x', 'u', 'y'},{'u': 1, 'y': 1})) < 1e-15
True
'''
theta = math.atan2(y2-y1, x2-x1)
return translation(x1, y1)*rotation(theta)*reflect_x()*rotation(-theta)*translation(-x1, -y1)
print(reflect_about(1,1,3,3) * Vec({'x','y','u'}, {'x':2, 'y':1, 'u':1}))