flocon.py

Created by telpe51

Created on April 15, 2018

878 Bytes

flocon(n) trace le flocon de Koch pour n étapes [n entier positif] dans la couleur définie dans seg().
seg(xa, ya, xb, yb, r, v, b) dessine un segment d’extrémités A(xa, ya) et B(xb, yb) dans la couleur (r, v, b). On peut juste demander seg(xa, ya, xb, yb) : la couleur sera alors magenta par défaut.


from math import *
from kandinsky import *
from cmath import *
def seg(xa,ya,xb,yb,r=250,v=0,b=250):
  col=color(r,v,b)
  if xa==xb :
    if ya<yb:
      y=ya
      bs=yb
    else :
      y=yb
      bs=ya
    while y<=bs:
      set_pixel(int(xa),int(y),col)
      y=y+1
  else:
    m=(yb-ya)/(xb-xa)
    p=ya-m*xa
    if xa<xb:
      x=xa
      bs=xb
    else :
      x=xb
      bs=xa
    while x<=bs:
      y=m*x+p
      set_pixel(int(x),int(y),col)
      x=x+1
    
def motif(za, zb, n):
  if n==0:
      seg(za.real,za.imag,zb.real,zb.imag)
  else:
      zc=za+(zb-za)/3
      zd=za+2*(zb-za)/3
      ze=zc+(zd-zc)*exp(pi/3*1j)
      motif(za, zc, n-1)
      motif(zc, ze, n-1)
      motif(ze, zd, n-1)
      motif(zd, zb, n-1)

def flocon(n):
  z1=50+165*1j
  z2=230+165*1j
  z3=z2+(z1-z2)*exp(pi/3*1j)
  motif(z1, z2, n)
  motif(z2, z3, n)
  motif(z3, z1, n)