# What is the smallest distance between a line segment and a cylinder?

Given a cylinder at origin represented by its radius and length (no rotation to simplify the problem), what is its minimum distance to a point on a line segment?

So far I managed to create a function that calculates the distance between the cylinder and a point, then used it as optimization function to iterative numerical methods in order to find the minimum distance. Unfortunately such approach is too slow. I'm wondering if there is any way of solving this problem analytically.

You can divide the line into three segments. The line is divided by the two planes of the bottom and top disks of the cylinder. See the image below (1) for example.

Each segment will find the minimum distance independently from each other, and only the minimum distance between all three segments will be chosen. To calculate the minimum distance of each segment, do:

• First line segment (blue line): calculate the minimum distance between this segment and the bottom disk of the cylinder (I'm still using an iterative numerical method here using the distance between a point and a disk as objective function).

• Second line segment (green line): project this line segment and the cylinder to the plane orthogonal to the main axis of the cylinder (as pointed by @Stefano). Find the point on this projected line that is closest to circle created by the cylinder projection.

• Third segment (red line): same as the first segment, but now use the top disk.

Problem with this approach: Imagine you have a line that is really small, almost like a single point, direct below the top disk, inside the cylinder that never crosses to the top side. This approach will calculate the minimum distance as per the Second segment before, (distance circle and line), but its wrong because the minimum distance should be from this line segment to the top disk.

You can project the problem on a plane orthogonal to the cylinder's axis, so that the cylinder becomes a circle and the line becomes a... line. They you are back to the problem of the distance between a circle and a line.

• It can get a bit more complicated when part of the line segment lies "beyond the end" of the cylinder. The nearest point may not be projected to the nearest point of the images of the line and cylinder. Oct 2 '14 at 20:16