ndonut.py

Created by nonogamer9

Created on September 28, 2024

1.79 KB

An Port Of donut.c For The NumWorks!


# NUM-DONUT! Coded By nonogamer9
# An Port Of Donut.c For The numworks!
import math
import kandinsky as kd
import time

theta_spacing = 0.3
phi_spacing = 0.1
R1, R2 = 1, 2
K2 = 5
screen_width, screen_height = 60, 40
K1 = screen_width * K2 * 3 / (8 * (R1 + R2))

def render_frame(A, B):
    cosA, sinA = math.cos(A), math.sin(A)
    cosB, sinB = math.cos(B), math.sin(B)

    kd.fill_rect(0, 0, screen_width, screen_height, kd.color(0, 0, 0))
    zbuffer = [[0] * screen_height for _ in range(screen_width)]

    for theta in range(int(2 * math.pi / theta_spacing)):
        costheta = math.cos(theta * theta_spacing)
        sintheta = math.sin(theta * theta_spacing)

        for phi in range(int(2 * math.pi / phi_spacing)):
            cosphi = math.cos(phi * phi_spacing)
            sinphi = math.sin(phi * phi_spacing)

            circlex = R2 + R1 * costheta
            circley = R1 * sintheta

            x = circlex * (cosB * cosphi + sinA * sinB * sinphi) - circley * cosA * sinB
            y = circlex * (sinB * cosphi - sinA * cosB * sinphi) + circley * cosA * cosB
            z = K2 + cosA * circlex * sinphi + circley * sinA
            ooz = 1 / z

            xp = int(screen_width / 2 + K1 * ooz * x)
            yp = int(screen_height / 2 - K1 * ooz * y)

            if 0 <= xp < screen_width and 0 <= yp < screen_height:
                L = cosphi * costheta * sinB - cosA * costheta * sinphi - sinA * sintheta + cosB * (cosA * sintheta - costheta * sinA * sinphi)

                if L > 0 and ooz > zbuffer[xp][yp]:
                    zbuffer[xp][yp] = ooz
                    brightness = int(L * 255)
                    kd.set_pixel(xp, yp, kd.color(brightness, brightness, brightness))

A, B = 1.0, 1.0

while True:
    render_frame(A, B)
    A += 0.08
    B += 0.03
    time.sleep(0.1)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.