-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform_polygons.py
380 lines (309 loc) · 10.6 KB
/
transform_polygons.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from turtle import *
from random import randint, randrange
#import tkinter as tk
MAX = 20
MIN = -20
PENCOLOR = (randint(0, 255), randint(0, 255), randint(0, 255))
all_shapes = []
def main():
screen = Screen()
screen.setup(400, 400)
screen.setworldcoordinates(MIN, MIN, MAX, MAX)
colormode(255)
pencolor(PENCOLOR)
draw_axes()
pencolor(randint(0, 255), randint(0, 255), randint(0, 255))
#nick_project()
#preston_project()
#alden_project()
#aiden_project()
tyson_project()
#example()
#mrl_project()
"""
print(all_shapes)
for shape in all_shapes:
pencolor(randint(0, 255), randint(0, 255), randint(0, 255))
reflect('x', shape) """
screen.exitonclick()
def shift_pencolor(pc, shift):
new_pc = [int(pc[0] + shift), int(pc[1] + shift), int(pc[2] + shift)]
for i in range(len(new_pc)):
if new_pc[i] > 255:
new_pc[i] = new_pc[i] % 255
return tuple(new_pc)
def draw_axes():
pu()
goto(0, MAX)
pd()
goto(0, MIN)
pu()
goto(MAX, 0)
pd()
goto(MIN, 0)
def convert_points(shapes):
local_max = 0
for shape in shapes:
new_max = max([x for x,y in shape] + [y for x,y in shape])
if new_max > local_max:
local_max = new_max
new_shapes = []
for shape in shapes:
new_shape = []
for x, y in shape:
point = (x / local_max * MAX, y / local_max * MAX)
new_shape.append(point)
new_shapes.append(new_shape)
return new_shapes
def draw_polygon(points):
points.append(points[0])
pu()
begin_poly()
goto(points[0])
pd()
for point in points[1:]:
goto(point)
end_poly()
all_shapes.append(get_poly())
return get_poly()
def translate(vector, shape, draw = True):
new_shape = []
for coord in shape:
new_shape.append((coord[0] + vector[0], coord[1] + vector[1]))
if draw: draw_polygon(new_shape)
return new_shape
def reflect(axis, shape, draw = True):
new_shape = []
for coord in shape:
if axis == 'x':
new_shape.append((coord[0], -1 * coord[1]))
elif axis == 'y':
new_shape.append((-1 * coord[0], coord[1]))
elif axis == 'y=x' or axis == 'y = x' or axis == 'x = y' or axis == 'x=y':
new_shape.append((coord[1], coord[0]))
if draw: draw_polygon(new_shape)
return new_shape
def rotate(deg, shape, dir = 'CW', draw = True):
deg = deg % 360
new_shape = []
if deg == 0:
return shape
elif deg == 60:
if dir == 'CW':
for coord in shape:
x = coord[0]/2 - coord[1]*(3**.5)/2
y = coord[0]*3**.5/2 + coord[1]/2
new_shape.append((x, y))
elif deg == 180:
for coord in shape:
new_shape.append((-1 * coord[0], -1 * coord[1]))
elif deg == 90:
if dir == 'CW':
for coord in shape:
new_shape.append((coord[1], -1 * coord[0]))
elif dir == 'CCW':
for coord in shape:
new_shape.append((-1 * coord[1], coord[0]))
if draw: draw_polygon(new_shape)
return new_shape
def dilate(factor, shape, draw = True):
new_shape = []
for coord in shape:
new_shape.append((factor * coord[0], factor * coord[1]))
if draw: draw_polygon(new_shape)
return new_shape
def nick_project():
shape_A = [ (3,0), (0.45,-6), (3,-8), (6.45,-6) ]
draw_polygon(shape_A)
shape_B = rotate(60, shape_A)
shape_C = rotate(60, shape_B)
shape_D = rotate(60, shape_C)
shape_E = rotate(60, shape_D)
shape_F = rotate(60, shape_E)
reflect( 'x' , shape_A)
reflect( 'x' , shape_B)
reflect( 'x' , shape_C)
reflect( 'x' , shape_D)
reflect( 'x' , shape_E)
reflect( 'x' , shape_F)
def preston_project():
Rec1 = [(0, 0), (1, 0), (1,1),(0, 1)]
Rec2 = [(0, 0), (2, 0), (2, 1), (0, 1)]
Rec4 = [(0, 0), (4,0), (4, 1), (0, 1)]
Rec5 = [(0, 0), (5,0), (5, 1), (0, 1)]
Rec6 = [(0, 0), (6,0), (6, 1), (0, 1)]
Rec7 = [(0, 0), (7,0), (7, 1), (0, 1)]
Rec8 = [(0, 0), (8,0), (8, 1), (0, 1)]
translate((0, 1), Rec1)
translate((1, 0), Rec1)
translate((3, 2), Rec1)
translate((6, 13), Rec1)
translate((4, 16), Rec1)
translate((1, 11), Rec1)
translate((0, 10), Rec1)
translate((-3, 8), Rec1)
translate((-6, 8), Rec1)
translate((-7, 9), Rec1)
translate((-7, 4), Rec1)
translate((-6, 3), Rec1)
translate((-5, 2), Rec1)
translate((-4, 1), Rec1)
translate((-2, -1), Rec1)
translate((-1, 0), Rec1)
translate((2, -3), Rec2)
translate((6, 9), Rec2)
translate((-2, 9), Rec2)
translate((-5, 7), Rec2)
translate((-3, -3), Rec2)
translate((5, 3), rotate(90, Rec2, 'CCW', False))
translate((8, 8), rotate(90, Rec2, 'CCW', False))
translate((6, 10), rotate(90, Rec2, 'CCW', False))
translate((6, 12), Rec4)
translate((6, 5), rotate(90, Rec4, 'CCW', False))
translate((12, 14), rotate(90, Rec4, 'CCW', False))
translate((-2, -3), rotate(90, Rec4, 'CCW', False))
translate((7, 14), Rec5)
translate((3, -3), rotate(90, Rec5, 'CCW', False))
translate((3, 12), rotate(90, Rec6, 'CCW', False))
translate((-7, 5), rotate(90, Rec7, 'CCW', False))
translate((3, 18), Rec8)
def mrl_project():
pencolor(shift_pencolor(pencolor(), 110))
rectangle = [(9, 0), (9, 2), (10, 2), (10, 0)]
triangle = [(-2, 3), (0, 5), (2, 3)]
draw_polygon(rectangle)
draw_polygon(triangle)
# Reflect Rectangle over y-axis
opp_rect = reflect('y', rectangle)
# Dilate Triangle with scale factor of 2, translate <0, -3>
# Dilate Triangle with scale factor of 2, translate <-6, -3>
# Dilate Triangle with scale factor of 2, translate <6, -3>
translate((0, -3), dilate(2, triangle, False))
translate((-6, -3), dilate(2, triangle, False))
translate((6, -3), dilate(2, triangle, False))
# Translate Rectangle <-2, 0>
# Translate Rectangle <-4, 0>
# Translate Rectangle <-6, 0>
# Translate Rectangle <-8, 0>
translate((-2, 0), rectangle)
translate((-4, 0), rectangle)
translate((-6, 0), rectangle)
translate((-8, 0), rectangle)
# Translate Opposite Rectangle <2, 0>
# Translate Opposite Rectangle <4, 0>
# Translate Opposite Rectangle <6, 0>
# Translate Opposite Rectangle <8, 0>
translate((2, 0), opp_rect)
translate((4, 0), opp_rect)
translate((6, 0), opp_rect)
translate((8, 0), opp_rect)
# Rotate Rectangle 90 CCW, translate <-5, -7>
# Rotate Rectangle 90 CCW, translate <-3, -7>
# Rotate Rectangle 90 CCW, translate <-1, -7>
# Rotate Rectangle 90 CCW, translate <1, -7>
# Rotate Rectangle 90 CCW, translate <3, -7>
# Rotate Rectangle 90 CCW, translate <5, -7>
# Rotate Rectangle 90 CCW, translate <7, -7>
# Rotate Rectangle 90 CCW, translate <9, -7>
translate((-7, -7), rotate(90, rectangle, 'CCW', False))
translate((-5, -7), rotate(90, rectangle, 'CCW', False))
translate((-3, -7), rotate(90, rectangle, 'CCW', False))
translate((-1, -7), rotate(90, rectangle, 'CCW', False))
translate((1, -7), rotate(90, rectangle, 'CCW', False))
translate((3, -7), rotate(90, rectangle, 'CCW', False))
translate((5, -7), rotate(90, rectangle, 'CCW', False))
translate((7, -7), rotate(90, rectangle, 'CCW', False))
translate((9, -7), rotate(90, rectangle, 'CCW', False))
def example():
shape1 = [(1, 2), (3, 5), (-3, -2)]
shape2 = [(3, 4), (-1, -3), (-4, -6), (-2, 4)]
translate((-3, 4), shape1)
dilate(2, shape2)
rotate(180, shape1)
reflect('y', shape2)
def alden_project():
"""
Picture created exactly as planned!
4 figures, translations and reflections used, no rotations.
90%
"""
Squair = [(2, 10), (4, 10), (4, 8), (2, 8)]
Rectangle = [(4, -1), (6, -1), (6, 3), (4, 3)]
Triangle = [(4, -1), (6, -1), (4, -3)]
Squair2 = reflect('y', Squair)
draw_polygon(Squair)
draw_polygon(Rectangle)
draw_polygon(Triangle)
translate((0, -2), Squair)
translate((-2, -2), Squair)
translate((0, -4), Squair)
translate((2, -2), Squair)
translate((0, -2), Squair2)
translate((-2, -2), Squair2)
translate((0, -4), Squair2)
translate((2, -2), Squair2)
reflect('y', Rectangle)
reflect('y', Triangle)
def aiden_project():
"""
Great job! This was well-planned and accurately described.
4 shapes, translation, reflection, and dilation used (no rotation)
95%
"""
Triangle = [ (1,6), (2,8), (4,9) ]
Octagon = [ (-1,6), (1,6), (2,5), (2,3), (1,2), (-1,2), (-2,3), (-2,5) ]
Rectangle = [ (-7,9), (7,9), (7,-6), (-7,-6) ]
Rectangle2 = [ (2,1), (7,1), (7,-1), (2,-1) ]
draw_polygon(Triangle)
draw_polygon(Octagon)
draw_polygon(Rectangle)
draw_polygon(Rectangle2)
reflect('y', Triangle)
translate((0,-4), Octagon)
dilate(2, Rectangle)
#Mr. L addition:
reflect('y', Rectangle2)
def tyson_project():
"""
Included 8 shapes, translations, reflections, and rotations.
Some inaccuracies, mainly rotations (watch out for those!), but overall accurate and a good picture!
95%
"""
trapezoid = [(-10,-3) , (-6,0) , (6,0), (10,-3)]
rectangle = [(-6,0), (-6,7), (-5,7), (-5,0)]
draw_polygon (trapezoid)
draw_polygon (rectangle)
translate((3,0), rectangle)
translate((5,0), rectangle)
translate((8,0), rectangle)
translate((10,0), rectangle)
translate((12,0), rectangle)
triangle = [(-7,7), (0,10), (0,7)]
draw_polygon(triangle)
reflect ('y', triangle)
Rectangle1 =[(6 ,0), (0 ,0), (0 ,1), (6 ,1)]
Rectangle2 =[(7 ,-1), (7 ,-2), (0 , -2), (0, -1)]
Rectangle3 =[( 9, -3), (9 , -4), (0 ,-4), (0 , -3)]
draw_polygon(Rectangle1)
draw_polygon(Rectangle2)
draw_polygon(Rectangle3)
rotate(180, Rectangle1)
rotate(180, Rectangle2)
rotate(180, Rectangle3)
Rect4 = [(-2, 6), (-2, 10), (-4, 10), (-4, 6)]
draw_polygon(Rect4)
reflect('y', Rect4)
Rect5 = [(2, 6), (2, 10), (4, 10), (4, 6)]
draw_polygon(Rect5)
translate((0, 6), rotate(90, Rect5, "CCW", False))
def build_ui(window):
label = tk.Label(text = "Trasnformation builder").pack()
button_make_poly = tk.Button(text = "Create Polygon",
width=30,
height = 10,
command = lambda: make_poly(entry_num_points.get())
).pack()
entry_num_points = tk.Entry(width = 50).pack()
window.mainloop()
if __name__ == "__main__":
main()