Back to Index

Donut-shaped C code that generates a 3D spinning donut


Transcript

Here on the left is a spinning ASCII donut, and on the right is a donut-shaped C code by Andy Sloan that generates it. Now with syntax highlighting. Now I recommend you check out Andy's blog post on the mathematics behind a flying spinning torus, aka donut. The link to the post is in the description.

The basic steps are create a circle, then create a torus by rotating the circle about the Y axis. Then using rotation matrices, spin the donut around the X and Z axes. Finally project the donut onto the 2D screen, adding illumination by calculating the surface normal after picking a particular light source.

The cool thing is because this is ASCII world, there's different characters associated with different levels of luminance. We can go back to the de-obfuscated version of the code that I generated, adding a microsecond sleep function to aid in the animation. Compiling and running the code, we get our spinning donut.

There's a lot of parameters that you can control with this donut, including the field of view and the distance of the donut from the viewer. I spend at least an hour every day learning and exploring outside my main line of work, so I thought it'd be cool to start throwing together quick little videos about things that I find beautiful, whether they're basic or advanced in the world of machine learning, math, computer science, programming, psychology, whatever, even biology, physics, history, and philosophy.

So, hope this is of value, fun, and something you would enjoy. Thanks for watching. Have a great day. Bye. 2 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2