here2share

# tk_Torus_Knot_3_in_7.py

May 8th, 2026 (edited)
178
0
Never
6
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. # tk_Torus_Knot_3_in_7.py
  2.  
  3. import tkinter as tk
  4. import math
  5. import random
  6. import array
  7. import struct
  8.  
  9. WIDTH, HEIGHT = 600, 600
  10. P, Q = 3, 7
  11. STEPS = 600
  12. N = STEPS + 1
  13. LINEWIDTH = 7
  14.  
  15. ANGLE_STEPS = 1440
  16. _step = 2 * math.pi / ANGLE_STEPS
  17.  
  18. x_cache = [(math.cos(i * _step), math.sin(i * _step)) for i in range(ANGLE_STEPS)]
  19. y_cache = x_cache
  20. z_cache = x_cache
  21.  
  22. color_cache = [
  23.     f'#{int(128 * (0.15 + 0.85 * (i / 255))):02x}00{int(200 * (0.15 + 0.85 * (i / 255))):02x}'
  24.     for i in range(256)
  25. ]
  26.  
  27. root = tk.Tk()
  28. root.title("Torus Knot")
  29. root.geometry("+-5+0")
  30. root.resizable(False, False)
  31. canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="black", highlightthickness=0)
  32. canvas.pack()
  33.  
  34. _raw = array.array('d')
  35. for i in range(N):
  36.     t = 2 * math.pi * i / STEPS
  37.     r = math.cos(Q * t) + 2
  38.     _raw.extend((r * math.cos(P * t), r * math.sin(P * t), -math.sin(Q * t)))
  39.  
  40. RAW_FMT = f'{N * 3}d'
  41.  
  42. _px  = array.array('d', bytes(N * 8))
  43. _py  = array.array('d', bytes(N * 8))
  44. _dep = array.array('B', bytes(N))
  45.  
  46. lines = [
  47.     canvas.create_line(0, 0, 1, 1, fill=color_cache[128], width=LINEWIDTH, capstyle=tk.ROUND)
  48.     for _ in range(STEPS)
  49. ]
  50.  
  51. ix = random.randrange(ANGLE_STEPS)
  52. iy = random.randrange(ANGLE_STEPS)
  53. iz = random.randrange(ANGLE_STEPS)
  54.  
  55. _coords  = canvas.coords
  56. _config  = canvas.itemconfig
  57. _after   = root.after
  58. _unpack  = struct.unpack
  59. _randint = random.randint
  60.  
  61. W2    = WIDTH  * 0.5
  62. H2    = HEIGHT * 0.5
  63. FOV   = 600.0
  64. SCALE = 110.0
  65. MASK  = ANGLE_STEPS - 1
  66.  
  67. def rotate():
  68.     return _randint(18, 32)
  69.  
  70. def draw_frame():
  71.     global ix, iy, iz
  72.  
  73.     ix = (ix + rotate()) % ANGLE_STEPS
  74.     iy = (iy + rotate()) % ANGLE_STEPS
  75.     iz = (iz + rotate()) % ANGLE_STEPS
  76.  
  77.     cx, sx = x_cache[ix]
  78.     cy, sy = y_cache[iy]
  79.     cz, sz = z_cache[iz]
  80.  
  81.     m00 = cy*cz;            m01 = cz*sx*sy - cx*sz;  m02 = cx*cz*sy + sx*sz
  82.     m10 = cy*sz;            m11 = cx*cz + sx*sy*sz;  m12 = cx*sy*sz - cz*sx
  83.     m20 = -sy;              m21 = cy*sx;              m22 = cx*cy
  84.  
  85.     pts = _unpack(RAW_FMT, _raw)
  86.  
  87.     px_arr  = _px
  88.     py_arr  = _py
  89.     dep_arr = _dep
  90.  
  91.     j = 0
  92.     for i in range(N):
  93.         x = pts[j];  y = pts[j + 1];  z = pts[j + 2];  j += 3
  94.         rx = m00*x + m01*y + m02*z
  95.         ry = m10*x + m11*y + m12*z
  96.         rz = m20*x + m21*y + m22*z
  97.         f  = FOV / (rz + FOV) * SCALE
  98.         px_arr[i]  = rx * f + W2
  99.         py_arr[i]  = -ry * f + H2
  100.         d = int((rz + 4.0) * 31.875)
  101.         dep_arr[i] = 255 if d > 255 else (0 if d < 0 else d)
  102.  
  103.     ccache = color_cache
  104.     lns    = lines
  105.     for i in range(STEPS):
  106.         i1 = i + 1
  107.         _coords(lns[i], px_arr[i], py_arr[i], px_arr[i1], py_arr[i1])
  108.         _config(lns[i], fill=ccache[(dep_arr[i] + dep_arr[i1]) >> 1])
  109.  
  110.     _after(1, draw_frame)
  111.  
  112. draw_frame()
  113. root.mainloop()
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment