-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparametric_lsystem_examples.py
More file actions
50 lines (40 loc) · 1.33 KB
/
parametric_lsystem_examples.py
File metadata and controls
50 lines (40 loc) · 1.33 KB
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
import turtle
from parametric_lsystem import ParametricLSystem
from renderer import render_parametric_pattern
def draw_sunflower():
angle = 137.5
times = 500
scale_factor = 15
# ω : A(0)
sunflower_omega = [("A", 0)]
# Parametric expansion rule: A(n) -> +(137.5) [ f(n^0.5) ~D ] A(n+1)
sunflower_expansion_rules = {
"A": lambda n: [
("+", angle),
("[", None),
("f", n**0.5),
("~D", None),
("]", None),
("A", n + 1)
]
}
stack = []
def push_position():
stack.append(turtle.pos())
def pop_position():
saved_pos = stack.pop()
turtle.penup()
turtle.setposition(saved_pos)
turtle.pendown()
sunflower_instructions = {
"+": lambda current_angle: turtle.left(current_angle),
"f": lambda distance: (turtle.penup(), turtle.forward(distance * scale_factor), turtle.pendown()),
"~D": lambda _: turtle.dot(8, "darkgreen"),
"[": lambda _: push_position(),
"]": lambda _: pop_position(),
}
system = ParametricLSystem(sunflower_omega, sunflower_expansion_rules)
pattern = system.expand(times=times)
render_parametric_pattern(pattern, sunflower_instructions, line_thickness=2)
if __name__ == "__main__":
draw_sunflower()