chunks n trees

For participation @ the Biotext-Seminar Session (on BigBlueButton) please mail us: ground-zero [at] khm [dot] de

F -> F [ + F ] F [ – F ] F

# F means "draw forward" # + means "turn left 25°" # − means "turn right 25°" # [ means "push position and angle" # ] means "pop position and angle" # L-System Rule to draw ‘Fractal Tree’ # Rule: F: F[+F]F[-F]F # Angle: 25 # Start With: F # Iteration : 4
import turtle, random

t = turtle.Turtle()
t.speed(10)
t.hideturtle()
t.penup()

def l_system(s) :
  new_s = []
  for each in s :
    if each == 'F':
      new_s.append('F[+F]F[-F]F')    
    else :
        new_s.append(each)
  return new_s

def draw_l_system(x,y,s,b):
    cp = []
    t.goto(x,y)
    t.setheading(90)
    t.pendown()
    for each in s:
        if each == 'F' :
            t.forward(b)
        elif each == '+':
            t.left(25)        
        elif each == '-':
            t.right(25)        
        elif each == '[':
            cp.append((t.heading(),t.pos()))
        elif each == ']':
            heading, position = cp.pop()
            t.penup()
            t.goto(position)
            t.setheading(heading)
            t.pendown()
    t.penup()

s = 'F'
s2 = []

repeat = 4
for x in range (repeat) :
  s2 = l_system(s)
  s = ''.join(str(x) for x in s2)

draw_l_system(-70,-400,s,10)