# Donut-shaped C code that generates a 3D spinning donut | Summary and Q&A

8.6M views
July 6, 2020
by
Lex Fridman
Donut-shaped C code that generates a 3D spinning donut

## Summary

This video features a spinning ASCII donut and explains the code behind it that generates the animation. The process involves creating a circle, rotating it around the y-axis to form a torus shape, spinning the donut around the x and z axes using rotation matrices, projecting it onto a 2D screen, and adding illumination by calculating the surface normal. The code can be customized to control parameters such as field of view and distance from the viewer.

### Q: How is the spinning ASCII donut created?

The process involves several steps. First, a circle is created. Then, the circle is rotated around the y-axis to form a torus, or donut shape. Rotation matrices are then used to spin the donut around the x and z axes. Finally, the donut is projected onto a 2D screen, and illumination is added by calculating the surface normal.

### Q: What are the different characters used in the ASCII world for different levels of luminance?

In the ASCII world, various characters are associated with different levels of luminance. For example, a darker character may represent a shaded area on the donut, while a lighter character may represent a highlight. By using different characters in the right places, the donut can appear more realistic and three-dimensional.

### Q: How can the code for the spinning ASCII donut be customized?

The code provides several parameters that can be adjusted. For instance, the field of view can be modified to change how much of the donut is visible to the viewer. The distance between the viewer and the donut can also be adjusted to create different perspectives. By tweaking these parameters, the appearance of the spinning donut can be tailored to specific preferences.

### Q: Can you explain the concept of rotation matrices?

Rotation matrices are mathematical tools used to represent rotations in three-dimensional space. In the case of the spinning ASCII donut, rotation matrices are applied to the donut's vertices to achieve the desired spinning motion. By combining rotation matrices along different axes, the donut can be rotated in various ways and create the illusion of movement.

### Q: How is illumination added to the donut animation?

Illumination is achieved by calculating the surface normal of the donut and considering a particular light source. The surface normal at each point on the donut helps determine how that point should be illuminated. By calculating the intensity of light hitting each point, shadows and highlights can be generated, making the donut animation appear more realistic and visually appealing.

### Q: What is the significance of the microsecond sleep function mentioned in the video?

The microsecond sleep function is added to the code to aid in the animation of the spinning donut. By introducing a small delay between each frame, the animation can be slowed down, allowing viewers to appreciate the spinning motion more easily. This function helps control the timing and smoothness of the animation.

### Q: How can the spinning donut code be compiled and executed?

To compile and run the code for the spinning ASCII donut, one can follow the instructions provided by the video creator. The specific steps may vary depending on the programming environment being used. However, after the code is successfully compiled and executed, the spinning donut animation should be displayed on the screen.

### Q: What are some possible parameters that can be controlled in the spinning donut code?

The spinning donut code offers several parameters that can be adjusted to customize the animation. Some of these parameters include the field of view, which determines how much of the donut is visible; the distance between the viewer and the donut, which affects the perspective; and potentially many more depending on the specific implementation of the code. Experimenting with these parameters can lead to different visual effects and experiences.

### Q: Why did the video creator decide to start making videos about a variety of subjects?

The video creator mentions spending at least an hour every day learning and exploring topics outside of their main line of work. This passion for continuous learning and fascination with various subjects led them to create quick videos about things they find beautiful, whether basic or advanced in fields such as machine learning, math, computer science, programming, psychology, and more. The goal is to provide value, fun, and enjoyable content that others can appreciate and learn from.

### Q: Is there a blog post related to the mathematics behind the spinning donut?

Yes, the video suggests checking out a blog post by Andy Sloan that explains the mathematics behind the flying spinning torus, which is the donut shape created in the video. The link to the blog post is provided in the video's description. Reading the blog post can provide more in-depth information on the mathematical concepts and calculations involved in generating the spinning ASCII donut animation.

## Takeaways

This video showcases the creation and customization of a spinning ASCII donut animation. By following a series of steps, including creating a torus shape, applying rotation matrices, and adding illumination, a realistic and visually appealing animation is achieved. The code for the donut animation can be adjusted using various parameters to control elements such as field of view and distance from the viewer. Additionally, the video creator encourages continuous learning and the exploration of diverse subjects, aiming to share valuable and enjoyable content with others.