Beispiel #1
0
def draw_koch_snowflake():
    turtle = Turtle(width=1000, height=1000, center_origin=False, show_borders_and_origin=False, animate=False, animation_speed=0.001)
    axiom = "F--F--F"
    rules = {"F": "F+F--F+F"}
    interpretation = {"F": ("forward", 1), "+": ("right", 60), "-": ("left", 60)}
    depth = 6

    turtle.set_x(150)
    turtle.set_y(300)
    l_system_draw(turtle, "img/koch_snowflake.svg", axiom, rules, interpretation, depth)
Beispiel #2
0
def draw_sierpinski_triange():
    turtle = Turtle(width=1100, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001)
    axiom = "B"
    rules = {"A": "B+A+B", "B": "A-B-A"}
    interpretation = {"A": ("forward", 4), "B": ("forward", 4), "+": ("right", 60), "-": ("left", 60)}
    depth = 8

    turtle.left(180)
    turtle.set_x(500)
    turtle.set_y(-500)
    l_system_draw(turtle, "img/sierpinski_triangle.svg", axiom, rules, interpretation, depth)
Beispiel #3
0
def draw_hilbert_curve():
    turtle = Turtle(width=1000, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001)
    axiom = "-L"
    rules = {"L": "LF+RFR+FL-F-LFLFL-FRFR+", "R": "-LFLF+RFRFR+F+RF-LFL-FR"}
    length = 10
    interpretation = {"F": ("forward", length), "+": ("right", 90), "-": ("left", 90)}
    depth = 4

    turtle.left(90)
    turtle.set_x(400)
    turtle.set_y(-400)
    l_system_draw(turtle, "img/hilbert_curve.svg", axiom, rules, interpretation, depth)
Beispiel #4
0
def draw_fancy_tree_3_stochastic():
    turtle = Turtle(width=800, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001)
    axiom = "F"
    rules = {"F": ["[+F]F[−F]", "F[+F]F", "F[-F]F"]}
    length = 5
    angle = [25, 30, 35]
    interpretation = {"F": ("forward", length), "[": ("stack", "push"), "]": ("stack", "pop"), "+": ("right", angle), "-": ("left", angle)}
    depth = 9

    turtle.left(90)
    turtle.set_y(-500)
    l_system_draw(turtle, "img/fancy_tree_3_stochastic.svg", axiom, rules, interpretation, depth, debug=True)
Beispiel #5
0
def draw_fancy_tree_2():
    turtle = Turtle(width=700, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001)
    axiom = "F"
    rules = {"F": "F[+FF]F[-FF]F"}
    length = 3
    angle = 25.7
    interpretation = {"F": ("forward", length), "[": ("stack", "push"), "]": ("stack", "pop"), "+": ("right", angle), "-": ("left", angle)}
    depth = 5

    turtle.left(90)
    turtle.set_y(-500)
    l_system_draw(turtle, "img/fancy_tree_2.svg", axiom, rules, interpretation, depth)
Beispiel #6
0
def draw_basic_tree():
    turtle = Turtle(width=600, height=600, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001)
    axiom = "A"
    rules = {"A": "F[+A]-A", "F": "FF"}
    length = 1
    angle = 30
    interpretation = {"F": ("forward", length), "[": ("stack", "push"), "]": ("stack", "pop"), "+": ("right", angle), "-": ("left", angle)}
    depth = 9

    turtle.left(90)
    turtle.set_y(-300)
    l_system_draw(turtle, "img/basic_tree.svg", axiom, rules, interpretation, depth)
Beispiel #7
0
def tree():
    """ Draws tree fractal

    TODO:
    - randomize number of branches, angles and lengths
    """
    t = Turtle(width=800,
               height=800,
               center_origin=True,
               show_borders_and_origin=False,
               animate=True,
               animation_speed=0.005)

    t.set_y(-400)
    length = 200
    branch_angle = 20
    branch_scale = 0.7
    steps = 10
    t.left(90)  # starting position
    t.forward(length, width=steps / 2)

    def draw_branch(length, direction, steps):
        if steps == 0:
            return

        if direction == "left":
            t.left(branch_angle)
        else:
            t.right(branch_angle)

        t.forward(length, width=steps / 3)
        draw_branch(length * branch_scale, "left", steps - 1)
        draw_branch(length * branch_scale, "right", steps - 1)

        t.penup()
        t.back(length, width=3)
        if direction == "left":
            t.right(branch_angle)
        else:
            t.left(branch_angle)
        t.pendown()

    draw_branch(length * branch_scale, "left", steps)
    draw_branch(length * branch_scale, "right", steps)

    t.save("img/tree_fractal_animated.svg")