

Donut math: how donut.c works (2011) - shrikant
http://www.a1k0n.net/2011/07/20/donut-math.html

======
tptacek
And here's the beginning of a1k0n's MSP430 port:

[https://gist.github.com/a1k0n/8582230](https://gist.github.com/a1k0n/8582230)

:)

------
jheriko
slightly disappointing - the effect is interesting but the implementation and
derivation are sloppy.

using a z buffer is a little unexciting i was expecting something raytracy or
voxely - maybe even scanliney... but thats too much to explain in this
comment.

also using the matrices for deriving the torus equation... its much simpler to
create a rotating basis intuitively with cos and sin then apply them again to
create a circle going around... then rotate the torus as a separate operation
before applying the 3d projection

e.g. start with

    
    
      x = R*cos(t);
      y = R*sin(t);
    

to define the big circle - note that ( cos(t), sin(t), 0 ) is the normal to
that circle and use that to create the circles winding around with a second
parameter:

    
    
      x = r*cos(t)*cos(u) + R*cos(t)
      y = r*sin(t)*cos(u) + R*sin(t)
      z = r*sin(u)
    

set u = n * t and then you get the torus shape with a single parameter where
the spiral around the surface closes up on itself.

the 3d rotation of the whole object can be kept separate and done on a per
axis basis (since using only two angle this is actually less operations as
well)

you end up with the same result but no need to do any particularly challenging
algebra.

~~~
tptacek
If learning more about 3D rendering means I'm going to end up being
disappointed by donut.c, I think I'm going to put off learning more about 3D
rendering.

~~~
NAFV_P
> _If learning more about 3D rendering means I 'm going to end up being
> disappointed by donut.c, I think I'm going to put off learning more about 3D
> rendering._

A topologist: "You can easily screw yourself up in three dimensions, but you
need at least five to unscrew yourself."

------
dpham
I read that article awhile ago, used the character brightness representation
idea to build a JavaScript image/webcam/video to ascii converter
[http://dph.am/jscii/](http://dph.am/jscii/)
[https://github.com/EnotionZ/jscii](https://github.com/EnotionZ/jscii)

------
gurtwo
I've ported it to Python:

[https://gist.github.com/reeesga/8580649](https://gist.github.com/reeesga/8580649)

~~~
cglace
When I run this with pypy it is far too fast to see what is going on.

------
JoeAcchino
How do I compile this? GCC complains:

    
    
        CFLAGS="-g -pipe" make -k donut
        cc -g -pipe    donut.c   -o donut
        donut.c:1:2: warning: data definition has no type or storage class [enabled by default]
        donut.c: In function ‘main’:
        donut.c:4:16: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
        donut.c:5:12: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
        donut.c:9:63: error: ‘s’ undeclared (first use in this function)
        donut.c:9:63: note: each undeclared identifier is reported only once for each function it appears in
        donut.c:10:18: error: expected ‘,’ or ‘;’ before ‘in’
        donut.c:11:32: error: ‘t’ undeclared (first use in this function)
        make: *** [donut] Errore 1
        
        Compilation exited abnormally with code 2 at Thu Jan 23 16:11:12

~~~
dominicgs
I had no problems using: gcc -o donut donut.c -lm

You may also have errors in your version of donut.c, try copying the version
from [http://www.a1k0n.net/2006/09/15/obfuscated-c-
donut.html](http://www.a1k0n.net/2006/09/15/obfuscated-c-donut.html)

------
kevinjohnson
Definitely worth checking out! Some fascinating code and math to create
animated ASCII art.

------
binji
I ported this to Lua as a PNaCl demo:
[https://gonativeclient.appspot.com/demo/lua](https://gonativeclient.appspot.com/demo/lua)

Click the "Donut" button, and press enter.

------
deletes
Replace line _printf( "\x1b[H")_ with you system specific screen clearing
function if you don't have POSIX.

