Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from turtle import *
- # L-Systems 0.1.0
- # By J.B. Wincek
- # These are the rules,grammars and alphabets for the differ L-Systems
- # They are organized by group and identified by the capitol letter
- # Algae
- axiomA='A'
- rulesA = {}
- rulesA['A'] = 'AB'
- rulesA['B'] = 'A'
- # Algae with the letters switched for testing perposes
- axiomB='B'
- rulesB = {}
- rulesB['B'] = 'BA'
- rulesB['A'] = 'B'
- # Kock curve
- axiomF='F'
- rulesF = {}
- rulesF['F'] = 'F+F-F-F+F'
- turtleRulesF = {}
- turtleRulesF['F'] = 'forward(1)'
- turtleRulesF['+'] = 'left(85)'
- turtleRulesF['-'] = 'right(85)'
- # Sierpinkski triangle
- axiomS='A'
- rulesS = {}
- rulesS['A'] = 'B-A-B'
- rulesS['B'] = 'A+B+A'
- turtleRulesS = {}
- turtleRulesS['A'] = 'forward(3)'
- turtleRulesS['B'] = 'forward(3)'
- turtleRulesS['+'] = 'left(60)'
- turtleRulesS['-'] = 'right(60)'
- # Dragon curve
- axiomD='FX'
- rulesD={}
- rulesD['X']='X+YF'
- rulesD['Y']='FX-Y'
- turtleRulesD={}
- turtleRulesD['F'] = 'forward(7)'
- turtleRulesD['-'] = 'left(90)'
- turtleRulesD['+'] = 'right(90)'
- turtleRulesD['X'] = 'nul'
- turtleRulesD['Y'] = 'nul'
- # Fractal Plant
- axiomP='X'
- positionHash = ()
- positionHeap = []
- rulesP={}
- rulesP['X']='F-[[X]+X]+F[+FX]-X'
- rulesP['F']='FF'
- turtleRulesP={}
- turtleRulesP['F'] = 'forward(3)'
- turtleRulesP['-'] = 'left(25)'
- turtleRulesP['+'] = 'right(25)'
- turtleRulesP['X'] = 'nul'
- turtleRulesP['['] = """positionHeap.append(hashPosition())"""
- turtleRulesP[']'] = 'restorePosition(positionHeap.pop())'
- # these two methods are needed for storing the turles state
- def hashPosition():
- output = ()
- output = output + pos()
- output = (output, heading())
- return output
- def restorePosition(hashed):
- pu()
- setx(int(hashed[0][0]))
- sety(int(hashed[0][1]))
- setheading(hashed[1])
- pd()
- return hashed
- # this applies the rules to the current axiom (the input string)
- # format compose(<string>, <Dictionary>)
- def compose(axiom, rules):
- output = ""
- for i in axiom:
- output = output + rules.get(i,i)
- return output
- # the meat and potatoes of the genralized L system
- # format iterate(<string>,<dictionary>,<number>)
- # pass the starting condition as the axiom to this function
- def iterate(axiom,rules, times):
- output = ''
- if times == 0:
- output = axiom
- else:
- return iterate(compose(axiom, rules), rules, times -1)
- return output
- # format for evaluate ( <which axiom to use>,
- # <which rule set to use> ,
- # < which ruleset for turtle navigation> ,
- # <how many iterations>)
- def evaluate(axiom,rules,turtleRules,times):
- for words in iterate(axiom,rules,times):
- if turtleRules[words] != 'nul':
- exec(turtleRules[words])
- screensize(210,210)
- speed(0)
- pu()
- setx(-350)
- sety(-350)
- # this adjusts the turtles starting angle
- left(0)
- pd()
- color('pink','yellow')
- begin_fill()
- # these are examples of different rule sets, becareful high numbers will be slow
- #evaluate(axiomS,rulesS,turtleRulesS,6)
- evaluate(axiomF,rulesF,turtleRulesF,4)
- #evaluate(axiomP,rulesP,turtleRulesP,6)
- end_fill()
- done()
- #special thanks to http://www.4dsolutions.net/ocn/lsystems.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement