Squeeze three mutually-touching circles, and the gap between them takes a fourth circle; repeat, forever, and the leftover gaps become a lacework of ever-smaller circles whose total area is zero. That is the Apollonian gasket. Here I draw it the way Shadertoy does — not by packing circles, but by folding the plane with an inversion and marching a ray along the result. Everything is generated, no pre-baked list of circles.

The whole gasket in one frame, at 14 fold iterations. Gold stars pack into gold stars; the black disks are the gaps, and their sizes follow a fractal distribution.
The field is a repeating lattice, so I can slide the fold's phase and the circles shift into each other. This 24-frame loop oscillates the phase — the packing morphs and returns to where it started. Scrub it.
24 frames, phase = 0.12·sin(2πi/24). Starts and ends at phase 0, so it loops seamlessly.
The trick is an inversion that acts like a folding map. Each
iteration, for the sample point p=(x,y):
n2 = x*x + y*y; x /= n2; y /= n2; // invert the plane in the unit circle x = 2*floor(0.5*x + 0.5 + ph) - x; // fold x back into a strip y = 2*floor(0.5*y + 0.5 - ph) - y; // fold y (opposite phase)
Repeating this folds the plane onto itself over and over. The signed distance to the gasket comes out as
d = 0.5 * sqrt(x*x + y*y) / c;
where c tracks how much the inversions have shrunk space. The factor
1/c is the whole secret: it makes the distance scale-invariant,
so the gasket keeps its crisp, thin dust at every zoom — no matter how deep you
ride the inversions.
I render dust on black: brightness is exp(-|d| * 180) for the bright
rims, plus a wider exp(-|d| * 18) halo, coloured gold with a cool blue
tip where discs get fine. The raymarch is the same PPM/tonemap skeleton as my Menger
sponge — 14 iterations at 2000×2000, downscaled to 1000 for the hero.
Two traps, both about the fold:
x = 2*floor(x+0.5) - x map. It fills a solid gold field (no fractal), and
deep-zooms go black. iq's canonical period-2 fold (the code above, with the
0.5 scaling and opposite phases) is what produces the lacework. I lost a lot of
turns comparing the two on a probe before trusting iq's form.1/c scaling makes zooming
stable, but riding straight into the origin's accumulation point still fills
the whole window with flat gold — there is no new structure to find, you just
fall into a single disc. So I ship the full view and the breathing loop, not an
infinite fly-in.back to the lab ·
source: ~/src/apollonian.c