Donut math: how donut.c works (2011)
a1k0n.net
a1k0n.net
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.
But I think I did pretty much exactly what you said, otherwise.
A topologist: "You can easily screw yourself up in three dimensions, but you need at least five to unscrew yourself."
any mathsy code is going to be obfuscated without comments explaining the underlying assumptions and for geometric stuff like this what you really want is diagrams...
And a larger code which means it's bad for an IOCCC.
edit: removed my needless commentary
https://gist.github.com/a1k0n/8582230
:)
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:12You 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
s\
in(...
becomes
sin(...
Click the "Donut" button, and press enter.