-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathherb.py
90 lines (76 loc) · 2.08 KB
/
herb.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
81
82
83
84
85
86
87
88
89
90
# Ashley Kang / Herb L-System
'''
Axiom: H
Rule 1: H -> HFX[+H][-H]
Rule 2: X -> X[-FFF][+FFF]FX
Turn 25.7 degrees
Graphics encoding in Turtle:
F = move forward
B = move backward
+ = turn right
- = turn left
[ = save current state
] = restore most recent state
'''
import turtle
# Define axiom rules
def applyRules(ch):
newStr = ""
if ch == 'H': # Axiom
newStr = 'HFX[+H][-H]' # Rule 1
elif ch == 'X':
newStr = 'X[-FFF][+FFF]FX' # Rule 2
else:
newStr = ch # Keep character in string
return newStr
# Produce a new string based on axiom rules
def processString(oldStr):
newStr = ""
for ch in oldStr:
newStr = newStr + applyRules(ch)
return newStr
# Create L-system based on number of iterations and axiom
def createLsystem(numIterations, axiom):
startStr = axiom
endStr = ""
for i in range(numIterations):
endStr = processString(startStr)
startStr = endStr
return endStr
'''
# Test L-system with 2 iterations
print(createLsystem(2, "H"))
# Output: HFX[+H][-H]FX[-FFF][+FFF]FX[+HFX[+H][-H]][-HFX[+H][-H]]
'''
# Draw L-system based on turtle object, instructions, angle, and distance
def drawLsystem(aTurtle, instructions, angle, distance):
savedInfoList = []
for cmd in instructions:
if cmd == 'F':
aTurtle.forward(distance)
elif cmd == 'B':
aTurtle.backward(distance)
elif cmd == '+':
aTurtle.right(angle)
elif cmd == '-':
aTurtle.left(angle)
elif cmd == '[':
savedInfoList.append([aTurtle.heading(), aTurtle.xcor(), aTurtle.ycor()])
print(savedInfoList)
elif cmd == ']':
newInfo = savedInfoList.pop()
aTurtle.setheading(newInfo[0])
aTurtle.setposition(newInfo[1], newInfo[2])
def main():
inst = createLsystem(6, "H")
print(inst)
turt = turtle.Turtle()
wn = turtle.Screen()
turt.speed(0)
#turtle.tracer(0.0)
turt.penup()
turt.setposition(-200, 0)
turt.pendown()
drawLsystem(turt, inst, 25.7, 5)
wn.exitonclick()
main()