snowflake.py

Created by schraf

Created on December 13, 2022

978 Bytes

En réponse à un Tweet de @NumWorksFR


from kandinsky import *
from random import random
from math import sin, cos, pi
from time import sleep

N = 20

# On dessine un point blanc ou bleu ciel
def point(p): 
   c = (100, 220, 240) if p[2] % 2 else (255,) * 3
   set_pixel(160 + p[0], 110 + p[1], c)

# rotation de (x,y) de 2*pi*n/N degres
# ce qui donne l'effet kaleidoscope
def rotation(x,y,n):
 a = 2 * pi * n / N
 cs, si = cos(a), sin(a)
 return (int(x * cs - y * si), int(x * si + y * cs), n)

# entier aleatoire entre -2 et 2.3
def alea(): return -2 + 4.3 * random()

def ligne():
 # on part du centre  
 x, y = 0,0
 # tant que l'on est dans cercle rayon 100
 while x * x + y * y < 1e4:
   # deplacement aleatoire de (x,y)
   x += alea()
   y += alea()
   for n in range(N):
     # rotations
     point(rotation(x, y, n))
     # et effet miroir sur les axes
     point(rotation(x, -y, n))

# on genere une infinite de figures
while True:
  fill_rect(0,0,320,222,(0, 55, 105))
  ligne()
  sleep(1)