Levy Dragon Fractal Curve using Turtle graphics module of Python.
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')
|
great! I love fractals :D
Very nice!
Using the same algorithm we can get some other variants, like von Koch snowflake:
(those first three lines speed up considerably the drawing; the last one keeps the window open)
The Cesaro variant:
And a quadratic Koch island:
http://local.wasp.uwa.edu.au/~pbourke/fractals/
Try this to draw Levy C fractal:
draw_fractal(2, 45, 10, 'F', 'F', '+F--F+', '', '')
I have written many other fractal programs and posted here (w/ source codes): http://myweb.unomaha.edu/~fbahadir/ Those are all Java applets though.
i have added a few fractals and the if character == 'F': needs character.upper() otherwise some may not work