SHARE
TWEET

L-Systems 0.1.0

jeffwincek Dec 16th, 2011 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from turtle import *
  2. # L-Systems 0.1.0
  3. # By J.B. Wincek
  4.  
  5.  
  6. # These are the rules,grammars and alphabets for the differ L-Systems
  7. # They are organized by group and identified by the capitol letter
  8.  
  9. # Algae
  10. axiomA='A'
  11. rulesA = {}
  12. rulesA['A'] = 'AB'
  13. rulesA['B'] = 'A'
  14.  
  15. # Algae with the letters switched for testing perposes
  16. axiomB='B'
  17. rulesB = {}
  18. rulesB['B'] = 'BA'
  19. rulesB['A'] = 'B'
  20.  
  21. # Kock curve
  22. axiomF='F'
  23. rulesF = {}
  24. rulesF['F'] = 'F+F-F-F+F'
  25. turtleRulesF = {}
  26. turtleRulesF['F'] = 'forward(1)'
  27. turtleRulesF['+'] = 'left(85)'
  28. turtleRulesF['-'] = 'right(85)'
  29.  
  30. # Sierpinkski triangle
  31. axiomS='A'
  32. rulesS = {}
  33. rulesS['A'] = 'B-A-B'
  34. rulesS['B'] = 'A+B+A'
  35. turtleRulesS = {}
  36. turtleRulesS['A'] = 'forward(3)'
  37. turtleRulesS['B'] = 'forward(3)'
  38. turtleRulesS['+'] = 'left(60)'
  39. turtleRulesS['-'] = 'right(60)'
  40.  
  41. # Dragon curve
  42. axiomD='FX'
  43. rulesD={}
  44. rulesD['X']='X+YF'
  45. rulesD['Y']='FX-Y'
  46. turtleRulesD={}
  47. turtleRulesD['F'] = 'forward(7)'
  48. turtleRulesD['-'] = 'left(90)'
  49. turtleRulesD['+'] = 'right(90)'
  50. turtleRulesD['X'] = 'nul'
  51. turtleRulesD['Y'] = 'nul'
  52.  
  53. # Fractal Plant
  54. axiomP='X'
  55. positionHash = ()
  56. positionHeap = []
  57. rulesP={}
  58. rulesP['X']='F-[[X]+X]+F[+FX]-X'
  59. rulesP['F']='FF'
  60. turtleRulesP={}
  61. turtleRulesP['F'] = 'forward(3)'
  62. turtleRulesP['-'] = 'left(25)'
  63. turtleRulesP['+'] = 'right(25)'
  64. turtleRulesP['X'] = 'nul'
  65. turtleRulesP['['] = """positionHeap.append(hashPosition())"""
  66. turtleRulesP[']'] = 'restorePosition(positionHeap.pop())'
  67.  
  68. # these two methods are needed for storing the turles state
  69. def hashPosition():
  70.     output = ()
  71.     output = output + pos()
  72.     output = (output, heading())
  73.     return output
  74. def restorePosition(hashed):
  75.     pu()
  76.     setx(int(hashed[0][0]))
  77.     sety(int(hashed[0][1]))
  78.     setheading(hashed[1])
  79.     pd()
  80.     return hashed
  81.  
  82. # this applies the rules to the current axiom (the input string)
  83. # format compose(<string>, <Dictionary>)
  84. def compose(axiom, rules):
  85.         output = ""
  86.         for i in axiom:
  87.             output = output + rules.get(i,i)
  88.         return output
  89.  
  90. # the meat and potatoes of the genralized L system
  91. # format iterate(<string>,<dictionary>,<number>)
  92. # pass the starting condition as the axiom to this function
  93. def iterate(axiom,rules, times):
  94.     output = ''
  95.     if times == 0:
  96.         output = axiom
  97.     else:
  98.         return iterate(compose(axiom, rules), rules, times -1)
  99.     return output
  100.  
  101. # format for evaluate ( <which axiom to use>,
  102. #                       <which rule set to use> ,
  103. #                       < which ruleset for turtle navigation> ,
  104. #                       <how many iterations>)
  105. def evaluate(axiom,rules,turtleRules,times):
  106.     for words in iterate(axiom,rules,times):
  107.         if turtleRules[words] != 'nul':
  108.             exec(turtleRules[words])
  109.  
  110.  
  111. screensize(210,210)
  112.  
  113. speed(0)
  114. pu()
  115. setx(-350)
  116. sety(-350)
  117.  
  118. # this adjusts the turtles starting angle
  119. left(0)
  120. pd()
  121. color('pink','yellow')
  122. begin_fill()
  123. # these are examples of different rule sets, becareful high numbers will be slow
  124. #evaluate(axiomS,rulesS,turtleRulesS,6)
  125. evaluate(axiomF,rulesF,turtleRulesF,4)
  126. #evaluate(axiomP,rulesP,turtleRulesP,6)
  127.  
  128. end_fill()
  129. done()
  130.  
  131. #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