-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflower.py
70 lines (54 loc) · 1.43 KB
/
flower.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
import turtle
import math
def arco(turtle, raggio, angolo):
arco_lunghezza = 2 * math.pi * raggio * angolo / 360
n = int(arco_lunghezza / 3) + 1
passo_lunghezza = arco_lunghezza / n
passo_angolo = float(angolo) / n
polilinea(turtle, n, passo_lunghezza, passo_angolo)
def polilinea(t, n, length, angle):
"""Draws n line segments.
t: Turtle object
n: number of line segments
length: length of each segment
angle: degrees between segments
"""
for i in range(n):
t.fd(length)
t.lt(angle)
def petal(t, r, angle):
"""Draws a petal using two arcs.
t: Turtle
r: radius of the arcs
angle: angle (degrees) that subtends the arcs
"""
for i in range(2):
arco(t, r, angle)
t.lt(180-angle)
def flower(t, n, r, angle):
"""Draws a flower with n petals.
t: Turtle
n: number of petals
r: radius of the arcs
angle: angle (degrees) that subtends the arcs
"""
for i in range(n):
petal(t, r, angle)
t.lt(360.0/n)
def move(t, length):
"""Move Turtle (t) forward (length) units without leaving a trail.
Leaves the pen down.
"""
t.pu()
t.fd(length)
t.pd()
bob = turtle.Turtle()
# draw a sequence of three flowers, as shown in the book.
move(bob, -100)
flower(bob, 7, 60.0, 60.0)
move(bob, 100)
flower(bob, 10, 40.0, 80.0)
move(bob, 100)
flower(bob, 20, 140.0, 20.0)
bob.hideturtle()
turtle.mainloop()