Welcome, guest | Sign In | My Account | Store | Cart

Levy Dragon Fractal Curve using Turtle graphics module of Python.

Python, 33 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Levy Dragon Fractal Curve
# FB - 200912093

from turtle import *

def draw_fractal(length, angle, level, initial_state, target, replacement, target2, replacement2):

    state = initial_state
   
    for counter in range(level):
        state2 = ''
        for character in state:
            if character == target:
                state2 += replacement
            elif character == target2:
                state2 += replacement2
            else:
                state2 += character
        state = state2
       
    # draw
    for character in state:
        if character == 'F':
            forward(length)
        elif character == '+':
            right(angle)
        elif character == '-':
            left(angle)

       
if __name__ == '__main__':
   
    draw_fractal(5, 90, 10, 'FX', 'X', 'X+YF+', 'Y', '-FX-Y')

5 comments

manuelaraoz 14 years, 4 months ago  # | flag

great! I love fractals :D

Gabriel Genellina 14 years, 4 months ago  # | flag

Very nice!

Using the same algorithm we can get some other variants, like von Koch snowflake:

delay(0)
speed(0)
hideturtle()
# copo de nieve de von Koch
up(); goto(-180, 60); down();
draw_fractal(1, 60, 5, 'X++X++X', 'X', 'FX-FX++XF-XF', '', '')
exitonclick()

(those first three lines speed up considerably the drawing; the last one keeps the window open)

The Cesaro variant:

# Cesaro
up(); goto(-256, 0); down();
draw_fractal(2, 75, 6, 'FX', 'X', 'X-FX++FX-FX', '', '')

And a quadratic Koch island:

# quadratic Koch island
up(); goto(0, 260); down();
draw_fractal(1, 90, 4, 'X+X+X+X', 'X', 'FX-FX+FX+FXFXFX-FX-FX+FX', '', '')

http://local.wasp.uwa.edu.au/~pbourke/fractals/

FB36 (author) 14 years, 4 months ago  # | flag

Try this to draw Levy C fractal:

draw_fractal(2, 45, 10, 'F', 'F', '+F--F+', '', '')

FB36 (author) 14 years, 2 months ago  # | flag

I have written many other fractal programs and posted here (w/ source codes): http://myweb.unomaha.edu/~fbahadir/ Those are all Java applets though.

NAME 7 years, 2 months ago  # | flag

i have added a few fractals and the if character == 'F': needs character.upper() otherwise some may not work

    for character in state:
        if character.upper() == 'F':
            forward(length)

#draw_fractal(1.9, 90, 10, 'XF', 'X', 'X+YF+', 'Y', '-FX-Y')

#draw_fractal(1, 60, 5, 'X++X++X', 'X', 'FX-FX++XF-XF', '', '')

#draw_fractal(2, 72, 5, 'FX+FX+FX+FX+FX', 'X', 'X-FX++FX-FX', '', '')

#draw_fractal(1, 90, 4, 'X+X+X+X', 'X', 'FX-FX+FX+FXFXFX-FX-FX+FX', '', '')

#draw_fractal(1, 45, 20, 'F', 'F', '+F--F+', '', '')

#draw_fractal(2, 60, 5, 'F', 'F', 'F-f--f+F++FF+f-', 'f', '+F-ff--f-F++F+f')

#draw_fractal(2, 90, 4, 'F', 'F', 'F-F+F+FF-F-F+F', '', '')

#draw_fractal(1, 90, 7, 'F', 'F', 'F-F+F+F-F', '', '')

#draw_fractal(1, 90, 5, 'F-F-F-F', 'F', 'F-F+F+F-F', '', '')

#draw_fractal(1, 90, 5, 'F+F+F+F', 'F', 'F-F+F+F-F', '', '')

#draw_fractal(2, 60, 12, 'XF', 'X', 'YF+XF+Y', 'Y', 'XF-YF-X')

#draw_fractal(5, 120, 2, 'F', 'F', 'F+F-F', '', '')

#draw_fractal(2, 90, 5, 'X', 'X', '-YF+XFX+FY-', 'Y', '+XF-YFY-FX+')

#draw_fractal(2, 90, 5, 'XFX+F+XFX', 'X', '-YF+XFX+FY-', 'Y', '+XF-YFY-FX+')

#draw_fractal(2, 90, 2, 'F+F+F+F', 'F', 'F+F-F-F+F+FF-F+F+FF+F-F-FF+FF-FF+F+F-FF-F-F+FF-F-F+F+F-F', '', '')

#draw_fractal(2, 90, 2, 'F+F+F+F', 'F', 'F-F+F+FFF-FF+FF-F-FFF-FF+FFFF+F+FFF-FF+FFF-F-FFFF-FF+FFF+F+FF-FF+FFF-F-F+F', '', '')

#draw_fractal(2, 90, 3, 'F-F-F-F', 'F', 'FF-F-F-F-F-F+F', '', '')

#draw_fractal(1, 60, 5, 'F-F-F-F-F-F', 'F', 'F-F++F+F-F-F', '', '')

#draw_fractal(5, 72, 3, 'F-F-F-F-F', 'F', 'F-F++F+F-F-F', '', '')

#draw_fractal(2, 90, 6, 'F', 'F', 'F+F-F-F-f+F+F+F-F', 'f', 'fff')

#draw_fractal(5, 160, 5, 'F', 'f', 'F+f+F', 'F', 'f-F-f')

#draw_fractal(5, 160, 5, 'F', 'f', 'F+f+F', 'F', 'f-F-f')

#draw_fractal(2, 120, 12, '-F-f-f', 'F', 'F-f+F+f-F', 'f', 'ff')