Last active
July 22, 2020 13:17
-
-
Save jonaprieto/effdc299acb391721842 to your computer and use it in GitHub Desktop.
Use python to generate nice and famous fractals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import turtle | |
# -- Settings | |
sys.setrecursionlimit(100000) | |
wn = turtle.Screen() | |
wn.bgcolor('black') | |
cursor = turtle.Turtle() | |
cursor.color('white') | |
cursor.pensize(2) | |
cursor.speed(90000000) | |
cursor.pila = [] | |
def fixPosition(): | |
cursor.pila.append(list(cursor.position()) + [cursor.heading()]) | |
def backToPosition(): | |
if not len(cursor.pila) is 0: | |
toPosition(cursor.pila[-1][0], cursor.pila[-1][1]) | |
cursor.setheading(cursor.pila[-1][2]) | |
cursor.pila.pop() | |
return | |
def toPosition(x, y): | |
cursor.up() | |
cursor.goto(x, y) | |
cursor.down() | |
return | |
fractal = { | |
'koch': { | |
'constants': { | |
'F': 'cursor.forward(5)', | |
'+': 'cursor.left(90)', | |
'-': 'cursor.right(90)' | |
}, | |
'initialState': 'F', | |
'rules': { | |
'F': 'F+F-F-F+F' | |
}, | |
'steps': 4 | |
}, | |
'koch2': { | |
'constants': { | |
'F': 'cursor.forward(5)', | |
'+': 'cursor.left(90)', | |
'-': 'cursor.right(90)' | |
}, | |
'initialState': 'F+F+F+F', | |
'rules': { | |
'F': 'F+F-F-FF+F+F-F' | |
}, | |
'steps': 3 | |
}, | |
'cesaro': { | |
'constants': { | |
'F': 'cursor.forward(7)', | |
'+': 'cursor.right(85)', | |
'-': 'cursor.left(85)' | |
}, | |
'initialState': 'F', | |
'rules': { | |
'F': 'F-F++F-F' | |
}, | |
'steps': 6 | |
}, | |
'sierpinski': { | |
'constants': { | |
'A': 'cursor.forward(4)', | |
'B': 'cursor.forward(4)', | |
'+': 'cursor.left(60)', | |
'-': 'cursor.right(60)' | |
}, | |
'initialState': 'A', | |
'rules': { | |
'A': 'B-A-B', | |
'B': 'A+B+A' | |
}, | |
'steps': 6 | |
}, | |
'sierpinski2': { | |
'constants': { | |
'F': 'cursor.forward(7)', | |
'G': 'cursor.forward(7)', | |
'+': 'cursor.right(120)', | |
'-': 'cursor.left(120)' | |
}, | |
'initialState': 'F-G-G', | |
'rules': { | |
'F': 'F-G+F+G-F', | |
'G': 'GG' | |
}, | |
'steps': 5 | |
}, | |
'dragon': { | |
'constants': { | |
'X': 'cursor.color("white")', | |
'Y': 'cursor.color("white")', | |
'F': 'cursor.forward(5)', | |
'+': 'cursor.right(90)', | |
'-': 'cursor.left(90)' | |
}, | |
'initialState': 'FX', | |
'rules': { | |
'X': 'X+YF', | |
'Y': 'FX-Y' | |
}, | |
'steps': 15 | |
}, | |
'plant': { | |
'constants': { | |
'F': 'cursor.forward(5)', | |
'X': 'cursor.color("white")', | |
'+': 'cursor.right(25)', | |
'-': 'cursor.left(25)', | |
'[': 'fixPosition()', | |
']': 'backToPosition()' | |
}, | |
'initialState': 'X', | |
'rules': { | |
'X': 'F-[[X]+X]+F[+FX]-X', | |
'F': 'FF' | |
}, | |
'steps': 6 | |
}, | |
'plant2': { | |
'constants': { | |
'F': 'cursor.forward(2)', | |
'X': 'cursor.color("white")', | |
'+': 'cursor.right(25.7)', | |
'-': 'cursor.left(25.7)', | |
'[': 'fixPosition()', | |
']': 'backToPosition()' | |
}, | |
'initialState': 'X', | |
'rules': { | |
'X': 'F[+X][-X]FX', | |
'F': 'FF' | |
}, | |
'steps': 5 | |
}, | |
} | |
def chainGeneration(fractalName='koch', s='', step=0): | |
if s is '': | |
return chainGeneration(fractalName, | |
fractal[fractalName]['initialState'], | |
step - 1) | |
if step >= 0: | |
rules = fractal[fractalName]['rules'] | |
L = [rules[c] if c in rules else c for c in s] | |
L = ''.join(L) | |
return chainGeneration(fractalName, L, step - 1) | |
return s | |
def paintChain(fractalName='koch', s=''): | |
print "steps: ", len(s) | |
for c in s: | |
eval(fractal[fractalName]["constants"][c]) | |
return | |
drawFractal = 'koch' | |
toPosition(0, 0) | |
cursor.left(90) | |
for i in range(1): | |
c = chainGeneration( | |
fractalName = drawFractal, | |
step = fractal[drawFractal]['steps'] | |
) | |
paintChain(fractalName=drawFractal, s=c) | |
toPosition(-50 * i, -50 * i) | |
print "Done", i | |
turtle.exitonclick() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment