Jason Davies

Ford Circles

Each Ford circle is associated with an irreducible fraction p / q, and is tangent to the horizontal axis and its neighbouring circles.

Note: you can zoom and pan, or click a circle to fly to it:

How it works

For performance, only the circles that are visible are generated, directly from the underlying Farey sequence, so it stays fast at any zoom.

The circle for p/q has radius 1/(2q2), so the smallest circle worth drawing, a pixel or so across, sets a largest denominator Q for the view. The fractions between its edges with denominators up to Q are a stretch of the Farey sequence of order Q. To find the first, it walks down the Stern–Brocot tree towards the left edge, taking as many steps in one direction at once as it can, so that there are only as many as there are terms in the edge’s continued fraction. From there, each fraction comes from the two before it: after a/b and c/d comes (kc − a)/(kd − b), where k = ⌊(Q + b)/d⌋. So the work is in proportion to the circles drawn, about two hundred, however deep the zoom.

A big circle centred just off the screen can still reach into it. By Legendre’s theorem, any that do are convergents of the edge’s continued fraction, which the walk down the tree passes on its way.

The fractions, and the view’s position, are represented exactly, using BigInts, so there’s no limit to how far it can zoom.

Flying to a number follows the path in Jarke van Wijk and Wim Nuij’s Smooth and Efficient Zooming and Panning, which pulls back to see where it’s going. It’s worked out from both ends, so as to keep its precision wherever the view is small.

See also: the Calkin–Wilf tree and the Apollonian gasket.