# menger.py

Created by schraf

Created on October 13, 2022

1.38 KB

```from kandinsky import fill_rect, set_pixel

fill_rect(0,0,320,222,(200,)*3)

def remplir(coul, *u):
(ax,ay),(bx,by),(cx,cy),(dx,dy)=u[0]
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):
if 0 <= col <= 320 and 0 <= lig <= 222:
xa,xb,xc,xd = ax-col,bx-col,cx-col,dx-col
ya,yb,yc,yd = ay-lig,by-lig,cy-lig,dy-lig
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
if u == v == w == t:
set_pixel(col,222 - lig,coul)

def light(z,g): return ((max(0,min(255,g-4*z)),)*3)

def cube(x, y, z):
tx,ty,tz = 26-x,y,26-z
x2,y2,z2 = x+.3,y,z-1
if not(x//9 == y//9 == 1 or y//9 == z//9 == 1 or x//9 == z//9 == 1) and \
not(x%9//3 == y%9//3 == 1 or y%9//3 == z%9//3 == 1 or x%9//3 == z%9//3 == 1) and \
not(x%3 == y%3 == 1 or y%3 == z%3 == 1 or x%3 == z%3 == 1):
face(x,y,z,((1,0,0),(0,0,-1),(-1,0,0)),light(z,140))
face(x,y,z,((0,-1,0),(0,0,-1),(0,1,0)),light(z,250))
face(x2,y2,z2,((1,0,0),(0,-1,0),(-1,0,0)),light(z,40))

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)

def pos2D(x, y, z):
t = x+z+1*3*y
w = 250/(110+x+z)
return (int(150+(x-z)*3*w), int(15+t*w))

for p in range(26, -1, -1):
for h in range(27):
for z in range(26, -1, -1):
cube(p, h, z)```