Advertisement
Mangus875

L-System Turtle Graphics

Jan 10th, 2024 (edited)
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import math
  2. import turtle
  3. t = turtle.Turtle()
  4.  
  5. '''
  6. defaultRules = {
  7.     'F': [t.fd, segLen],
  8.     'B': [t.bk, segLen],
  9.     'f': [fd, segLen],
  10.     'b': [bk, segLen],
  11.     '-': [lt, ang],
  12.     '+': [rt, ang],
  13.     '[': [push],
  14.     ']': [pop],
  15.     '?': [peek],
  16.     '0': [t.pu],
  17.     '1': [t.pd]
  18. }
  19. '''
  20.  
  21. def setup():
  22.     t.ht()
  23.     t.tracer(rate)
  24.     t.width(thickness)
  25.     t.speed(speed)
  26.     t.rt(tilt)
  27.  
  28. def Lsystem(axiom, rules, iterations=1):
  29.     result = "";
  30.     for i in range(iterations):
  31.         for c in axiom:
  32.             if c in rules:
  33.                 result += rules[c]
  34.             else:
  35.                 result += c
  36.         axiom = result
  37.         result = ""
  38.     return axiom
  39. def drawPath(path, rules):
  40.     for c in path:
  41.         if c in rules:
  42.             rules[c][0](*rules[c][1:len(rules[c])])
  43.     t.tracer(1)
  44. def dPath(path, rules):
  45.     for c in path:
  46.         if c not in rules:
  47.             continue
  48.         if isinstance(rules[c], list):
  49.             for rule in rules[c]:
  50.                 eval(rule)
  51.         else:
  52.             eval(rules[c])
  53.            
  54.     t.tracer(1)
  55.  
  56. def fd(len):
  57.     t.pu()
  58.     t.fd(len)
  59.     t.pd()
  60. def bk(len):
  61.     t.pu()
  62.     t.bk(len)
  63.     t.pd()
  64. def lt(a):
  65.     t.speed(speed*5)
  66.     t.lt(a)
  67.     t.speed(speed)
  68. def rt(a):
  69.     t.speed(speed*5)
  70.     t.rt(a)
  71.     t.speed(speed)
  72. def seth(h):
  73.     tHead = t.heading()
  74.     t.write((h-tHead) % 180)
  75.     rt((tHead-h) % 360)
  76.  
  77. stack = []
  78. def push():
  79.     pos = [t.pos(), t.heading()]
  80.     stack.append(pos)
  81. def pop():
  82.     penDown = t.isdown()
  83.     pos = stack.pop()
  84.     t.pu()
  85.     t.speed(0)
  86.     t.seth(pos[1])
  87.     t.goto(pos[0])
  88.     t.speed(speed)
  89.     if isDown:
  90.         t.pd()
  91. def peek():
  92.     penDown = t.isdown()
  93.     pos = stack[-1]
  94.     t.speed(0)
  95.     t.seth(pos[1])
  96.     t.goto(pos[0])
  97.     t.speed(speed)
  98.     if isDown:
  99.         t.pd()
  100.  
  101. def branch():
  102.     t.fd(segLen)
  103.    
  104.  
  105. def main(fill = False):
  106.     global ang,segLen,iters,speed,rate,tilt,thickness
  107.    
  108.     tilt = 0
  109.     thickness = 1
  110.     rate = 1
  111.     speed = 3
  112.     iters = 0
  113.     segLen = 10
  114.     ang = 45
  115.    
  116.     setup()
  117.     t.pu()
  118.     t.goto(0, 0)
  119.     t.pd()
  120.  
  121.     path = Lsystem('0', {
  122.         '0': '1[0]0',
  123.         '1': '11'
  124.     }, iters)
  125.    
  126.     if fill:
  127.         t.begin_fill()
  128.     dPath(path, {
  129.         '0': ['t.fd(segLen)', 't.rt(90)', 't.circle(segLen/2)'],
  130.         '1': 't.fd(segLen)',
  131.         '[': ['push()', 't.lt(ang)'],
  132.         ']': ['pop()', 't.rt(ang)']
  133.     })
  134.     """
  135.     drawPath(path, {
  136.         '0': [leaf],
  137.         '1': [branch],
  138.         '[': [push],
  139.         ']': [pop]
  140.     })
  141.     """
  142.     t.end_fill()
  143.  
  144. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement