Line-cylinder intersection is the calculation of any points of intersection, given an analytic geometry description of a line and a cylinder in 3d space.
[1] Or a line may lie along the surface of a cylinder, parallel to its axis, resulting in infinitely many points of intersection.
The method described here distinguishes between these cases, and when intersections exist, computes their positions.
The term “cylinder” can refer to a three-dimensional solid or, as in this article, only the curved external surface of the solid.
This is why a line piercing a cylinder's volume is considered to have two points of intersection: the surface point where it enters and the one where it leaves.
A key intuition of this sort of intersection problem is to represent each shape as an equation which is true for all points on the shape.
Solving them as a system of two simultaneous equations finds the points which belong to both shapes, which is the intersection.
This method has applications in computational geometry, graphics rendering, shape modeling, physics-based modeling, and related types of computational 3d simulations.
[2][3][4] This method is closely related to Line–sphere intersection.
The equation for an infinite cylinder can be written as where
The equation simply states that points
Because both sides of the equation are always positive or zero, we can square it, and eliminate the square root operation in the Euclidean norm on the left side: Point
Therefore, the two equations defining the cylinder, excluding the end caps, is Let
If your line does not pass through origin but point
The intersection between the line and the cylinder is where the signed distance along the axis
only gives you the distance at which the line intersects the infinite cylinder.
To see if the intersection occurs within the part we consider the actual cylinder, we need to check if the signed distance
is still The above assumes that the cylinder does not have end caps; they must be checked for separately.
The seam where the end cap meets the cylinder is assumed to belong to the cylinder, and is excluded from the end cap.
This object is sometimes called a capsule, or possibly fixed-radius linearly-swept sphere.
is the cylinder height including both hemispherical end caps, then
: If the line does not intersect the end cap sphere.
, accept only those that hit the actual end cap hemisphere: where, once again, Planar end caps are circular regions, radius
intersects the plane if and only if Solving d is simple, Note that if the line is parallel to the end cap plane (and also perpendicular to the cylinder axis).
is within the actual end cap (the circular region in the plane).
One of the many applications for this algorithm is in ray tracing, where the cylinder unit normal vector
is needed for refracted and reflected rays and lighting.
, which is always For the cylinder surface (excluding the end caps, but including the seam),
: For a spherical end cap at the base,
: For a planar end cap at the base,