Guest User

Untitled

a guest
Dec 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. """
  2. Single Rule L-System Generator with DXF Output
  3. Jordan Perr - 2010 - All Rights Reserved
  4. Version 1
  5. """
  6.  
  7. ############### DEPENDENCIES ################
  8.  
  9. from math import cos, sin, pi
  10. import sdxf
  11.  
  12.  
  13. ############### CONFIGURATION ################
  14.  
  15. # Path to output file
  16. OFILE = "fern.dxf"
  17.  
  18. # Start position [xcor, ycor, theta]
  19. START = [0,0,pi/2]
  20.  
  21. # Segment of depth 0 will be this long
  22. SCALE = 20
  23.  
  24. # How many times should RULES be applied?
  25. # (Warning: Large DEPTH may freeze your machine.)
  26. DEPTH = 4
  27.  
  28. # L-System configuration (See ISBN:0262561271)
  29. ANGLE = 0.13962634
  30. DS = 0.4
  31. AXIOM = "F"
  32. RULE = "F=|[5+F][7-F]-|[4+F][6-F]-|[3+F][5-F]-|F"
  33.  
  34.  
  35. ################## CODE #####################
  36.  
  37. # Apply rule DEPTH times. Create turtle's roadmap.
  38. rule = RULE.split('=')
  39. commands = AXIOM[:]
  40. for step in range(DEPTH):
  41. commands = commands.replace(rule[0], '('+rule[1]+')')
  42.  
  43. # Set Up sdxf AutoCad dxf library.
  44. CAD = sdxf.Drawing()
  45. CAD.styles.append(sdxf.Style())
  46. CAD.views.append(sdxf.View('Normal'))
  47. CAD.views.append(sdxf.ViewByWindow('Window',leftBottom=(0,0),rightTop=(1,1))) #idem
  48.  
  49. # Precompute line segment distances. Prepare to draw.
  50. dst = [SCALE*(DS**l) for l in range(1,DEPTH+2)]
  51. stack = [START]
  52. coef = 1
  53. dep = 0
  54.  
  55. # Draw the L-System using a virtual turtle.
  56. for c in commands:
  57. if c in ['1','2','3','4','5','6','7','8','9']:
  58. coef = int(c)
  59. elif c == '(':
  60. dep = dep + 1
  61. elif c == ')':
  62. dep = dep - 1
  63. elif c == '[':
  64. stack.append(list(stack[-1]))
  65. elif c == ']':
  66. stack.pop()
  67. elif c == 'F' or c == '|':
  68. top = stack[-1]
  69. point1 = list(top)
  70. top[0] = top[0] + dst[dep]*cos(top[2])
  71. top[1] = top[1] + dst[dep]*sin(top[2])
  72. CAD.append(sdxf.Line(points=[point1[:2], list(top[:2])]))
  73. elif c == '+':
  74. top = stack[-1]
  75. top[2] = top[2] + ANGLE*coef
  76. coef = 1
  77. elif c == '-':
  78. top = stack[-1]
  79. top[2] = top[2] - ANGLE*coef
  80. coef = 1
  81.  
  82. # Save the DXF file
  83. CAD.saveas(OFILE)
Add Comment
Please, Sign In to add comment