Several years ago, I was given a practical geometry problem:
Given thousands of measured points in three-dimensional space, find the cylinder that best fits them.
There was an additional requirement that made the problem much more interesting. The calculation had to be fast enough to operate in real time.
This meant that simply defining a mathematically reasonable fitting problem was not enough. The equations had to be arranged so that thousands of data points could be processed efficiently, the derivatives could be evaluated quickly, and each optimization step would require solving only a very small system of equations.
The resulting method combines geometry, calculus, least squares, and the Gauss–Newton method.
The Geometry of a Cylinder
Suppose that
is a point on the axis of a cylinder, and
is a vector pointing along the axis.
Let be a point on the surface of the cylinder.
The defining geometric property is simple:
The perpendicular distance from every point on the cylinder to its axis is the radius .
Distance from a Point to the Axis
Consider the vector
Part of this vector points along the cylinder axis, while the remaining part is perpendicular to the axis.
If is the angle between and , then
Squaring gives
The dot product gives
Combining these relations eliminates the angle completely and gives
This is the equation we need.
The first term measures the squared distance from to . The fraction subtracts the squared component parallel to the axis. What remains is exactly the squared perpendicular distance to the axis.
Turning the Geometry into a Residual
Measured points will not lie exactly on a perfect cylinder.
For a measured point , define
For a point exactly on the cylinder,
For a noisy measured point, the residual will usually be nonzero.
Reducing the Number of Unknowns
At first the cylinder appears to have seven parameters: three coordinates for a point on its axis, three coordinates for the axis direction, and the radius.
But there is redundancy.
Moving the reference point along the axis does not change the cylinder. Also, multiplying by a nonzero constant does not change its direction.
Assuming the cylinder axis is not parallel to the plane, choose
Now the entire cylinder is described by only five unknown numbers:
Two numbers locate the axis, two determine its direction, and one gives the radius.
From Geometry to Least Squares
Suppose our measuring system produces points:
Each point produces a residual .
We find the best-fitting cylinder by minimizing
This is a nonlinear least-squares problem.
Why the Derivatives Matter
Because the calculation had to run quickly, I derived the residual derivatives analytically.
For each measured point, we need derivatives with respect to the five unknown parameters:
The formulas contain several repeated expressions. In an implementation, there is no reason to calculate the same quantity over and over.
For example, define
Then several derivative formulas become much shorter and faster to evaluate.
This may look like a minor algebraic simplification on paper. In a real-time numerical algorithm that evaluates the same expressions thousands of times, such simplifications matter.
The Jacobian
Collect all the residuals into the vector
The Jacobian has one row for every measured point and one column for every unknown parameter.
Therefore,
Gauss–Newton
The least-squares objective can be written compactly as
Its gradient is
Gauss–Newton approximates the Hessian by
At each iteration, instead of solving the original nonlinear problem from scratch, we solve
The vector tells us how to change the current estimate of the cylinder.
Why Thousands of Points Are Not as Bad as They Sound
This is the computational feature that makes the method especially attractive.
Suppose the measuring system gives us 10,000 points.
Then has 10,000 rows.
But it still has only five columns.
Therefore,
is only a
matrix.
And
contains only five numbers.
So although every iteration uses information from thousands of measured points, the linear system that determines the next step has only five unknowns.
That is a very useful structure for a real-time calculation.
Do Not Always Take the Full Step
A Gauss–Newton direction tells us which way to move, but taking the entire step is not always wise.
Write the update as
The step length is chosen adaptively. Start with and reduce it if necessary until the new point produces a sufficient decrease in the objective function.
In practice, the full step can often be accepted, but the line search gives the algorithm additional protection when the current estimate is not yet close to the solution.
When Do We Stop?
At a minimum we expect
Numerically, we stop when
where is a small tolerance.
There is no benefit in demanding far more numerical precision than the data and the computer arithmetic can support. An unnecessarily small tolerance can even create numerical difficulties.
Did It Work?
Yes.
This was not a numerical example invented after the fact. The cylinder fitting problem came from a real application, and the algorithm had to work fast enough for real-time use.
The implementation worked extremely well.
What I found especially satisfying was that the final algorithm was built from familiar mathematical ingredients:
geometry → least squares → calculus → Gauss–Newton → real-time computation.
The important part was arranging those ingredients in the right way.
From Cylinders to Cones
A cylinder has constant radius. Once this fitting method works, a natural question is:
What happens if the radius changes as we move along the axis?
That leads to a cone.
The cone-fitting problem requires one additional parameter—the opening angle—and the derivatives become more complicated. But the central strategy remains the same:
choose an efficient geometric residual → derive its Jacobian → form a nonlinear least-squares problem → solve it with Gauss–Newton.
That will be the subject of the next post.
The Larger Lesson
There is an important distinction between solving a mathematical problem and solving it in a form that is useful in practice.
With 10,000 measured points, the original problem sounds large. But the geometry allows the unknown cylinder to be represented by only five parameters. Gauss–Newton then converts each iteration into a small five-variable linear problem.
This is one of the recurring ideas in applied mathematics:
A good mathematical formulation can be as important as the algorithm itself.
When the formulation is right, a problem involving thousands of three-dimensional measurements can become small enough to solve in real time.

A Closely Related Fitting Problem
Cylinder fitting is only one version of the real-time surface-fitting problem. A closely related challenge arises when the measured surface is a cone rather than a cylinder.
The change in geometry leads to a different mathematical problem, but the objective is the same: use the structure of the surface to obtain an accurate fit quickly enough for practical real-time computation.
Continue exploring: How Do You Fit a Cone to Thousands of Points in Real Time?