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.
An arbitrary line and cylinder may have no intersection at all. Or there may be one or two points of intersection.[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. See § end caps.
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. The equations below were solved using Maple.
This method has applications in computational geometry, graphics rendering, shape modeling, physics-based modeling, and related types of computational 3d simulations. This has led to various implementations.[2] [3] [4] This method is closely related to Line–sphere intersection.
Let
{\barb}=(bx,by,bz)
{\hata}=(ax,ay,az)
r
h
The equation for an infinite cylinder can be written as
\lVert{\hata} x ({\barp}-{\barb})\rVert=r
{\barp}=(x, y, z)
{\barp}
r
{\hata}
{\barb}
r
\lVert{\hata}\rVert
\lVert{\hata}\rVert=1
{\hata}
{\lVert{\hata} x ({\barp}-{\barb})\rVert}2=r2
{\barp}
t={\hata} ⋅ ({\barp}-{\barb})
{\lVert{\hata} x ({\barp}-{\barb})\rVert}2=r2
0\leq{\hata} ⋅ ({\barp}-{\barb})\leqh
Let
{\barp}={\hatn}d
{\hatn}
d
If your line does not pass through origin but point
{\baro}
{\baro}+{\hatn}d
{\barb}
({\barb}-{\baro})
d
{\baro}
The intersection between the line and the cylinder is
{\lVert{\hata} x ({\hatn}d-{\barb})\rVert}2=r2
0\leqt\leqh
t
t={\hata} ⋅ ({\hatn}d-{\barb})
Rearranging the first equation gives a quadratic equation for
d
d
d=
({\hatn | |
x {\hat |
a}) ⋅ ({\barb} x {\hata})\pm\sqrt{({\hatn} x {\hata}) ⋅ ({\hatn} x {\hata})r2-({\hata} ⋅ {\hata})({\barb} ⋅ ({\hatn} x {\hata}))2}}{({\hatn} x {\hata}) ⋅ ({\hatn} x {\hata})}
({\hata} ⋅ {\hata})=1
{\hata}
\lVert{\hatn} x {\hata}\rVert=0
({\hatn} x {\hata}) ⋅ ({\hatn} x {\hata})r2-({\hata} ⋅ {\hata})({\barb} ⋅ ({\hatn} x {\hata}))2<0
Solving
d
t
{\barb}
{\hata}
{\barp}={\hatn}d
0\let\leh
t
t={\hata} ⋅ ({\hatn}d-{\barb})
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.
Hemispherical end caps are just half-spheres at both ends of the cylinder. This object is sometimes called a capsule,or possibly fixed-radius linearly-swept sphere.
Cylinder height
h
H
h=H-2r
Check if the line
{\barp}={\hatn}d
{\barc}={\barb}
{\barc}={\barb}+{\hata}h
r
d={\hatn} ⋅ {\barc}\pm\sqrt{({\hatn} ⋅ {\barc})2+r2-({\barc} ⋅ {\barc})}
({\hatn} ⋅ {\barc})2+r2-({\barc} ⋅ {\barc})<0
If there are solutions
d
-r\let<0
h<t\leh+r
t={\hata} ⋅ ({\hatn}d-{\barb})
Planar end caps are circular regions, radius
r
{\barc}={\barb}
{\barc}={\barb}+{\hata}h
-{\hata}
{\hata}
{\hatn}d
({\hatn}d-{\barc}) ⋅ {\hata}=0
d=
{\hata | |
⋅ |
{\barc}}{{\hata} ⋅ {\hatn}}
{\hata} ⋅ {\hatn}=0
({\hatn}d-{\barc}) ⋅ ({\hatn}d-{\barc})<r2
{\hatn}d
One of the many applications for this algorithm is in ray tracing, where the cylinder unit normal vector
{\hatv}
{\hatn}d
The equations below use the signed distance
t
{\hatn}d
{\barb}
{\hata}
t={\hata} ⋅ ({\hatn}d-{\barb})
For the cylinder surface (excluding the end caps, but including the seam),
0\let\leh
{\hatv}=
{\hatn | |
d |
-{\hata}t-{\barb}}{\lVert{\hatn}d-{\hata}t-{\barb}\rVert}
For a spherical end cap at the base,
-r\let<0
{\hatv}=
{\hatn | |
d |
-{\barb}}{r}
h<t\leh+r
{\hatv}=
{\hatn | |
d |
-{\barb}-{\hata}h}{r}
For a planar end cap at the base,
t=0
{\hatv}=-{\hata}
t=h
{\hatv}={\hata}