Advertisement
nonogamer9

NUM-DONUT: An port of donut.c for the numworks!

Sep 5th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | Software | 0 0
  1. # NUM-DONUT! Coded By nonogamer9
  2. # An Port Of Donut.c For The numworks!
  3. import math
  4. import kandinsky as kd
  5. import time
  6.  
  7. theta_spacing = 0.3
  8. phi_spacing = 0.1
  9. R1, R2 = 1, 2
  10. K2 = 5
  11. screen_width, screen_height = 80, 60
  12. K1 = screen_width * K2 * 3 / (8 * (R1 + R2))
  13.  
  14. def render_frame(A, B):
  15.     cosA, sinA = math.cos(A), math.sin(A)
  16.     cosB, sinB = math.cos(B), math.sin(B)
  17.  
  18.     kd.fill_rect(0, 0, screen_width, screen_height, kd.color(0, 0, 0))
  19.     zbuffer = [[0] * screen_height for _ in range(screen_width)]
  20.  
  21.     for theta in range(int(2 * math.pi / theta_spacing)):
  22.         costheta = math.cos(theta * theta_spacing)
  23.         sintheta = math.sin(theta * theta_spacing)
  24.  
  25.         for phi in range(int(2 * math.pi / phi_spacing)):
  26.             cosphi = math.cos(phi * phi_spacing)
  27.             sinphi = math.sin(phi * phi_spacing)
  28.  
  29.             circlex = R2 + R1 * costheta
  30.             circley = R1 * sintheta
  31.  
  32.             x = circlex * (cosB * cosphi + sinA * sinB * sinphi) - circley * cosA * sinB
  33.             y = circlex * (sinB * cosphi - sinA * cosB * sinphi) + circley * cosA * cosB
  34.             z = K2 + cosA * circlex * sinphi + circley * sinA
  35.             ooz = 1 / z
  36.  
  37.             xp = int(screen_width / 2 + K1 * ooz * x)
  38.             yp = int(screen_height / 2 - K1 * ooz * y)
  39.  
  40.             if 0 <= xp < screen_width and 0 <= yp < screen_height:
  41.                 L = cosphi * costheta * sinB - cosA * costheta * sinphi - sinA * sintheta + cosB * (cosA * sintheta - costheta * sinA * sinphi)
  42.  
  43.                 if L > 0 and ooz > zbuffer[xp][yp]:
  44.                     zbuffer[xp][yp] = ooz
  45.                     brightness = int(L * 255)
  46.                     kd.set_pixel(xp, yp, kd.color(brightness, brightness, brightness))
  47.  
  48. A, B = 1.0, 1.0
  49.  
  50. while True:
  51.     render_frame(A, B)
  52.     A += 0.08
  53.     B += 0.03
  54.     time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement