-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary tree.py
More file actions
40 lines (37 loc) · 839 Bytes
/
Copy pathbinary tree.py
File metadata and controls
40 lines (37 loc) · 839 Bytes
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
import turtle
bob = turtle.Turtle()
def line(x):
bob.fd(x)
bob.lt(180)
bob.fd(x)
bob.lt(180)
def branch(x, d, color, colorstep):
bob.width(0.5 + 0.08 * d ** 2)
bob.color([0, color, 0])
newx = x * (7 / 10)
angle = 30
bob.fd(x)
bob.lt(angle)
if d != 0: # left branch
branch(newx, d - 1, color + colorstep, colorstep)
else:
line(newx)
bob.rt(angle * 2)
if d != 0: # right branch
branch(newx, d - 1, color + colorstep, colorstep)
else:
line(newx)
bob.rt(180 - angle)
bob.penup()
bob.fd(x)
bob.pendown()
bob.rt(180)
def plantATree(x, d):
color = 0.3
colorstep = (1 - color) / d
turtle.tracer(100, 16)
# bob.speed(0)
bob.lt(90)
branch(x, d, color, colorstep)
turtle.exitonclick()
plantATree(100, 12)