Ejemplo n.º 1
0
 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")
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 def run(self, fnprovider: AbstractFilenameProvider):
     fn = fnprovider.get_filename(suffix='.svg', name="shapes")
     turtle = Turtle(fn, (50, 50))
     polygon(turtle, 5, 50)
     turtle.penup()
     turtle.forward(100)
     turtle.pendown()
     star(turtle, 5, 50)
     turtle.penup()
     turtle.forward(100)
     turtle.pendown()
     star(turtle, 7, 50)
     turtle.save(frame=(280, 100))
     return fn
Ejemplo n.º 4
0
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))
Ejemplo n.º 5
0
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))
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
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))
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
Archivo: PartB.py Proyecto: jksdf/IV122
 def flower(self, turtle: Turtle):
     for _ in range(12):
         for _ in range(12):
             turtle.forward(20)
             turtle.right(360 / 12)
         turtle.right(360 / 12)
Ejemplo n.º 10
0
def star(turtle: Turtle, n: int, size: float):
    angle = 180 - 2 * (90 - 360 / n)
    for _ in range(n):
        turtle.forward(size)
        turtle.right(angle)
Ejemplo n.º 11
0
def polygon(turtle: Turtle, n: int, size: float):
    angle = 360 / n
    for _ in range(n):
        turtle.forward(size)
        turtle.right(angle)
Ejemplo n.º 12
0
 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)