SHARE
TWEET

L-Systems 0.0.5

jeffwincek Dec 16th, 2011 39 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from turtle import *
  2.  
  3. #Version 0.0.5 by J.B. Wincek
  4. #The next version will be 0.1.0 which will be code clean up and comments
  5. #Changes from version 0.0.4: added in fractal Plant based grammar
  6.  
  7.  
  8. axiomA='A'
  9. rulesA = {}
  10. rulesA['A'] = 'AB'
  11. rulesA['B'] = 'A'
  12.  
  13. axiomB='B'
  14. rulesB = {}
  15. rulesB['B'] = 'BA'
  16. rulesB['A'] = 'B'
  17.  
  18. axiomF='F'
  19. rulesF = {}
  20. rulesF['F'] = 'F+F-F-F+F'
  21. turtleRulesF = {}
  22. turtleRulesF['F'] = 'forward(10)'
  23. turtleRulesF['+'] = 'left(90)'
  24. turtleRulesF['-'] = 'right(90)'
  25.  
  26. axiomS='A'
  27. rulesS = {}
  28. rulesS['A'] = 'B-A-B'
  29. rulesS['B'] = 'A+B+A'
  30. turtleRulesS = {}
  31. turtleRulesS['A'] = 'forward(3)'
  32. turtleRulesS['B'] = 'forward(3)'
  33. turtleRulesS['+'] = 'left(60)'
  34. turtleRulesS['-'] = 'right(60)'
  35.  
  36. axiomD='FX'
  37. rulesD={}
  38. rulesD['X']='X+YF'
  39. rulesD['Y']='FX-Y'
  40. turtleRulesD={}
  41. turtleRulesD['F'] = 'forward(3)'
  42. turtleRulesD['-'] = 'left(90)'
  43. turtleRulesD['+'] = 'right(90)'
  44. turtleRulesD['X'] = 'nul'
  45. turtleRulesD['Y'] = 'nul'
  46.  
  47. axiomP='X'
  48. positionHash = ()
  49. positionHeap = []
  50. rulesP={}
  51. rulesP['X']='F-[[X]+X]+F[+FX]-X'
  52. rulesP['F']='FF'
  53. turtleRulesP={}
  54. turtleRulesP['F'] = 'forward(3)'
  55. turtleRulesP['-'] = 'left(25)'
  56. turtleRulesP['+'] = 'right(25)'
  57. turtleRulesP['X'] = 'nul'
  58. turtleRulesP['['] = """positionHeap.append(hashPosition())"""
  59. turtleRulesP[']'] = 'restorePosition(positionHeap.pop())'
  60.  
  61. def hashPosition():
  62.     output = ()
  63.     output = output + pos()
  64.     output = (output, heading())
  65.     return output
  66.  
  67. def restorePosition(hashed):
  68.     pu()
  69.     setx(int(hashed[0][0]))
  70.     sety(int(hashed[0][1]))
  71.     setheading(hashed[1])
  72.     pd()
  73.     return hashed
  74.  
  75. def compose(axiom, rules):
  76.         output = ""
  77.         for i in axiom:
  78.             output = output + rules.get(i,i)
  79.         return output
  80.  
  81. def iterate(axiom,rules, times):
  82.     output = ''
  83.     if times == 0:
  84.         output = axiom
  85.     else:
  86.         return iterate(compose(axiom, rules), rules, times -1)
  87.     return output
  88.  
  89. # format for evaluate ( <which axiom to use>,
  90. #                       <which rule set to use> ,
  91. #                       < which ruleset for turtle navigation> ,
  92. #                       <how many iterations>)
  93. def evaluate(axiom,rules,turtleRules,times):
  94.     for words in iterate(axiom,rules,times):
  95.         if turtleRules[words] != 'nul':
  96.             exec(turtleRules[words])
  97.  
  98. #print iterate(axiomA,rulesB,6)
  99. #print iterate(axiomB,rulesA,6)
  100. #directions = iterate(axiomF,rulesF,3)
  101. #print directions
  102.  
  103. screensize(210,210)
  104.  
  105. speed(0)
  106. pu()
  107. setx(200)
  108. sety(-200)
  109. left(90)
  110. pd()
  111. color('red','yellow')
  112. #begin_fill()
  113. #evaluate(axiomS,rulesS,turtleRulesS,6)
  114. #evaluate(axiomD,rulesD,turtleRulesD,14)
  115. #testString = "newHash = hashPosition()"
  116. #exec(testString)
  117. #print newHash
  118.  
  119. evaluate(axiomP,rulesP,turtleRulesP,6)
  120.  
  121. #end_fill()
  122. done()
  123.  
  124. #special thanks to http://www.4dsolutions.net/ocn/lsystems.html
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top