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():
  global p,l,rad
  p1=(p[0]+l*cos(rad),
      p[1]+l*sin(rad))
  ligne(p,p1)
  p=p1

def tourne(angle):
  global rad
  rad+=angle*pi/180

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

def dessin(position=(160,120),rang=8,longueur=100,radian=0,couleur=0):
  global p,l,rad,r,c
  rad=radian
  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])

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.