Skip to content

Instantly share code, notes, and snippets.

@analogpixel
Created July 7, 2015 22:06
Show Gist options
  • Save analogpixel/29d3f1420586e3a9ee0a to your computer and use it in GitHub Desktop.
Save analogpixel/29d3f1420586e3a9ee0a to your computer and use it in GitHub Desktop.
Python Turtle Lindermayer System
#!/usr/bin/python
import turtle
system = {
'a': ['b','|','c'],
'b': ['b'],
'c': ['b','|','d'],
'd': ['e','/','d'],
'e': ['f'],
'f': ['g'],
'g': ['h',['a']],
'h': ['h']
}
drawcmd = {
'a': 'f:500',
'b': 'r:45,f:1000',
'c': 'r:45,f:150',
'd': 'r:45,f:110',
'e': 'r:45,f:500,l:45',
'f': 'l:45,f:400,l:7,f:320',
'g': 'l:45,f:100,r:90,f:320',
'h': 'r:45,f:500',
'|': 'r:30,f:400',
'/': 'l:30,f:300,r:45,f:200,r:45,f:100',
}
#turtle.screensize(1920,1080)
turtle.setworldcoordinates(-5000,-5000, 5000,5000)
turtles = []
globalt = turtle.Turtle()
#globalt.speed('fastest')
def push():
global turtles,globalt
turtles.append( [globalt.pos(), globalt.heading()] )
def pop():
global turtles,globalt
d = turtles.pop()
currentPen = globalt.pen()
globalt.pu()
globalt.setpos(d[0])
globalt.setheading( d[1])
globalt.pen(currentPen)
def draw(cmds):
global globalt
for cmd in cmds:
if type(cmd) is list:
push()
#globalt.right(45)
#globalt.forward(10)
draw(cmd)
pop()
else:
if cmd in drawcmd:
for dc in drawcmd[cmd].split(','):
(k,v) = dc.split(':')
if k == 'd':
globalt.dot(int(v))
elif k == 'f':
globalt.forward(int(v))
elif k == 'r':
globalt.right(int(v))
elif k == 'l':
globalt.left(int(v))
elif k == 'pu':
globalt.pu()
elif k == 'pd':
globalt.pd()
def step(systemin):
out = []
for cmd in systemin:
if type(cmd) is list:
out.append( step(cmd) )
elif cmd in system:
out = out + system[cmd]
return out
a = ['a']
for i in range(40):
a = step(a)
#print a
turtle.tracer(1000,0)
draw(a)
#turtle.onscreenclick(update)
#turtle.mainloop()
turtle.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment