back to index

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


Whisper Transcript | Transcript Only Page

00:00:00.000 | Here on the left is a spinning ASCII donut, and on the right is a donut-shaped C code
00:00:06.720 | by Andy Sloan that generates it.
00:00:10.080 | Now with syntax highlighting.
00:00:13.780 | Now I recommend you check out Andy's blog post on the mathematics behind a flying spinning
00:00:19.600 | torus, aka donut.
00:00:21.920 | The link to the post is in the description.
00:00:24.220 | The basic steps are create a circle, then create a torus by rotating the circle about
00:00:29.540 | the Y axis.
00:00:31.300 | Then using rotation matrices, spin the donut around the X and Z axes.
00:00:37.800 | Finally project the donut onto the 2D screen, adding illumination by calculating the surface
00:00:42.960 | normal after picking a particular light source.
00:00:46.820 | The cool thing is because this is ASCII world, there's different characters associated with
00:00:50.660 | different levels of luminance.
00:00:52.820 | We can go back to the de-obfuscated version of the code that I generated, adding a microsecond
00:00:59.080 | sleep function to aid in the animation.
00:01:02.680 | Compiling and running the code, we get our spinning donut.
00:01:08.880 | There's a lot of parameters that you can control with this donut, including the field of view
00:01:13.320 | and the distance of the donut from the viewer.
00:01:17.080 | I spend at least an hour every day learning and exploring outside my main line of work,
00:01:22.320 | so I thought it'd be cool to start throwing together quick little videos about things
00:01:27.600 | that I find beautiful, whether they're basic or advanced in the world of machine learning,
00:01:32.840 | math, computer science, programming, psychology, whatever, even biology, physics, history,
00:01:37.440 | and philosophy.
00:01:38.440 | So, hope this is of value, fun, and something you would enjoy.
00:01:43.840 | Thanks for watching.
00:01:44.840 | Have a great day.
00:01:45.840 | [end]