[Math] Line Cylinder Intersection

geometrylinear algebra

I am looking for an efficient way of finding the intersection of a line with a cylinder. Several answers are suggested on this site and other places, however I found this answer the most efficient one. The only challenge I am facing is that the given question and its answer only apply to a case where the line originates from the cylinder axis.
Is there any similar approach that can be generalized to lines starting and ending at any arbitrary position?

To be specific, I am looking for finding intersection of any arbitrary line with a cylinder where the cylinder axis is assumed to lie on one the axes (say Z-axis). The given cylinder doesn't have caps and therefore, the line may or may not intersect with the cylinder. If it does intersect, it may have one or two intersection(s) of course.

Best Answer

If your cylinder is set along an an axis, one way you could think of this is the following:

You "watch" your cylinder with your vision axis colinear with the cylinder axis, you can transform your parameterized line in the same referential as the cylinder then solve for a simple circle-line intersection in 2D.

enter image description here

I'd recommend working with a paremeterized line too, then finding:

  • 0 intersection, you're done.
  • 1 intersection, keep the parameter $t$ you solved for and compute the value in the referential you wish for ( given your line parameterized equation )
  • 2 intersections, keep the 2 parameters $t1$, $t2$ you solved for and compute the values in the referential you wish for for ( given your line parameterized equation )
  • $\infty$ intersections ( the line is contained within the 2d circle ) for that you can check for 2 points really far appart on the line and check if they are both inside the circle.

You are not forced to solve quadratics in order to find this intersection, but you can't avoid some trigonometry, here is an example:

enter image description here

enter image description here