texte3d.py

Created by schraf

Created on October 09, 2022

1.67 KB


from kandinsky import fill_rect, set_pixel
from math import *

# Codage du mot 31415
mot=[32767,32767,32767,0,29975,0,0,17684,0,0,30166,0,0,5460,0,0,30039,0]

fill_rect(0,0,320,222,(0,0,0))

# remplissage parallelogramme
def remplir(a, b, c, d, coul):
  (ax,ay) = a
  (bx,by) = b
  (cx,cy) = c
  (dx,dy) = d
  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):
      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 = copysign(1,d1), copysign(1,d2), copysign(1,d3), copysign(1,d4)
      if u == v and v == w and w == t:
       if  d1*d2*d3*d4 == 0: co = (0,0,0)
       else: co = coul
       set_pixel(col,222 - lig,co)

def cube(x, y, z):
  (tx, ty, tz) = (3 - x, y, 15 - z)
  (x2, y2, z2) = (x, y, z - 1)
  # Doit on afficher le cube ?
  if mot[3 * ty + tx] >> tz & 1:
    trait(x, y, z, ((1, 0, 0), (0, 0, -1), (-1, 0, 0)), (150-10*z,)*3)
    trait(x, y, z, ((0, -1, 0), (0, 0, -1), (0, 1, 0)), (255-10*z,)*3)
    trait(x2, y2, z2, ((1, 0, 0), (0, -1, 0), (-1, 0, 0)), (255-10*z,0,0))

# Une face
def trait(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.append(pos2D(x, y, z))
  remplir(coord[0], coord[1], coord[2], coord[3], coul)

# Coord 3D vers 2D
def pos2D(x, y, z):
  t = x + z + y * 2
  w = 300 / (30 + x + z)
  return (int(210 + (x-z) * 2 * w), int(30 + t * w))

# On parcourt tous les cubes
for a in range(3, 0, -1):
  for b in range(6):
    for c in range(15, 0, -1):
      cube(a, b, c)