donut.ua: rewriting donut.c in 2 lines of uiua

jan 27, 2026

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. torus radii labelled 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).

given our point $p=(x,y,z)$, let $v=(x,y)$ be our xy coordinates. we can find the xy coordinates of the nearest point $q$ on the circle by normalizing $v$ and scaling by $R$. let $u=R\frac{v}{|v|}$, and thus $q=(u_x,u_y,0)$. our point lies inside of the torus iff $|p-q|<r$.

uiua implementation

for consistency, i’m going to use normalized coordinates from -1 to 1 for all math involved. thus, let us start with some constants:

R ← 2/3
r ← 1/3

next, let’s implement a function, In, to determine if a single point lies inside of our torus. in uiua, that looks something like this:

In ← <r⩜⌵-⊸(⊂⊙0×R⩜±↙2)

keeping in mind that uiua runs right-to-left, let’s break down the parenthesized part, which computes $q$:

  • ↙2 says “take two”, the xy components
  • ⩜± says “geometric sign”, which normalizes our vector
  • ×R is straightforward
  • ⊂⊙0 says “join dip 0”, which joins 0 as our z component

to finish it off:

  • -⊸(...) says “subtract by (…)”, which computes $p-q$
  • ⩜⌵ says “geometric absolute value”, which computes $|p-q|$
  • <r is straightforward

using some common idioms, we can generate the coordinates of voxels in a 50x50x50 linspace as follows:

-⊸¬ ⧋÷⟜⇡ ↯3 50

by running ≡₁In, we can apply In to each coordinate vector, leaving us with a 3d bool array to hand to voxels (with some scaling and fog):

# Experimental!
R  ← 2/3
r  ← 1/3
In ← <r⩜⌵-⊸(⊂⊙0×R⩜±↙2)
≡₁In -⊸¬ ⧋÷⟜⇡ ↯3 50
voxels!(°⊸Scale 4 °⊸Fog Black)

running our code, we get this:

almost-working donut looks pretty good, but we we seem to have a weird laser beam through the center of our torus! this occurs when our xy coordinates are $(0,0)$; because the 0 vector has no defined direction, the normalization in our formula for $q$ fails, leaving us with a point that doesn’t lie on the circle. we can easily fix this by shifting our 3d linspace by $\epsilon$, a shift that has no impact on our render other than to eliminate $(0,0)$. much better!

laser-free donut

another issue is that this render was slow. to fix this, we need to think like an array programmer. running In on every vector independently is very expensive—we should instead use pervasive array operations. to do this, we remove ≡₁ and leverage my best friend (evert) to do our bidding. this takes us from 1.6s to 0.04s—a free 40x speedup.

# Experimental!
R  ← 2/3
r  ← 1/3
In ← <r⩜⌵-⊸(⧋⊂⊙0×R⩜±⧋↙2)
In +ε-⊸¬ ⧋÷⟜⇡ ↯3 50
voxels!(°⊸Scale 4 °⊸Fog Black)

finally, let’s jazz this render up a bit while we’re at it. i’ll leave understanding this code as an exercise for the reader, equipped with the excellent uiua docs. you can try out the final program online here.

# Experimental!
R  ← 2/3
r  ← 1/3
In ← <r⩜⌵-⊸(⧋⊂⊙0×R⩜±⧋↙2)
In +ε-⊸¬ ⧋÷⟜⇡ ↯3 50
×τ÷⟜⇡60
≡⌟voxels!(°⊸Scale 4 °⊸Fog Black °⊸Camera ⊟₃⊸∘°∠)

spinning torus

if we inline those constants and remove some line breaks, we’re left with this 2-line beauty (excluding the comment):

# Experimental!
×τ÷⟜⇡60 <1/3⩜⌵-⊸(⧋⊂⊙0×2/3⩜±⧋↙2) +ε-⊸¬⧋÷⟜⇡↯3 50
≡⌟voxels!(°⊸Scale 4 °⊸Fog Black °⊸Camera ⊟₃⊸∘°∠)