How the recursive structure of Apollonian gaskets can be described in order to be able to reproduce them?

I wrote a Mathematica function to generate such gaskets, and the way it navigates the net of circles is based on a function that returns the indices of the $3$ predecessors for the circle with index $n$:

Pred[n_Integer] := If[n < 6, {1, 2, 3}, Module[{q = Quotient[n, 3] + 2, p}, p = Pred[q]; Append[ Switch[Mod[n, 3], 0, p[[{1, 2}]], 1, p[[{1, 3}]], 2, p[[{2, 3}]]], q]]]

The circles $1$-$5$ are easy to generate explicitly from the first $3$ curvatures, $a\lt0$ and $b,c\gt0$:

$d=a+b+c-2\,\mathrm{Disc}(a,b,c)$
$e=a+b+c+2\,\mathrm{Disc}(a,b,c)$

$\text{circle }1:\left(\left(-\frac1a,0\right),\frac1a\right)$
$\text{circle }2:\left(\left(\frac1b,0\right),\frac1b\right)$
$\text{circle }3:\left(\left(\frac{b-a}{(a+b)c},\frac{2\,\mathrm{Disc}(a,b,c)}{(a+b)c}\right),\frac1c\right)$
$\text{circle }4:\left(\left(\frac{b-a}{(a+b)d},-\frac{2\,\mathrm{Disc}(a,b,d)}{(a+b)d}\right),\frac1d\right)$
$\text{circle }5:\left(\left(\frac{b-a}{(a+b)e},\frac{2\,\mathrm{Disc}(a,b,e)}{(a+b)e}\right),\frac1e\right)$

where $\mathrm{Disc}(a,b,c)=\sqrt{ab+bc+ca}$.

Then circles $\ge6$ can be computed using Pred[n] and a function that takes $3$ circles and returns the smaller circle touching all $3$:

NextCircle[a_Circle, b_Circle, c_Circle] := Module[{wa, ka = Curv[a], wb, kb = Curv[b], wc, kc = Curv[c], kd}, kd = ka + kb + kc + 2 Disc[ka, kb, kc]; wa = ka Disc[kb, kc, kd]; wb = kb Disc[kc, kd, ka]; wc = kc Disc[kd, ka, kb]; Circle[(wa Cent[a] + wb Cent[b] + wc Cent[c])/(wa + wb + wc), 1/kd]]

where Curv[c] returns the reciprocal of the radius of $c$ and Cent[c] returns the center of $c$.

Here is the result for $(a,b,c)=(-9,14,26)$:

enter image description here


Start off with the triple of circles given by the outer circle of curvature $1$ and the two circles with curvature $2$. In each step, find the interior tangent circle to a triple and generate three new triples, each containing the new circle and a pair of circles from the previous triple. To draw the circles in order of increasing curvature, immediately calculate the curvature for each triple as you generate it and keep them in a data structure sorted by curvature so that you can always process the smallest curvature remaining.

In the first step you'll have the two symmetric circles of curvature $3$ instead of an interior and an exterior one. You can save half the calculations by arbitrarily choosing one of these and then for each circle drawing both the circle and its vertical mirror image.