-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_fractal.py
46 lines (36 loc) · 943 Bytes
/
tree_fractal.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
# draws a tree
import turtle
# set the canvas window
def set_canvas():
s = turtle.Screen()
s.setup(450, 410)
s.bgcolor('ivory')
s.title('Turtle Program')
return s
# set a turtle (a pen)
def set_pen(color):
t = turtle.Turtle()
t.shape('turtle')
t.pen(pencolor=color, fillcolor=color, pensize=1, speed=10)
return t
# draw a tree fractal using recursion
def draw_tree(t, branch, angle, n):
if n <= 0: # base case
return
else: # recursive step
t.forward(branch)
t.right(angle)
draw_tree(t, branch * 0.75, angle, n - 1) # right branch
t.left(angle * 2)
draw_tree(t, branch * 0.75, angle, n - 1) # left branch
t.right(angle)
t.backward(branch)
# main program
if __name__ == '__main__':
s = set_canvas()
t = set_pen('brown')
t.penup()
t.goto(-45, -150)
t.left(90)
t.pendown()
draw_tree(t, 60, 20, 6)