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)

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.