-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtleTest.py
More file actions
62 lines (53 loc) · 1.54 KB
/
turtleTest.py
File metadata and controls
62 lines (53 loc) · 1.54 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
51
52
53
54
55
56
57
58
59
60
61
62
import turtle
brackets = []
def createLSystem(numIters,axiom):
startString = axiom
endString = ""
for i in range(numIters):
endString = processString(startString)
startString = endString
return endString
def processString(oldStr):
newstr = ""
for ch in oldStr:
newstr = newstr + applyRules(ch)
return newstr
def applyRules(ch):
newstr = ""
if ch == 'f':
newstr = 'f[+f[+f-f][-f+f]][-f[-f+f][+f-f]]fffffff' # Rule 1
else:
newstr = ch # no rules apply so keep the character
return newstr
def drawLsystem(aTurtle, instructions, angle, distance):
global brackets
for cmd in instructions:
if cmd == 'f':
aTurtle.forward(distance)
elif cmd == 'B':
aTurtle.backward(distance)
elif cmd == '[':
brackets.append((aTurtle.position(),aTurtle.heading()))
elif cmd == ']':
pos,head = brackets.pop()
aTurtle.penup()
aTurtle.setposition(pos)
aTurtle.setheading(head)
aTurtle.pendown()
elif cmd == '+':
aTurtle.right(angle)
elif cmd == '-':
aTurtle.left(angle)
def main():
inst = createLSystem(2, "f") # create the string
print inst
t = turtle.Turtle() # create the turtle
wn = turtle.Screen()
t.up()
t.back(200)
t.down()
t.speed(9)
drawLsystem(t, inst, 80, 15) # draw the picture
# angle 60, segment length 5
wn.exitonclick()
main()