-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolygons.py
More file actions
125 lines (111 loc) · 4.07 KB
/
Copy pathpolygons.py
File metadata and controls
125 lines (111 loc) · 4.07 KB
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
'''
Created on 20-Sep-2018
@author: rishab katta
@author: akhil karrothu
'''
import turtle
import sys
'''
use "python polygons.py 8 FILL" for the maximum size of the polygon
'''
sidelength = 200
colors = ['lightgreen', 'green', 'purple', 'pink', 'blue', 'black', 'grey', 'yellow', 'violet', 'brown', 'orange',
'red']
def init():
'''
:pre: relative pos(0,0), heading (east), up
:post: relative pos(-250,0), heading(east), up
:return: turtle and screen objects
'''
# Initializes and returns the turtle and screen objects, sets the world coordinates, writes author names
s = turtle.Screen()
s.screensize(500, 500)
s.title("polygons")
s.bgcolor("white")
s.setworldcoordinates(-600, -600, 600, 600)
t = turtle.Turtle()
t.up()
t.goto(-600, 600)
t.write("A1: Rishab A2: Akhil", font=("Arial", 10, "normal"))
t.goto(0, 0)
t.backward(250)
t.down()
turtle.Screen().tracer(0, 0)
return t, s
def draw_polygons_1(t, numberofsides, sidelength, isFill):
'''
:pre: relative pos(0,0), heading (east), up
:post: relative pos(0,0), heading(east), up
:return: total sum of sides in all of the polygons in figure
'''
# draws polygons of decreasing length recursively and returns the sum of sides of all the polygons drawn
sumofsides = 0
if numberofsides == 2:
return sumofsides
else:
if (isFill == "fill"):
t.fillcolor(colors[numberofsides])
t.begin_fill()
if isFill == "unfill":
t.pencolor(colors[numberofsides])
t.pensize(numberofsides - 2)
for i in range(numberofsides):
t.forward(sidelength)
t.left(360 / numberofsides)
sumofsides += sidelength
if (isFill == "fill"):
t.end_fill()
for j in range(numberofsides):
t.forward(sidelength)
t.left(360 / numberofsides)
sumofsides += draw_polygons_1(t,numberofsides - 1, sidelength / 2, isFill)
return sumofsides
def draw_polygons_2(t,numberofsides, sidelength, isFill):
'''
:pre: relative pos(0,0), heading (east), up
:post: relative pos(0,0), heading(east), up
:return: total sum of sides in all of the polygons in figure
'''
# draws polygons of increasing length recursively and returns the sum of sides of all the polygons drawn
# This polygon is drawn for the "intrestingness" part of the homework
sumofsides = 0
if numberofsides == 2:
return sumofsides
else:
if isFill == "fill":
t.fillcolor(colors[numberofsides])
t.begin_fill()
if isFill == "unfill":
t.pencolor(colors[numberofsides])
t.pensize(numberofsides - 2)
for i in range(numberofsides):
t.forward(sidelength)
t.left(360 / numberofsides)
sumofsides += sidelength
if isFill == "fill":
t.end_fill()
for j in range(numberofsides):
t.forward(sidelength)
t.left(360 / numberofsides)
sumofsides += draw_polygons_1(t,numberofsides - 1, sidelength * 1.5, isFill)
return sumofsides
def main():
'''
:pre: relative pos(0,0), heading (east), up
:post: relative pos(0,0), heading(east), up
:return: total sum of sides in all of the polygons in figure
'''
# Main function calls init function for initializing the turtle and screen objects and draw_polygons function for
# drawing all the polygons recursively.
numberofsides = int(sys.argv[1])
assert (numberofsides >= 3 and numberofsides <= 8), "number of sides must be in range 3-8"
isFill = str(sys.argv[2])
isFill = isFill.lower()
t, s =init()
print("sum of sides of polygon1 ", draw_polygons_1(t, numberofsides, sidelength, isFill))
t.penup()
t.forward(700)
t.pendown()
print("sum of sides of polygon2 ", draw_polygons_2(t, numberofsides, sidelength, isFill))
main()
turtle.Screen().mainloop()