Advertisement
nonogamer9

CA-DONUT: An port of donut.c for the Casio PRIZM!

Sep 5th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | Software | 0 0
  1. # CA-DONUT! Coded By nonogamer9
  2. # An Port Of Donut.c For The Casio PRIZM!
  3. import math
  4. import casioplot as cp
  5.  
  6. theta_spacing = 0.3
  7. phi_spacing = 0.1
  8. R1, R2 = 1, 2
  9. K2 = 5
  10. screen_width, screen_height = 80, 60
  11. K1 = screen_width * K2 * 3 / (8 * (R1 + R2))
  12.  
  13. TWOPI = 2 * math.pi
  14. THETA_STEPS = int(TWOPI / theta_spacing)
  15. PHI_STEPS = int(TWOPI / phi_spacing)
  16. HALF_WIDTH = screen_width // 2
  17. HALF_HEIGHT = screen_height // 2
  18.  
  19. sin_table = [math.sin(i * theta_spacing) for i in range(THETA_STEPS)]
  20. cos_table = [math.cos(i * theta_spacing) for i in range(THETA_STEPS)]
  21.  
  22. def render_frame(A, B):
  23.     cosA, sinA = math.cos(A), math.sin(A)
  24.     cosB, sinB = math.cos(B), math.sin(B)
  25.  
  26.     cp.clear_screen()
  27.     zbuffer = [[0] * screen_height for _ in range(screen_width)]
  28.  
  29.     for theta in range(THETA_STEPS):
  30.         costheta = cos_table[theta]
  31.         sintheta = sin_table[theta]
  32.  
  33.         for phi in range(PHI_STEPS):
  34.             cosphi = math.cos(phi * phi_spacing)
  35.             sinphi = math.sin(phi * phi_spacing)
  36.  
  37.             circlex = R2 + R1 * costheta
  38.             circley = R1 * sintheta
  39.  
  40.             x = circlex * (cosB * cosphi + sinA * sinB * sinphi) - circley * cosA * sinB
  41.             y = circlex * (sinB * cosphi - sinA * cosB * sinphi) + circley * cosA * cosB
  42.             z = K2 + cosA * circlex * sinphi + circley * sinA
  43.             ooz = 1 / z
  44.  
  45.             xp = int(HALF_WIDTH + K1 * ooz * x)
  46.             yp = int(HALF_HEIGHT - K1 * ooz * y)
  47.  
  48.             if 0 <= xp < screen_width and 0 <= yp < screen_height:
  49.                 L = cosphi * costheta * sinB - cosA * costheta * sinphi - sinA * sintheta + cosB * (cosA * sintheta - costheta * sinA * sinphi)
  50.  
  51.                 if L > 0 and ooz > zbuffer[xp][yp]:
  52.                     zbuffer[xp][yp] = ooz
  53.                     brightness = int(L * 255)
  54.                     cp.set_pixel(xp, yp, (brightness, brightness, brightness))
  55.  
  56.     cp.show_screen()
  57.  
  58. A, B = 1.0, 1.0
  59. frame_count = 0
  60.  
  61. while True:
  62.     render_frame(A, B)
  63.     A += 0.08
  64.     B += 0.03
  65.     frame_count += 1
  66.  
  67.     if frame_count % 5 == 0:
  68.         for _ in range(10000):
  69.             pass
  70.  
  71.     if frame_count >= 200:
  72.         break
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement