When I thought of coding tutorial, I know I must a post on donut-shaped C code that generates a 3D spinning donut. It’s just so legendary that you have to know it.

Although I’m good at math in highschool, I haven’t touched math for the last 3 years. In this post, instead of bombing with code, I will help you understand the process and the match behind this masterpiece.
donut-shaped C code that generates a 3D spinning donut
anh’s note from reading Math Behind Donut Shape C Code by whoisslimshady and Donut Math: Make a 3D spinning torus with C by Shreyos Ghosh:
Mathematical Background:

- 3D Projection:
- Projecting 3D coordinates onto a 2D plane by scaling a coordinate by a screen distance constant, denoted as 𝐾1K1.
- Rotation Matrix:
- Rotating a point in 3D space involves matrix multiplications. In this case, rotation around the y-axis is utilized.
Drawing the Torus:

- Torus Construction:
- A torus is formed by revolving a circle of radius 𝑅2R2 around a line generated by a bigger circle of radius 𝑅1R1.
- Point Rotation:
- Each point on the circle with radius 𝑅2R2 is rotated by an angle 𝜙ϕ, and the circle is rotated by an angle 𝜃θ.
- Rotation Matrices:
- Utilizing rotation matrices, points are rotated around the y-axis and additional axes if required.
3D Projection and Illumination:

- Depth Adjustment:
- To move the torus in front of the viewer, a constant 𝐾′K′ is added to the Z-coordinate.
- Projection Formulas:
- The 3D coordinates are projected onto the 2D screen with adjustments based on constants 𝐾1K1 and 𝐾′K′.
- Illuminance Calculation:
- Illuminance is determined by the dot product of the surface normal and the chosen lighting direction.
Coding the Donut:

- Headers Used:
- The necessary libraries like
<stdio.h>
and<math.h>
are included.
- The necessary libraries like
- Variables Initialization:
- Various constants and variables for screen resolution, rotation angles, and buffer size are declared.
- Custom Functions:
render_frame()
calculates pixel positions and illuminance.display()
outputs the rendered frame to the console.
Final Code Execution:
- Loop Execution:
- The code repeatedly generates and displays frames, updating rotation angles for animation.
- ASCII Art Implementation:
- Pixels are represented by ASCII characters based on illuminance levels, producing a donut-like shape.

Understanding the Code:

- Framebuffer Approach:
- The code utilizes a framebuffer and a Z-buffer to render ASCII art of the donut.
- Pixel Plotting:
- Pixels along the torus surface are plotted at fixed-angle increments, simulating a solid shape.
- Projection Simplification:
- 3D projection onto the 2D screen involves scaling coordinates and depth buffering to manage overlapping points.
- Surface Normal Calculation:
- The direction perpendicular to the torus surface at each point is computed for illumination purposes.
- Lighting Direction:
- A lighting direction is chosen to determine the shade of each pixel, affecting its appearance.
any fun codes suggestion?
Same, after writing this post, I didn’t understand a thing. But I hope you have fun feeling cool reading this! I only know donut in coding C, so please lemme know if there is any fun code math out there in the comment section.
Leave a Reply