def sierpinsky(fn, angle=60, step=6): turtle = Turtle(fn, (50, 50)) turtle.left(90) lsystem = LSystem('A', {'A': 'B-A-B', 'B': 'A+B+A'}) turtle.resetpos((400, 400), 180) lsystem.run(turtle, depth=6, step=step, angle=angle) turtle.save(fn, frame=(500, 550))
def koch(fn): turtle = Turtle(fn, (50, 50)) turtle.left(90) lsystem = LSystem('F--F--F', {'F': 'F+F--F+F'}) turtle.resetpos((100, 150), -90) lsystem.run(turtle, depth=3, step=6, angle=60) turtle.save(fn, frame=(200, 200))
def pentaplexity(fn): """By: Paul Bourke""" turtle = Turtle(fn, (50, 50)) turtle.left(90) lsystem = LSystem('F++F++F++F++F', {'F': 'F++F++F|F-F++F'}) turtle.resetpos((80, 0), 0) lsystem.run(turtle, depth=4, step=8, angle=36) turtle.save(fn, frame=(600, 600)) pass
def crystal(fn): turtle = Turtle(fn, (2, 2)) lsystem = LSystem( { 'F': lambda: turtle.forward(2), '+': lambda: turtle.right(90) }, {'F': 'FF+F++F+F'}, 'F+F+F+F') lsystem.run(5) turtle.save(fn, (490, 490))
def paprad(fn): """Attributed to Saupe""" turtle = Turtle(fn, (50, 50)) turtle.left(90) lsystem = LSystem( 'VZFFF', { 'V': '[+++W][---W]YV', 'W': '+X[-W]Z', 'X': '-W[+X]Z', 'Y': 'YZ', 'Z': '[-FFF][+FFF]F' }) turtle.resetpos((200, 400), -90) lsystem.run(turtle, depth=8, step=8, angle=20, thickness='sq') turtle.save(fn, frame=(500, 550))
def cantor(fn, length=300): turtle = Turtle(fn, (20, 10)) lsystem = LSystem( { 'F': lambda: turtle.forward(length), 'f': lambda: turtle.forward(length, usepen=False) }, { 'F': 'FfF', 'f': 'fff' }, 'F') for i in range(7): turtle.resetpos((0, i * 20)) lsystem.run(i) length /= 3 turtle.save(fn, (350, 100))
def square_sierpinsky(fn): angle = 90 length = 3 turtle = Turtle(fn, (10, 100)) turtle.left(90) lsystem = LSystem( { 'F': lambda: turtle.forward(length), 'X': lambda: None, '+': lambda: turtle.right(angle), '-': lambda: turtle.left(angle) }, { 'F': 'F', 'X': 'XF-F+F-XF+F+XF-F+F-X' }, 'F+XF+F+XF') lsystem.run(4) turtle.save(fn, (200, 100))
def triangle(fn): angle = 120 length = 10 turtle = Turtle(fn, (80, 250)) lsystem = LSystem( { 'F': lambda: turtle.forward(length), 'f': lambda: turtle.forward(length, usepen=False), '+': lambda: turtle.right(angle), '-': lambda: turtle.left(angle) }, {'F': 'F-F+F'}, 'F+F+F') lsystem.run(0) turtle.resetpos((50, 0)) lsystem.run(1) turtle.resetpos((100, 0)) lsystem.run(2) turtle.resetpos((160, 0)) lsystem.run(3) turtle.resetpos((280, 0)) lsystem.run(4) turtle.resetpos((400, 0)) lsystem.run(5) turtle.save(frame=(500, 150))
def weed(fn): turtle = Turtle(fn, (10, 500)) turtle.left(90) lsystem = LSystem('X', {'X': 'F+[[X]-X]-F[-FX]+X', 'F': 'FF'}) turtle.resetpos((50, 0), -90) lsystem.run(turtle, 1, 6, 25, thickness='sq') turtle.resetpos((100, 0), -90) lsystem.run(turtle, 2, 6, 25, thickness='sq') turtle.resetpos((150, 0), -90) lsystem.run(turtle, 3, 6, 25, thickness='sq') turtle.resetpos((200, 0), -90) lsystem.run(turtle, 4, 6, 25, thickness='sq') turtle.resetpos((300, 0), -90) lsystem.run(turtle, 5, 6, 25, thickness='sq') turtle.save(fn, frame=(480, 20))