random thoughts on programming, math, and more.
complex grids are simpler
in my opinion, advent of code is definitely the most exciting part of december (i’d rather not think about what that says about me). every year i pick a theme to complete the puzzles (previous years have included uiua, k, polyglot, etc.) and last year’s theme was concise python. sounds pretty boring, i know, but i did learn some neat tricks along the way. i used to dread the “annoying grid days”, of which my main complaints were:
…donut.ua: rewriting donut.c in 2 lines of uiua
i’m sure many of you are familiar with the famous donut.c, but for those of you who aren’t, i highly recommend checking it out. following andy’s writeup, i wrote my own implementation in c a few years back. my implementation spanned ~70 lines of code—i think we can do better this time around.
torus math
a torus is defined by its major ($R$) and minor ($r$) radii. $R$ is the distance between the center of the torus and the center of the tube, and $r$ is the radius of the tube.
while donut.c iterates in $\theta$ and $\phi$ steps to plot points lying on the surface of the torus, i’m going to leverage uiua’s voxels renderer by going in the opposite direction. for a given voxel in a 3d linspace, we can decide whether or not that voxel should be rendered by determining if its coordinates lie inside of the torus. to do this, we check if the point is within $r$ of the nearest point along the circle defined by $R$ (shown in black above).