Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import turtle
- t = turtle.Turtle()
- '''
- defaultRules = {
- 'F': [t.fd, segLen],
- 'B': [t.bk, segLen],
- 'f': [fd, segLen],
- 'b': [bk, segLen],
- '-': [lt, ang],
- '+': [rt, ang],
- '[': [push],
- ']': [pop],
- '?': [peek],
- '0': [t.pu],
- '1': [t.pd]
- }
- '''
- def setup():
- t.ht()
- t.tracer(rate)
- t.width(thickness)
- t.speed(speed)
- t.rt(tilt)
- def Lsystem(axiom, rules, iterations=1):
- result = "";
- for i in range(iterations):
- for c in axiom:
- if c in rules:
- result += rules[c]
- else:
- result += c
- axiom = result
- result = ""
- return axiom
- def drawPath(path, rules):
- for c in path:
- if c in rules:
- rules[c][0](*rules[c][1:len(rules[c])])
- t.tracer(1)
- def dPath(path, rules):
- for c in path:
- if c not in rules:
- continue
- if isinstance(rules[c], list):
- for rule in rules[c]:
- eval(rule)
- else:
- eval(rules[c])
- t.tracer(1)
- def fd(len):
- t.pu()
- t.fd(len)
- t.pd()
- def bk(len):
- t.pu()
- t.bk(len)
- t.pd()
- def lt(a):
- t.speed(speed*5)
- t.lt(a)
- t.speed(speed)
- def rt(a):
- t.speed(speed*5)
- t.rt(a)
- t.speed(speed)
- def seth(h):
- tHead = t.heading()
- t.write((h-tHead) % 180)
- rt((tHead-h) % 360)
- stack = []
- def push():
- pos = [t.pos(), t.heading()]
- stack.append(pos)
- def pop():
- penDown = t.isdown()
- pos = stack.pop()
- t.pu()
- t.speed(0)
- t.seth(pos[1])
- t.goto(pos[0])
- t.speed(speed)
- if isDown:
- t.pd()
- def peek():
- penDown = t.isdown()
- pos = stack[-1]
- t.speed(0)
- t.seth(pos[1])
- t.goto(pos[0])
- t.speed(speed)
- if isDown:
- t.pd()
- def branch():
- t.fd(segLen)
- def main(fill = False):
- global ang,segLen,iters,speed,rate,tilt,thickness
- tilt = 0
- thickness = 1
- rate = 1
- speed = 3
- iters = 0
- segLen = 10
- ang = 45
- setup()
- t.pu()
- t.goto(0, 0)
- t.pd()
- path = Lsystem('0', {
- '0': '1[0]0',
- '1': '11'
- }, iters)
- if fill:
- t.begin_fill()
- dPath(path, {
- '0': ['t.fd(segLen)', 't.rt(90)', 't.circle(segLen/2)'],
- '1': 't.fd(segLen)',
- '[': ['push()', 't.lt(ang)'],
- ']': ['pop()', 't.rt(ang)']
- })
- """
- drawPath(path, {
- '0': [leaf],
- '1': [branch],
- '[': [push],
- ']': [pop]
- })
- """
- t.end_fill()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement