{"id":20207720,"url":"https://github.com/malikmaky/spinning-donut","last_synced_at":"2026-02-15T14:36:18.092Z","repository":{"id":223165936,"uuid":"759477420","full_name":"malikmaky/spinning-donut","owner":"malikmaky","description":"This repository dives into the creation and the logic of implementing a rotating/spinning torus or doughnut.","archived":false,"fork":false,"pushed_at":"2024-07-08T11:42:34.000Z","size":426,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-10T09:13:40.295Z","etag":null,"topics":["3d2d","bitmap-graphics","csharp","donut","graphics-programming","math","mathmatics","rotation-matrix","spin","spinning-don"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/malikmaky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-18T17:43:54.000Z","updated_at":"2024-07-08T11:42:37.000Z","dependencies_parsed_at":"2024-02-18T18:43:47.429Z","dependency_job_id":"a706d9e6-aac8-439f-a298-6dc091bfa840","html_url":"https://github.com/malikmaky/spinning-donut","commit_stats":null,"previous_names":["malikmaky/spinning-donut"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/malikmaky/spinning-donut","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malikmaky%2Fspinning-donut","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malikmaky%2Fspinning-donut/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malikmaky%2Fspinning-donut/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malikmaky%2Fspinning-donut/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/malikmaky","download_url":"https://codeload.github.com/malikmaky/spinning-donut/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malikmaky%2Fspinning-donut/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29481525,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-15T11:35:25.641Z","status":"ssl_error","status_checked_at":"2026-02-15T11:34:57.128Z","response_time":118,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["3d2d","bitmap-graphics","csharp","donut","graphics-programming","math","mathmatics","rotation-matrix","spin","spinning-don"],"created_at":"2024-11-14T05:31:37.278Z","updated_at":"2026-02-15T14:36:18.071Z","avatar_url":"https://github.com/malikmaky.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n![Logo](https://images2.imgbox.com/c6/8b/yUbLlonJ_o.jpg)\n\n\n\n# Exploring a Spinning Doughnut[![AGPL License](https://img.shields.io/badge/CSharp-Project-purple)](https://dotnet.microsoft.com/en-us/languages/csharp) \n    \n\n\n\n## Donut Math\nThis report dives into the creation of a rotating/spinning torus or doughnut.\n\nThe implementation contains fundamental mathematical concepts and techniques to generate an animated display of a three-dimensional object projected onto a two-dimensional surface, Through the manipulation of angles and coordinates, coupled with graphical rendering within a Windows Forms application.\n\n\n\n\n\n\n\n## Explanation Of Doughnut Math\n\nThe spinning torus, also known as a \"doughnut”, is a shape formed by rotating a circle in three-dimensional space.\n\nMathematically, it is defined by two radiuses, R1 and R2,\nwhich determine the size and position of the torus.\n\n![App Screenshot](https://images2.imgbox.com/be/e8/eNeYudOB_o.png)\n\nThe torus is then projected onto a two-dimensional plane located a certain distance away from the viewer, resulting in a flattened representation that mimics the perception of depth.\n\n## Projecting A 3D Object Onto A 2D Screen\n\n(x,y,z) is the object points in the 3D-Space, In order to project a 3D object onto a 2D screen,we imagine an imaginary flat surface positioned at\na specific distance (denoted as z') from the viewer.\n\n![App Screenshot](https://images2.imgbox.com/fc/d0/6w31dxJq_o.png)\n\nThis surface (z') acts as a reference onto which the 3D points, represented as (x, y, z), are projected, through this projection we determine their corresponding positions  (x', y') on the 2D screen.\n\n##### __Note :__ \nIn the Since we’re looking from the side, we can only see the y and z axes, but the math works the same for the x axis.\n\nWe can notice that the origin, the y-axis, and the point (x, y, z) create a right triangle,\nand a similar right triangle is also formed with the points (x', y', z').\nSo we can use the Triangle Similarity Theorem and find out that relative proportions are maintained.\n\n![App Screenshot](https://images2.imgbox.com/10/db/94m54JO4_o.png)\n\n##### For example :\nif we want to see an object which is 10 units wide in our 3D space,\nset back 5 units from the viewer, then *K1* should be chosen so that the projection of\nthe point x=10, z=5 is still on the screen with\n*x’* \u003c 50 ==\u003e 10*K1*/5 \u003c 50 ==\u003e *K1* \u003c 25.\n\n- To make cost-effective computations, we use the inverse of the z-coordinate \n    denoted as  *z^(-1)=1/z* by precomputing *z^(-1)*, we can use it when computing the  actual (x', y') positions.\n\n- Dividing once by z and then multiplying by z-1 twice is cheaper than dividing by z twice, this optimization helps in speeding up the rendering process.\n\n## Drawing The Torus\n\nCircle of radius __R1__ centered at point __(R2,0,0)__, θ — from 0 to 2π:\n\n![App Screenshot](https://images2.imgbox.com/2f/eb/IoV8Lu63_o.png)\n\nNow we take that circle and rotate it around the y-axis by another angle, to rotate a 3D point around an axe the standard technique is to multiply by a rotation matrix.\n\n![App Screenshot](https://images2.imgbox.com/ea/f4/00u2IE9S_o.png)\n\nWe also want the whole donut to spin around on at least two more axes for the animation.\n\nRotation about the x-axis by __A__ and a rotation about the z-axis by __B__.\n\n![App Screenshot](https://images2.imgbox.com/2a/63/xuMj4XOj_o.png)\n\nWorking through the above gets us an (x,y,z) point on the surface of our torus,\nrotated around two axes.\n\nTo actually get screen coordinates, we need to move the torus somewhere in front of the viewer (the viewer is at the origin) — so we just add some constant to z to move it backward.\n\nSo we have another constant to pick, call it __K2__, for the distance of the donut from the viewer, and our projection now looks like:\n \n![App Screenshot](https://images2.imgbox.com/06/99/cmpP0GMr_o.png)\n\n- K1 and K2 can be tweaked together to change the field of view and flatten or exaggerate the depth of the object.\n\n## Implementation\n\n- We use a bitmap to draw/represent the rotating donut points. \n- Mathematical calculations determine each point's position and color on the donut's surface, which is projected onto the 2D bitmap.\n- Double buffering ensures smooth animation by drawing frames onto a hidden buffer before displaying them seamlessly, this process creates a visually engaging spinning donut animation, showcasing the intersection of mathematics and computer graphics.\n\n## References\n\nDonut Math :\nhttps://www.a1k0n.net/2011/07/20/donut-math.html\t\n\nRotation Matrix :\nhttps://en.wikipedia.org/wiki/Rotation_matrix\t\n\n## Demo\n\nCheck out the demo through this youtube video down bellow\nhttps://www.youtube.com/watch?v=40IfGnu9R9Q\n\n## Credits\n\nThis project is maintained by @malikmaky.\n\nFor any inquiries or feedback, please contact malikmhmd@hotmail.com\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalikmaky%2Fspinning-donut","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmalikmaky%2Fspinning-donut","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalikmaky%2Fspinning-donut/lists"}