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
| import turtle
turtle.speed(0)
turtle.penup()
turtle.setx(-turtle.window_width()*0.45)
turtle.sety(-turtle.window_height()*0.45)
turtle.pendown()
def apply_rules(S):
S = ("A", "B")
newS = ""
for i in S:
if i == "A":
newS += "B-A-B"
if i == "B":
newS += "A+B+A"
else:
newS += ""
return newS
def apply_rec(S,n):
if n == 0:
return S
else:
return n*(apply_rules(S), n-1)
def draw(S, length):
for i in [S]:
if [i] == "A" or "B":
turtle.forward(length)
elif [i] == ["+"]:
turtle.right(60)
elif [i] == ["-"]:
turtle.left(60)
return draw(S,length)
S = apply_rec("B", 8)
draw(S, 2)
turtle.getcanvas().postscript(files="l_system.eps")
turtle.done() |