Python implemention of the popular "Donut.C" program in 2006. The donut behaves like the donut in C, however, now has RGB colouring.
The intricate mathematics driving the donut animation involves trigonometry and parametric equations. Key parameters, including theta (ฮธ), phi (ฯ), and various trigonometric functions, govern the positions of characters on the screen. The donut rotates by incrementing the values of A and B over time.
The parametric equations for a torus (donut) in 3D space define its coordinates based on the angles of rotation (( \theta ) and ( \phi )) and the major and minor radii (( R ) and ( r )):
To simulate the rotation of the torus, rotation matrices are employed. These matrices allow for rotation about the x-axis
The original torus coordinates are transformed using the rotation matrices to obtain the rotated coordinates. The transformed coordinates are then projected onto the 2D screen using perspective projection:
To determine the brightness of each character on the screen, a luminance index is calculated ranging from
- Pygame library
- Install Pygame using
pip install pygame
. - Run the Python script
main.py
for normal performance, anddonut_improved.py
for performance enhanced script.
-
Initialization: The Pygame library is initialized, and necessary variables and constants are defined.
-
Display Setup: The Pygame window is configured with the specified width and height.
-
Text Rendering: ASCII characters are skillfully rendered on the screen using a chosen font and color.
-
Donut Animation: The enchanting donut animation unfolds by iteratively updating character positions based on mathematical equations. Rotation angles A and B are finely adjusted in each iteration.
-
Event Handling: The script adeptly handles Pygame events, enabling users to gracefully exit the program with a key press.
- Press
Esc
to gracefully exit the program inmain.py
anddonut_improved.py
.
Certainly! Let's delve deeper into the "Performance Optimization" section and explore additional details:
Reduced Precision: In the interest of optimizing performance, certain calculations involving trigonometric functions were adjusted to use slightly reduced precision. The precision reduction does not significantly impact the visual quality of the donut animation but contributes to a noticeable improvement in execution speed. By strategically choosing where precision can be sacrificed without compromising the overall aesthetic, the script achieves a good balance between performance and visual fidelity.
Adjusted Step Sizes:
Fine-tuning the values of theta_spacing
and phi_spacing
provides a crucial optimization in terms of computational efficiency and visual output. Smaller step sizes result in a more detailed animation as the torus is rendered with higher precision. However, such fine details may come at the cost of increased computational load. Conversely, larger step sizes contribute to a smoother performance by reducing the number of iterations, albeit with a potential decrease in visual intricacy. The chosen values strike an equilibrium, ensuring an optimal compromise between visual richness and runtime efficiency.
Pixel Step Sizes:
The implementation of pixel_width
and pixel_height
variables offers users the flexibility to customize the size of each rendered pixel. Adjusting these values allows for a fine-tuning of the pixel grid, influencing the level of detail in the output. Smaller pixel sizes result in a more refined depiction of the donut's structure, while larger pixel sizes can expedite the rendering process. This customization empowers users to tailor the script's performance to their preference, aligning the visual experience with computational efficiency.
Overall Impact: The performance optimizations contribute to a more efficient execution of the donut animation script. By carefully selecting precision levels and step sizes, the enhanced script strikes an effective balance between speed and visual quality. Users can enjoy a smooth and responsive animation while having the flexibility to tailor certain parameters for their specific preferences, showcasing the adaptability and optimization efforts incorporated into the code.
- The original donut code was crafted by Andy Sloane in 2006.
- Inspired by https://www.a1k0n.net/2011/07/20/donut-math.html