city2.py

Created by mathieu-croslacoste

Created on September 15, 2025

1.25 KB

compressed https://my.numworks.com/python/schraf/city


from kandinsky import fill_rect as F,set_pixel as S;from math import*;from random import randint as R;F(0,0,320,222,(40,)*3)
def rempl(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<321 and 0<=lig<223:
    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;s,v,w,t=d1>0,d2>0,d3>0,d3>0
    if s==v==w==t:co=(0,)*3 if d1*d2*d3*d4==0 else coul;S(col,222-lig,co)
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
 if ty<=int(5+5*sin(tz)*cos(tx/2))and(x%10>2)and(z%10>3):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))
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),)
 rempl(coul,coord)
def pos2D(x,y,z):
 w=450/(110+x+z)
 return(int(140+(x-z)*3*w),int(-70+(x+z+2*y)*w))
for c in range(320):
 for _ in range(5):S(c,R(0,90),(R(50,180),)*3)
for p in range(50,0,-1):
 for h in range(10):
  for z in range(80,0,-1):cube(p,h,z)

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.