here2share

# tk_3d_panels.py

Oct 7th, 2026 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. # tk_3d_panels.py
  2.  
  3. import tkinter as tk
  4. import math
  5.  
  6. W = 640
  7. s = 64
  8. fov = 800
  9. r = 0
  10.  
  11. root = tk.Tk()
  12. root.title("# tk_3d_panels.py")
  13. root.geometry("+0+0")
  14. canvas = tk.Canvas(root, width=W, height=W, bg='#881308')
  15. canvas.pack()
  16.  
  17. gap = s - 8
  18. hs = gap / 2
  19. hz = 0.5
  20. verts = [
  21.     (-hs, -hs, -hz), (hs, -hs, -hz), (hs, hs, -hz), (-hs, hs, -hz),
  22.     (-hs, -hs, hz), (hs, -hs, hz), (hs, hs, hz), (-hs, hs, hz)
  23. ]
  24.  
  25. faces = [
  26.     [4,5,6,7],
  27.     [1,0,3,2],
  28.     [0,4,7,3],
  29.     [5,1,2,6],
  30.     [0,1,5,4],
  31.     [3,7,6,2]
  32. ]
  33.  
  34. def rotateY(x, y, z, a):
  35.     c = math.cos(a)
  36.     s = math.sin(a)
  37.     return (x*c + z*s, y, -x*s + z*c)
  38.  
  39. def rotateX(x, y, z, a):
  40.     c = math.cos(a)
  41.     s = math.sin(a)
  42.     return (x, y*c - z*s, y*s + z*c)
  43.  
  44. def update():
  45.     global r
  46.     canvas.delete("all")
  47.     all_faces = []
  48.     for i in range(121):
  49.         tx = (i % 11) * s - W/2
  50.         ty = (i // 11) * s - W/2
  51.         angleY = i/22 + r
  52.         angleX = math.sin(r/10)/2
  53.         transformed = []
  54.         for vx, vy, vz in verts:
  55.             x1, y1, z1 = rotateY(vx, vy, vz, angleY)
  56.             x2, y2, z2 = rotateX(x1, y1, z1, angleX)
  57.             x2 += tx
  58.             y2 += ty
  59.             transformed.append((x2, y2, z2))
  60.         projected = []
  61.         for x, y, z in transformed:
  62.             scale = fov / (fov - z)
  63.             sx = W/2 + x * scale
  64.             sy = W/2 + y * scale
  65.             projected.append((sx, sy))
  66.         color = '#000000' if i % 2 == 0 else '#ffffff'
  67.         for face in faces:
  68.             v0 = transformed[face[0]]
  69.             v1 = transformed[face[1]]
  70.             v2 = transformed[face[2]]
  71.             ax = v1[0] - v0[0]
  72.             ay = v1[1] - v0[1]
  73.             az = v1[2] - v0[2]
  74.             bx = v2[0] - v0[0]
  75.             by = v2[1] - v0[1]
  76.             bz = v2[2] - v0[2]
  77.             nx = ay*bz - az*by
  78.             ny = az*bx - ax*bz
  79.             nz = ax*by - ay*bx
  80.             cx = sum(transformed[idx][0] for idx in face) / 4
  81.             cy = sum(transformed[idx][1] for idx in face) / 4
  82.             cz = sum(transformed[idx][2] for idx in face) / 4
  83.             if nx*(0-cx) + ny*(0-cy) + nz*(fov-cz) > 0:
  84.                 z_avg = sum(transformed[idx][2] for idx in face) / 4
  85.                 points = []
  86.                 for idx in face:
  87.                     points.extend(projected[idx])
  88.                 all_faces.append((z_avg, points, color))
  89.     all_faces.sort(key=lambda x: x[0])
  90.     for _, points, color in all_faces:
  91.         canvas.create_polygon(points, fill=color)
  92.     r += 0.25
  93.     root.after(1, update)
  94.  
  95. update()
  96. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment