-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfern.py
80 lines (67 loc) · 1.5 KB
/
fern.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
71
72
73
74
75
76
77
78
79
80
'''
Fern L-System
By Cameron Cassells
An implementation of an L-system for a fern found in Przemyslaw Prusinkiewicz &
Aristid Lindenmayer's book "The Algorithmic Beauty of Plants".
V = {X,F,+,-,[,]}
ω = X
P = {(X→F+[[X]-X]-F[-FX]+X),(F→FF)}
n = 8
δ = 22.5
'''
import turtle
#Creation Parameters
GENERATION = 8
LENGTH = 2
ANGLE = 22.5
AXIOM = "X"
#Placement Parameters
INIT_X = -650
INIT_Y = -450
INIT_HEADING = 45
#Generate string according to rules
for _ in range(GENERATION):
temp_string = ""
for char in AXIOM:
if char == 'X':
temp_string += "F+[[X]-X]-F[-FX]+X"
elif char == 'F':
temp_string += "FF"
else:
temp_string += char
AXIOM = temp_string
print(AXIOM)
#Initialise canvas
window = turtle.Screen()
window.title("ABOP Fern")
window.bgcolor("#222222")
window.tracer(0) #Comment out to see drawing
t = turtle.Turtle()
t.pencolor("#adff00")
t.speed("fastest")
t.hideturtle()
t.penup()
t.setposition(INIT_X, INIT_Y)
t.setheading(INIT_HEADING)
t.pendown()
#Draw plant
stack = []
for char in AXIOM:
if char == 'X':
continue
elif char == 'F':
t.forward(LENGTH)
elif char == '+':
t.left(ANGLE)
elif char == '-':
t.right(ANGLE)
elif char == '[':
stack.append((t.position(), t.heading()))
elif char == ']':
position, heading = stack.pop()
t.penup()
t.setposition(position)
t.setheading(heading)
t.pendown()
window.update()
window.mainloop()