sapin3d.py

Created by schraf

Created on December 20, 2021

1.58 KB


from turtle import *
from kandinsky import fill_rect,get_pixel,draw_string
from math import cos,sin,exp,pi
from time import sleep
from random import random

speed(0)
hideturtle()

# Produit matrice * vecteur
def prod(m,v):
    r = []
    for l in range(4):
        s = 0
        for c in range(4):
            s += m[l][c] * v[c]
        r.append(s)
    return r

def affiche(t,x,y):
      for i,c in enumerate(t):
            # vert ou jaune
            coul = (0,255,0) if random() < .5 else (255,255,0)
            draw_string(c, x + 10 * i, y, coul, (0,0,0))

# Position de la camera
M = [[1,0,0,0],\
      [0,.28,-.96,0],\
      [0,.96,.28,0],\
      [0,0,0,1]]      

# ecran noir
fill_rect(0,0,320,222,(0,0,0))

for r in range(42,1,-1):
 # rayon des cercles
 d = 4 * exp(r / 10) * sin(r * 3 *  pi / 100)
 # sapin  epais et sol plus fin
 pensize(3 if d > 0 else 2)
 # nb de points par cercle
 p = 36
 for k in range(p):
  # coordonnees x,y sur le cercle
  x, y = d * cos(k * 2 * pi / p), d * sin(k * 2 * pi /p)      
  # w sert pour les translations
  z, w = -110 + 5 * r,  1
  # coordonnees sur ecran en regardant depuis la camera
  [xv,yv,zv,wv] = prod(M,[x,y,z,w])
  # si point est loin alors plus sombre
  c = min(255,220 - zv)  
  v = get_pixel(int(xv+160),int(110 - yv))[0]
  # On evite de mettre un point sombre sur un point plus clair
  if v < c:
   penup()
   goto(xv,yv)
   pendown()
   # sapin si d > 0 sinon sol
   if d > 0: pencolor(0, c, 0)
   else: pencolor(c, c, 0)
   goto(xv, yv)

while True:
      affiche("BONNES",40,40)
      affiche("FETES !",220,100)
      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 cookies policy.