Squaring the Circle
I had the opportunity this week to give a presentation on a technical topic, and for several reasons I chose to discuss taxicab geometry, which has featured on this blog before. Here are my slides and notes. You can walk through the presentation and interact with the geometry yourself here. The math is recreational but ends with a practical lesson about challenging assumptions, making the presentation a kind of technical sermon.
I'm excited to share today, because as I warned the host, while my career has been in software, my degree is in math, and this topic brings together math, algorithms, and even a little bit of philosophy. Also, I couldn't pass up a chance to talk about this topic while in Manhattan.
I was introduced to the idea in college, but I've visited many times since. You might know the term "squaring the circle" from the ancient Greek geometry problem of constructing a square with the same area as a circle, but I'm borrowing it for a different geometry problem, one that only sounds impossible. Namely, can you create a circle that's also a square?
Making a square circle sounds like nonsense, because we know what a circle looks like and we know what a square looks like, but let's examine the actual definitions. A square is a shape with four sides of equal length that have corners with the same size. A circle is all the points a constant distance from another point. There's no inherent contradiction between those definitions. If we're going to make a square circle, we have to change one of the assumptions that these definitions rely on, because we can change assumptions without changing the definition of either shape. Some assumptions in these definitions are what it means for two line segments to have the same length, two angles to have the same measure, and how far apart two points are. We'll change the last one, the measure of distance.
There's more than one way to measure distance. Traditionally we use the Euclidean distance, the shortest path between two points, but we can also calculate distance you'd have to travel in a city with a grid of streets, measuring not as the crow flies but as you'd have to drive or walk. That's the "Manhattan distance", also called the "taxicab distance" or "taxicab metric". While there's only one shortest path in Euclidean distance, there are many route with the shortest distance in taxicab geometry.
Using the Manhattan distance to measure how far one point is from another gives us a circle that's a diamond, that is, a square tipped on one corner. All the points in the circle are one unit away from the center point.
But this change of an assumption has repurcussions, because circles aren't the only shape that relies on the definition of distance. Any graphics platform will give us the ability to draw a square and rotate it 45 degrees, but how do we draw other shapes using a non-standard distance metric?
First, we need signed distance fields. A signed distance field (SDF) is a function that returns a negative value inside the shape, a positive value outside the shape, and zero on the boundary. (You can also have it be positive inside the shape and negative outside, so long as you're consistent.)
Here's a signed distance field for a circle. It takes a point in the plane, the location of the circle's center, and the radius of the circle. The square root calculates the distance between the point and the center. If the radius is larger than that, the whole expression is negative, meaning the point is inside the circle. If the radius is smaller, the expression is positive and the point is outside the circle.
The square root calculates the Euclidean distance, which we can abstract into a dist function so the SDF doesn't have to know the details. Then we can pass in the distance function of our choice. That's turning into alphabet soup, so here's a TypeScript definition.
Here's the value of the signed distance field inside a circle, showing it's negative, as expected.
Now that we have a signed distance field, how do we use it to draw a shape?
The answer is an algorithm called "marching squares". To perform it, we divide the plane into a grid, then get the value of the signed distance field at each point in the grid. That gives us a very rough idea of the shape. Very rough. This circle looks more like a block. To get a better approximation, we take a look at all the cells in the grid and the signs of each of their corners.
There are sixteen combinations. If all of a cell's corners are outside the shape or inside the shape, the boundary of the shape might pass through that cell, but we'll ignore it. If at least one corner is inside the shape and one corner is outside the shape, then the boundary passes in one side of the cell and out another side.
We can put these tiles into the cells in the grid of SDF values, which changes the shape from a block to an octagon. Better, but still a rough approximation. To improve it further, we can use the value of the SDF, not just the sign of the value.
For cell edges that have one corner inside the shape and one corner outside, the point on the edge where the SDF is zero—the boundary of the shape—is more likely to be closer to end that's closer to zero. Here, the zero value is probably closer to -0.1 than to 0.9, so we approximate its distance along the edge by taking the ratio of the SDF's value at that corner to the difference between SDF SDFs don't have to be linear, so this is still an approximation, but it's a much better approximation, at least for our circle.
From there we can shrink the size of the cells to get a better and better approximation of the shape. By the time we have a 24-by-24 grid, the circle is visually indistinguishable from a circle.
I've toyed with a recursive version of this algorithm, where you start with a coarse grid and subdivide only the cells that the boundary crosses, but it's more complicated and in practice not necessary, since simple SDFs are fast enough to calculate and linear interpolation a good enough approximation that getting to the pixel level isn't really necessary.
Now that we've proved SDFs and marching squares can produce the circle we're used to, let's try drawing a new shape with the Manhattan distance.
A parabola is all the points that are the same distance from a focus point and a line, called a directrix. Here's a TypeScript function that calculates the SDF. Note that we have to modify our distance function to find not only the distance between a point and a point but also the distance between a point and a line. That's beyond the scope of this presentation, but in short it involves finding the point on the line that's closest to the point, then using the chosen point-point distance calculation.
Taxicab parabolas are much more angular than Euclidean parabolas, but they do bear a certain resemblance to the U shape we're accustomed to. The angularity is a result of how direction matters in taxicab geometry. We see the significance of the axes coming through.
We can get some visual confirmation that these points on the parabola are the same distance from the focus and directrix by drawing each geometry's respective circle on the parabola and seeing that it passes through the focus and just touches the directrix.
Another shape that depends on distance is the ellipse. An ellipse can be defined as a constant sum of the distances to two foci. In taxicab geometry, that gives us hexagons and octagons.
One other shape is the hyperbola, which can be defined as a constant difference of distances to two foci. That can produce shapes reminiscent of Euclidean hyperbolas, but also shapes that look more like railroad tracks.
If you remember your high school geometry, though, you may remember that parabolas, ellipses, and hyperbolas are all conic sections, meaning you can produce them by slicing through a double cone. A cone in taxicab geometry is a pyramid, because circles are squares. But while you can slide a pyramid to get a taxicab parabola, you can't slice a pyramid to get an octagon, nor a double pyramid to get these railroad tracks that hyperbolas can produce.
Somehow, when we changed the definition of distance, we broke conic sections.
The problem is that there are different definitions of these shapes. We defined ellipses as a constant sum of distances to two foci, but we can also define an ellipse like we did parabolas, as a ratio of the distance to a focus and a directrix. We get a conic ellipse when that ratio is less than one, and a conic hyperbola when the ratio is greater than one.
In Euclidean geometry, those two definitions produce the same kinds of shapes, but not in taxicab geometry.
Taxicab geometry ends up with two different families of ellipses, one that might be called "sum ellipses", and another kind of ellipse that are conic sections.
Same for hyperbolas: we have "difference hyperbolas" and "conic hyperbolas".
An abstraction is when we have two different things that we can treat as if they were the same. We use that all the time in software, such as when we had two different functions for calculating distances and callers didn't have to know which they had. We put classes behind interfaces. We mock web service APIs to mimic the actual services.
Changing the definition of distance broke our geometric abstractions of "ellipse" and "hyperbola", which are names we normally use to refer to two different kinds of shapes but which don't when using the Manhattan distance. This happens with software too. You can hide interactions with, say, S3 behind a file system abstraction, up until the assumptions we hold about file systems break down, such as if S3 goes down or rate limits users. Or maybe two databases have the same SQL language, but that abstraction breaks down when they have different engines for handling embedded regular expressions, and one handles edge cases differently from the other.
When things break, we often have to know not only what abstractions they depend on, but what assumptions those abstractions expected to hold. That's all the more true in the era of LLMs, which don't always tell you the assumptions they're making on your behalf.
But if we do understand the assumptions underlying our abstractions, when an abstraction breaks, we can make some new discoveries.
For example, here's a table of shapes that identifies their formulation in addition to their names, which gives us a richer mental model than just names. It also shows us where there are gaps in this table. Those gaps may be worth exploring.
Some of the missing shapes have indeed been considered before, hundreds and thousands of years ago. Ratios of distances to two foci produce circles. A constant product of distances to two foci produces "ovals of Cassini", among them the lemniscate of Bernoulli, which we use as the symbol for infinity. Taxicab geometry has its own analog, which creates shapes that have something we haven't seen so far under the Manhattan distance: curves.
In taxicab geometry, they produce different circles than the square ones we started with, so taxicab geometry not only has two families of ellipses and hyperbolas, but also two families of circles.
But other shapes in this table don't seem to have any standard names. Maybe someone can get these shapes named after them.
I enjoy thinking about this parallel universe of geometry, but I share all this not because taxicab geometry is somehow better or more useful than Euclidean geometry, but because it provides a kind of object lesson for dealing with conflict. Often we're asked by investors, bosses, or customers to do what seems impossible (Make a square circle!). When that happens, we have to examine our assumptions (What if we changed the measure of distance?). But we can't change assumptions for free; we have to understand the consequences (Weird parabolas, ellipses, and hyperbolas). Exploring those consequences may require using or devising new tools and algorithms (Signed distance fields and marching squeries). And that exploration may expose overlooked distinctions (Sum- and difference-based shapes, in contrast with conic sections). Those distinctions give us new structures of thought (A table of shapes) and suggest fresh avenues for investigation (What if we had a constant sum of the distances to a focus and directrix? What if we used the distance to a circular directrix?).
Thank you for the invitation. Any questions?
Notes
I called the shape that's an equal ratio of distances to two foci a "bisector" because in Euclidean geometry it produces a circle of zero curvature that's identical to the foci's perpendicular bisector. The equivalent taxicab shape is also equidistant from two points, but with a kink in the line, so it's not exactly perpendicular.
On the last slide I mentioned using a circular directrix, which is yet another way to define a hyperbola in Euclidean geometry. Thus, the Table of Shapes can be given a third column, which as far as I can tell is unexplored other than for hyperbolas.
Euclidean and Manhattan distance metrics generalize into the Minkowski distance. Taking the Minkowski distance to infinity produces the Chebyshev distance or "chessboard distance", which also produces square circles, though different from the ones in taxicab geometry.
Claude Code implemented the JavaScript library for the interactive geometry widgets, as well as the presentation framework, which builds static HTML assets from slides I authored in Markdown, with fenced code blocks specifying in YAML the parameters for the UI widgets.