from kandinsky import fill_rect, set_pixel
from math import *
from random import randint
# Fond gris foncé
fill_rect(0,0,320,222,(40,40,40))
# remplissage parallélogramme (begin_fill n'existe pas sur la NUMWORKS)
def remplir(coul, *u):
# On récupère les coordonnées des 4 points
(ax,ay),(bx,by),(cx,cy),(dx,dy)=u[0]
# On parcourt les pixels du rectangle contenant le parallélogramme
for col in range(min(ax,bx,cx,dx), max(ax,bx,cx,dx)+1):
for lig in range(min(ay,by,cy,dy), max(ay,by,cy,dy)+1):
# Si c'est un pixel de l'écran
if 0 <= col <= 320 and 0 <= lig <= 222:
# On teste si le point est dans le parallélogramme
xa,xb,xc,xd = ax-col,bx-col,cx-col,dx-col
ya,yb,yc,yd = ay-lig,by-lig,cy-lig,dy-lig
# xa*yb-ya*xb permet de déterminer de quel côté on est de la droite AB
d1,d2,d3,d4 = xa*yb-ya*xb,xb*yc-yb*xc,xc*yd-yc*xd,xd*ya-yd*xa
u,v,w,t = d1>0,d2>0,d3>0,d4>0
# Si tous identiques on est dans le parallélogramme
if u == v == w == t:
# On met la bordure en noir
co = (0,0,0) if d1*d2*d3*d4 == 0 else coul
set_pixel(col,222 - lig,co)
# Plus z (profondeur) est grand, plus la couleur sera sombre
def light(z,g): return ((max(0,min(255,g-1.1*z)),)*3)
def cube(x, y, z):
tx,ty,tz = 50-x,y,80-z
x2,y2,z2 = x,y,z-1
# Description immeubles et rues
# Testez avec d'autres fonctions
if ty <= int(5+5*sin(tz)*cos(tx/2)) and (x % 10 > 2) and (z % 10 > 3):
# dessus - devant - cote
face(x,y,z,((1,0,0),(0,0,-1),(-1,0,0)),light(z,280))
face(x,y,z,((0,-1,0),(0,0,-1),(0,1,0)),light(z,60))
face(x2,y2,z2,((1,0,0),(0,-1,0),(-1,0,0)),light(z,140))
# Une face
def face(x,y,z,dd,coul):
coord = (pos2D(x,y,z),)
for d in dd:
x,y,z = x+d[0],y+d[1],z+d[2]
coord += (pos2D(x, y, z),)
remplir(coul, coord)
# Coord 3D vers 2D
def pos2D(x, y, z):
t = x+z+2*y
w = 450/(110+x+z)
return (int(140+(x-z)*3*w), int(-70+t*w))
# Les etoiles
for c in range(320):
for _ in range(5):
set_pixel(c
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.