def run(self, turtle: Turtle, depth: int, step: float, angle: float = 90, thickness='constant'): s = self.generate_string(depth) stack = [] for i in s: if i == '[': stack.append((turtle.position, turtle.angle)) elif i == ']': turtle.position, turtle.angle = stack.pop() elif i == '+': turtle.right(angle) elif i == '-': turtle.left(angle) elif i == '|': turtle.left(180) elif i.islower(): turtle.forward(step, usepen=False) elif i.isupper(): if thickness == 'constant': thicknessVal = 1 elif thickness == 'sq': thicknessVal = 1.5 / (len(stack) + 1) ** 0.5 else: raise ValueError() turtle.forward(step, usepen=True, thickness=thicknessVal) else: raise ValueError("Incorrect action")
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 kochline(self, turtle: Turtle, depth, size): if depth == 1: turtle.forward(size * 3**depth) return self.kochline(turtle, depth - 1, size) turtle.left(60) self.kochline(turtle, depth - 1, size) turtle.right(120) self.kochline(turtle, depth - 1, size) turtle.left(60) self.kochline(turtle, depth - 1, size)
def hilbert(self, turtle: Turtle, depth, size): turtle.right(90) if depth == 0: turtle.forward(size * 3) turtle.left(90) turtle.forward(size * 3) turtle.left(90) turtle.forward(size * 3) turtle.right(90) else: innersize = self.hs(depth - 1) * size * 3 turtle.forward(innersize, usepen=False) turtle.right(180) self.hilbert(turtle, depth - 1, size) turtle.right(180) turtle.forward(innersize, usepen=False) turtle.forward(size * 3) turtle.left(90) self.hilbert(turtle, depth - 1, size) turtle.forward(size * 3) self.hilbert(turtle, depth - 1, size) turtle.left(90) turtle.forward(size * 3) turtle.forward(innersize, usepen=False) turtle.right(180) self.hilbert(turtle, depth - 1, size) turtle.right(180) turtle.forward(innersize, usepen=False) turtle.right(90)
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 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 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 pentagon(self, turtle: Turtle, depth, size): outerangle = 360 / 5 innerangle = 180 - outerangle if depth == 0: for _ in range(5): turtle.forward(size) turtle.right(outerangle) return for _ in range(5): self.pentagon(turtle, depth - 1, size) turtle.forward(self.pentsize(depth) * size, usepen=False) turtle.right(outerangle) prevsize = self.pentsize(depth - 1) * size turtle.forward(prevsize, usepen=False) turtle.right(outerangle) turtle.forward(prevsize, usepen=False) turtle.right(outerangle - innerangle) self.pentagon(turtle, depth - 1, size) turtle.left(outerangle - innerangle) turtle.back(prevsize, usepen=False) turtle.left(outerangle) turtle.back(prevsize, usepen=False)
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))