{"id":228,"date":"2024-04-25T23:23:51","date_gmt":"2024-04-25T23:23:51","guid":{"rendered":"https:\/\/croakthecode.tcu.edu\/nguyen\/?p=228"},"modified":"2024-05-06T20:18:26","modified_gmt":"2024-05-06T20:18:26","slug":"how-to-code-donut-shaped-c-code-that-generates-a-3d-spinning-donut","status":"publish","type":"post","link":"https:\/\/croakthecode.tcu.edu\/nguyen\/2024\/04\/25\/how-to-code-donut-shaped-c-code-that-generates-a-3d-spinning-donut\/","title":{"rendered":"how to: code donut-shaped C code that generates a 3D spinning donut"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When I thought of coding tutorial, I know I must a post on donut-shaped C code that generates a 3D spinning donut. It&#8217;s just so legendary that you have to know it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-rounded is-style-rounded--1\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/04\/maxresdefault-1024x576.jpg\" alt=\"\" class=\"wp-image-299\" srcset=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/04\/maxresdefault-1024x576.jpg 1024w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/04\/maxresdefault-300x169.jpg 300w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/04\/maxresdefault-768x432.jpg 768w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/04\/maxresdefault.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Although I&#8217;m good at math in highschool, I haven\u2019t 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">donut-shaped C code that generates a 3D spinning donut<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">anh&#8217;s note from reading <em><strong><a href=\"https:\/\/whoisslimshady.medium.com\/math-behind-donut-shape-c-code-a671ae197e67\" data-type=\"link\" data-id=\"https:\/\/whoisslimshady.medium.com\/math-behind-donut-shape-c-code-a671ae197e67\">Math Behind Donut Shape C Code<\/a><\/strong><\/em> by <strong>whoisslimshady<\/strong> and <strong><em><a href=\"https:\/\/shreyos.hashnode.dev\/donut-math\" data-type=\"link\" data-id=\"https:\/\/shreyos.hashnode.dev\/donut-math\">Donut Math: Make a 3D spinning torus with C<\/a><\/em><\/strong> by <strong>Shreyos Ghosh<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mathematical Background:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-rounded is-style-rounded--2\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"398\" src=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.55-1024x398.png\" alt=\"\" class=\"wp-image-303\" style=\"width:620px;height:auto\" srcset=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.55-1024x398.png 1024w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.55-300x117.png 300w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.55-768x298.png 768w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.55.png 1338w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>3D Projection:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Projecting 3D coordinates onto a 2D plane by scaling a coordinate by a screen distance constant, denoted as \ud835\udc3e1<em>K<\/em>1\u200b.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Rotation Matrix:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Rotating a point in 3D space involves matrix multiplications. In this case, rotation around the y-axis is utilized.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Drawing the Torus:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-rounded is-style-rounded--3\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"506\" src=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.40-1024x506.png\" alt=\"\" class=\"wp-image-304\" srcset=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.40-1024x506.png 1024w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.40-300x148.png 300w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.40-768x379.png 768w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-14.55.40.png 1458w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Torus Construction:<\/strong>\n<ul class=\"wp-block-list\">\n<li>A torus is formed by revolving a circle of radius \ud835\udc452<em>R<\/em>2\u200b around a line generated by a bigger circle of radius \ud835\udc451<em>R<\/em>1\u200b.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Point Rotation:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Each point on the circle with radius \ud835\udc452<em>R<\/em>2\u200b is rotated by an angle \ud835\udf19<em>\u03d5<\/em>, and the circle is rotated by an angle \ud835\udf03<em>\u03b8<\/em>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Rotation Matrices:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Utilizing rotation matrices, points are rotated around the y-axis and additional axes if required.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3D Projection and Illumination:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-rounded is-style-rounded--4\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"472\" src=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-15.04.40-1024x472.png\" alt=\"\" class=\"wp-image-305\" srcset=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-15.04.40-1024x472.png 1024w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-15.04.40-300x138.png 300w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-15.04.40-768x354.png 768w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-15.04.40.png 1114w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Depth Adjustment:<\/strong>\n<ul class=\"wp-block-list\">\n<li>To move the torus in front of the viewer, a constant \ud835\udc3e\u2032<em>K<\/em>\u2032 is added to the Z-coordinate.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Projection Formulas:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The 3D coordinates are projected onto the 2D screen with adjustments based on constants \ud835\udc3e1<em>K<\/em>1\u200b and \ud835\udc3e\u2032<em>K<\/em>\u2032.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Illuminance Calculation:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Illuminance is determined by the dot product of the surface normal and the chosen lighting direction.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Coding the Donut:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-rounded is-style-rounded--5\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"563\" src=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/fahim-mohammed-EumgKI11Hns-unsplash.jpg\" alt=\"\" class=\"wp-image-306\" srcset=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/fahim-mohammed-EumgKI11Hns-unsplash.jpg 1000w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/fahim-mohammed-EumgKI11Hns-unsplash-300x169.jpg 300w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/fahim-mohammed-EumgKI11Hns-unsplash-768x432.jpg 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption class=\"wp-element-caption\">photo for reference and great motivation. credit: Unsplash\/Fahim Mohammed<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Headers Used:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The necessary libraries like <code>&lt;stdio.h&gt;<\/code> and <code>&lt;math.h&gt;<\/code> are included.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Variables Initialization:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Various constants and variables for screen resolution, rotation angles, and buffer size are declared.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Custom Functions:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>render_frame()<\/code> calculates pixel positions and illuminance.<\/li>\n\n\n\n<li><code>display()<\/code> outputs the rendered frame to the console.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Final Code Execution:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Loop Execution:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The code repeatedly generates and displays frames, updating rotation angles for animation.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>ASCII Art Implementation:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Pixels are represented by ASCII characters based on illuminance levels, producing a donut-like shape.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full is-style-rounded is-style-rounded--6\"><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"922\" src=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-15.09.50.png\" alt=\"\" class=\"wp-image-307\" srcset=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-15.09.50.png 676w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/Screenshot-2024-05-06-at-15.09.50-220x300.png 220w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Understanding the Code:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized is-style-rounded is-style-rounded--7\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"220\" src=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/IMG_1291.jpg\" alt=\"\" class=\"wp-image-309\" style=\"width:620px;height:auto\" srcset=\"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/IMG_1291.jpg 402w, https:\/\/croakthecode.tcu.edu\/nguyen\/wp-content\/uploads\/sites\/9\/2024\/05\/IMG_1291-300x164.jpg 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><figcaption class=\"wp-element-caption\">I feel you bro. Didn&#8217;t know what I got myself into.<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Framebuffer Approach:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The code utilizes a framebuffer and a Z-buffer to render ASCII art of the donut.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Pixel Plotting:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Pixels along the torus surface are plotted at fixed-angle increments, simulating a solid shape.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Projection Simplification:<\/strong>\n<ul class=\"wp-block-list\">\n<li>3D projection onto the 2D screen involves scaling coordinates and depth buffering to manage overlapping points.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Surface Normal Calculation:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The direction perpendicular to the torus surface at each point is computed for illumination purposes.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Lighting Direction:<\/strong>\n<ul class=\"wp-block-list\">\n<li>A lighting direction is chosen to determine the shade of each pixel, affecting its appearance.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">any fun codes suggestion?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Same, after writing this post, I didn&#8217;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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I thought of coding tutorial, I know I must a post on donut-shaped C code that generates a 3D spinning donut. It&#8217;s just so legendary that you have to know it. Although I&#8217;m good at math in highschool, I haven\u2019t touched math for the last 3 years. In this post, instead of bombing with [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":300,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorial"],"_links":{"self":[{"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":0,"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/media\/300"}],"wp:attachment":[{"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/croakthecode.tcu.edu\/nguyen\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}