# l_turtle_fractals.py

Created by nicolas-patrois

Created on January 19, 2021

1.76 KB

Dessine des approximations de fractales définies par des L-systèmes. Couleurs disponibles : “blue”, “red”, “green”, “yellow”, “brown”, “black”, “white”, “pink”, “orange”, “purple” and “grey”.

```from turtle import *

def remplace(chaine,regles):
return ''.join(regles[caractere] for caractere in chaine)

def dessin(position=(0,0),rang=5,
longueur=100,angle=0,
couleur=(0,0,0),
vitesse=10):
reset()
speed(vitesse)
penup()
color(couleur)
goto(*position)
left(angle)
pendown()

# Courbe du C
#  regles={"F":"+F-F+",
#          "+":"+","-":"-"}
#  red=0.5**.05
#  longueur*=red**rang
#  mouvements={"F":"forward(%f)"%longueur,
#              "+":"left(45)",
#              "-":"right(90)"}
#  chaine="F"

# Courbe de Peano
#  regles={"F":"F-F+F+F+F-F-F-F+F",
#          "+":"+","-":"-"}
#  red=1/3
#  longueur*=red**rang
#  mouvements={"F":"forward(%f)"%longueur,
#              "+":"left(90)",
#              "-":"right(90)"}
#  chaine="F"

# Courbe de Von Koch
#  regles={"A":"A+A-A+A",
#          "+":"+","-":"-"}
#  red=1/3
#  longueur*=red**rang
#  chaine="A"
#  mouvements={"A":"forward(%f)"%longueur,
#              "+":"right(60)",
#              "-":"left(120)"}

# Courbe de Sierpinski
regles={"A":"B-A-B","-":"-",
"B":"A+B+A","+":"+"}
red=0.5
longueur*=red**rang
chaine="A"
mouvements={"A":"forward(%f)"%longueur,
"B":"forward(%f)"%longueur,
"+":"left(60)",
"-":"right(60)"}

# Courbe du dragon
#  regles={"A":"AGB","B":"CGB",
#        "C":"AGD","D":"CGD",
#        "G":"G"}
#  chaine="A"
#  red=0.5**0.5
#  longueur*=red**rang

#  mouvements={"A":"right(90)",
#            "B":"right(90)",
#            "C":"left(90)",
#            "D":"left(90)",
#            "G":"forward(%f)"%longueur}
# forward(longueur) se plaint que longueur est inexistant

for _ in range(rang):
chaine=remplace(chaine,regles)

for mot in chaine:
exec(mouvements[mot])```