See the full code in action here. I made it so that you can click the image to toggle the interactivity on and off, this way you can pause it on a specific color phase if you want.

Rational functions of the form $f(z)=\frac{az+b}{cz+d}$, called *Mobius transformations*, have surprisingly interesting behavior in the complex plane. I talk a little bit about their surprising connections in my special relativity article. One interesting thing that can be shown about these functions is that they can be represented on the extended complex plane, $\mathbb{C}\cup \{ \infty \}$, by stereographically projecting the plane on to the sphere, rotating and translating the sphere, and then projecting back onto the plane. Note that since I'm talking about the extended complex plane, when I say *circle*, from now on, I mean something that can be either a line or a circle - because lines can be thought of as circles with infinite radius.

A simple example of a Mobius transformation is $z\mapsto \frac{1}{z}$, which is equivalent to $z\mapsto \frac{\bar{z}}{|z|^2}$. Scaling a point by the inverse square of its distance to the origin is an *inversion* of the unit circle. It gets the name inversion because it turns the unit circle inside out, and leaves the boundary unchanged in the same way that $f(x)=\frac{1}{x}$ turns the positive real numbers inside out while leaving $1$ unchanged. The $\bar{z}$ part of this map amounts to a reflection across the real axis. If you know about sterographic projection (which is a very good thing to know about) you can visualize this transformation by imagining the sphere onto which the plane is projected rotating by half a revolution about the real axis. This may be difficult, so here's a video that supplies a good visual intuition for how these transformations work.

Some excellent properties of these transformations, which follow from the fact about stereographic projection, are that they preserve angles between circles and that circles will always be mapped to other circles. It turns out that a Mobius transformation can be specified just by selecting three points, $u_0$, $u_1$, $u_2$ and assigning each of those three points an image, $f(u_0)=v_0$, $f(u_1)=v_1$, $f(u_2)=v_2$. By the previous property, the circle specified by $u_0$, $u_1$, $u_2$ will be mapped to the circle specified by $v_0$, $v_1$, $v_2$, and whether or not there is an inversion depends on whether or not you can map one set of points to the other without using a reflection.

If there are 4 non-overlapping circles, $A$, $B$, $C$, and $D$, as well as Mobius transformations: $\alpha$, taking the exterior of $A$ to the interior of $B$; $\beta$, taking the exterior of $B$ to the interior of $A$; $\gamma$ taking the exterior of $C$ to the interior of $D$; and $\delta$ taking the exterior of $D$ to the interior of $C$; then $\alpha$, $\beta$, $\gamma$, and $\delta$ generate a *Schottky group*, a specific type of *Kleinian group*. A Kleinian group is any discrete subgroup of the group of all Mobius transformations; and a Schottky group is one constructed just like the one here, except you can have any even number of non-overlapping circles.

If we want to find the *limit set* of the Schottky group described above (the set of points which is closed under the action of the group), we would have to look for all of the points which end up in another circle after the point's circle is inverted (This is according to this paper and works specifically for Schottky groups where there is at least a pair of tangent circles). For example, if a point is in $A$, then to check if it is in the limit set is to check that it falls into the interior of $B$, $C$, or $D$ once $A$ is inverted. Iterating this checking process infinitely many times will give you the limit set. For Schottky groups where the circles aren't tangent to each other at all, the limit set is a discrete set of points, which isn't very visually interesting. If all of the circles were tangent to each other, the limit set would be an Apollonian Gasket. The fractal you see here is an example of the iterated inversion system applied to overlapping circles.

This might seem like a lot to take in. Indeed, this is an area of math that I wish I knew more about. That being said, a popular text about Mobius transformations and the novelties that arise from them is Indra's Pearls.