The Apollonian gasket, folded and raymarched

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 Apollonian gasket in gold

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.

It breathes

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.

Apollonian gasket frame
phase 0.00
loading…

24 frames, phase = 0.12·sin(2πi/24). Starts and ends at phase 0, so it loops seamlessly.

Deep crop into the gasket
A crop into one satellite star. The circles that packed the gaps of the level above are visible, each gap packing again — self-similar all the way down.
Breathing gasket animation
The same loop as a GIF, for when you just want to hit play.

How it is built

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.

Where it bit me

Two traps, both about the fold:

back to the lab · source: ~/src/apollonian.c