Monthly Archives: May 2021

Equal Spacing Around an Ellipse

My Unity asset Gravity Engine models orbits around planets. These are typically ellipses so gravity engine generates a lot of ellipses. The simplest way to do this is to use an equation for the ellipse in polar coordinates and take equal angle steps in the angle and link the points together. These points are then used by a line renderer to make the ellipse. As the ellipse gets more elongated there are issues at the ends and it starts to look “janky”.

The ideal would be to create points that were equally spaced along the ellipse. How hard could that be?

Pretty hard, actually.

This is one of those interesting math puzzles that gets under my skin. What is especially interesting about this puzzle is that it is not a bunch of messy algebra (in fact the equations end up being quite simple), it’s that the equation that results is an integral that does not have a tidy solution. The integral is common enough that the way this gets handled is that a new function is defined as the solution to this integral. In this case events hinge around finding a solution to the integral:

 E(\phi, k) = \int_0^{\phi} \sqrt{1-k^2 \sin{\theta}^2} d\theta

In fact, just to increase the “fun factor” this equation needs to be solved for a specific length around the ellipse i.e. for a given value of E(\phi, k) = s find the value of phi (which is in the limit of the integral).

I have written up the details of this in a white-paper on this site. This allows me to write up the topic in more detail and to provide a better format for reading the equations than this blog allows. It also spares people from the gory details if they do not want to see them.

I implemented the full-on numerical solution using elliptic functions in the latest version of the Numerical Tools asset. I also show a much simpler heuristic in which the steps in angle are scaled by (r/a)^{2/3}. The comparison of these is shown below.

Exact equal spacing (red squares) versus simple heuristic (blue dots).