# l_system_fractals.py

Created by nicolas-patrois

Created on January 20, 2021

2.21 KB

Trace des fractales définies comme L-systèmes.

```import kandinsky
from math import cos,sin,pi

def ligne(p1,p2):
global c
# algorithme de Bresenham
x1,y1=p1
x2,y2=p2
dx=x2-x1
dy=y2-y1
pentu=abs(dy)>abs(dx)
if pentu:
x1,y1=y1,x1
x2,y2=y2,x2
if x1>x2:
x1,x2=x2,x1
y1,y2=y2,y1
dx=x2-x1
dy=y2-y1
erreur=int(dx/2.0)
pasy=1 if y1<y2 else -1
y=y1
for x in range(x1,x2+1):
coord=(y,x) if pentu else (x,y)
kandinsky.set_pixel(int(coord[0]),int(coord[1]),(c,c,c))
erreur-=abs(dy)
if erreur<0:
y+=pasy
erreur+=dx

def avance():
ligne(p,p1)
p=p1

def tourne(angle):

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

r=rang
l=longueur
p=position
c=couleur

# Courbe du C
#  regles={"F":"+F-F+",
#          "+":"+","-":"-"}
#  red=0.5**.5
#  l*=red**rang
#  mouvements={"F":"avance()",
#              "+":"tourne(45)",
#              "-":"tourne(-90)"}
#  chaine="F"

# Courbe de Peano
#  regles={"F":"F-F+F+F+F-F-F-F+F",
#          "+":"+","-":"-"}
#  red=1/3
#  l*=red**rang
#  mouvements={"F":"avance()",
#              "+":"tourne(90)",
#              "-":"tourne(-90)"}
#  chaine="F"

# Courbe de Von Koch
#  regles={"A":"A+A-A+A",
#          "+":"+","-":"-"}
#  red=1/3
#  l*=red**rang
#  chaine="A"
#  mouvements={"A":"avance()",
#              "+":"tourne(-60)",
#              "-":"tourne(120)"}

# Courbe de Sierpinski
#  regles={"A":"B-A-B","-":"-",
#          "B":"A+B+A","+":"+"}
#  red=0.5
#  l*=red**rang
#  mouvements={"A":"avance()",
#              "B":"avance()",
#              "+":"tourne(60)",
#              "-":"tourne(-60)"}
#  chaine="A"

# Courbe du dragon
regles={"A":"AGB","B":"CGB",
"C":"AGD","D":"CGD",
"G":"G"}
chaine="A"
red=0.5**0.5
mouvements={"A":"tourne(-90)",
"B":"tourne(-90)",
"C":"tourne(90)",
"D":"tourne(90)",
"G":"avance()"}
l*=red**rang

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

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