-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtles.py
80 lines (58 loc) · 1.26 KB
/
turtles.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
import turtle
import math
bob = turtle.Turtle()
edge_ = 40
def square(t, l):
for i in range(4):
t.fd(l)
t.lt(90)
def polygon(t, l, n):
for i in range(n):
t.fd(l)
t.lt(360/n)
def circle(t, r):
print(r)
polygon(t, 2 * math.pi * r / edge_, edge_)
def polyline(t, n, length, angle):
for i in range(n):
t.fd(length)
t.lt(angle)
def arc(t, r, angle):
arc_length = 2 * math.pi * r * angle / 360
n = int(arc_length / 3) + 1
step_length = arc_length / n
step_angle = angle / n
polyline(t, n, step_length, step_angle)
def leaf(t, r, angle):
t.rt(angle / 2)
arc(t, r, angle)
t.lt(180 - angle)
arc(t, r, angle)
t.lt(180 - angle)
t.lt(angle / 2)
def flower(t, n, angle):
for i in range(n):
leaf(bob, 50, angle)
bob.lt(360 / n)
def edge(t, length):
t.fd(length)
t.pu()
t.lt(180)
t.fd(length)
t.lt(180)
t.pd()
def shape(t, l, n):
polygon(t, l, n)
t.lt((n - 2) * 180 / n / 2)
t.pu()
r = l / (2 * math.sin(math.radians(180 / n)))
print(r)
t.fd(r)
t.pd()
t.lt(180)
for i in range(n):
edge(t, r)
t.lt(360 / n)
if __name__ == '__main__':
shape(bob, 200, 7)
turtle.mainloop()