/
binary_tree.py
34 lines (27 loc) · 997 Bytes
/
binary_tree.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
from l_system import Figure, LSystem, Pose, Turtle
l_system = LSystem(
variables='01',
constants='[]',
axiom='0',
rules={'0': '1[0]0', '1': '11'},
)
def push_and_turn_left(pose: Pose, stack: list[Pose], figures: list[Figure]) -> Pose:
new_pose = Turtle.get_push()(pose, stack, figures)
return Turtle.get_turn(45.0)(new_pose, stack, figures)
def pop_and_turn_right(pose: Pose, stack: list[Pose], figures: list[Figure]) -> Pose:
new_pose = Turtle.get_pop()(pose, stack, figures)
return Turtle.get_turn(-45.0)(new_pose, stack, figures)
s = l_system.axiom
for n in range(8):
turtle = Turtle(
direction=90.0,
move_rules={
'0': Turtle.get_go_forward('green', 2),
'1': Turtle.get_go_forward('brown', 2),
'[': push_and_turn_left,
']': pop_and_turn_right,
},
)
turtle.move(s)
turtle.show_and_save(f'n = {n}', output_path=f'resource/binary_tree_{n}.svg')
s = l_system.step(s)