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 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))
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 run(self, fnprovider: AbstractFilenameProvider): fn = fnprovider.get_filename(".svg", "fractals") turtle = Turtle(fn, (50, 250)) turtle.left(90) self.ker(turtle, 9, size=0.2) turtle.resetpos(position=(200, -150)) turtle.right(60) self.sierpinsky(turtle, 5, 9) turtle.resetpos(position=(350, -120)) self.koch(turtle, 5, 0.8) turtle.resetpos(position=(600, -150)) self.hilbert(turtle, 4, 2) turtle.resetpos(position=(900, -200)) self.pentagon(turtle, 3, 10) turtle.resetpos(position=(1200, -200)) self.square_sierpinsky(turtle, 4, 3) turtle.save(frame=(1500, 500)) return fn
def run(self, fnprovider: AbstractFilenameProvider): fn = fnprovider.get_filename(suffix='.svg', name="polygon") turtle = Turtle(fn) self.pentagon(turtle) turtle.resetpos(position=(250, 0)) self.squares(turtle) turtle.resetpos(position=(400, 0)) self.grid(turtle) turtle.resetpos(position=(750, 0)) self.triangles(turtle) turtle.resetpos(position=(900, 50)) self.flower(turtle) turtle.save(frame=(1000, 200)) return fn
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))