# Line-cylinder intersection

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. 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. This method is closely related to Line–sphere intersection.

## Cylinder equation, end caps excluded

Let ${\bar {b}}=(b_{x},b_{y},b_{z})$ be the cylinder base (or one endpoint), ${\hat {a}}=(a_{x},a_{y},a_{z})$ be the cylinder axis unit vector, cylinder radius $r$, and height (or axis length) $h$. The cylinder may be in any orientation.
The equation for an infinite cylinder can be written as
    $\lVert {\hat {a}}\times ({\bar {p}}-{\bar {b}})\rVert =r$
where ${\bar {p}}=(x,\ y,\ z)$ is any point on the cylinder surface. The equation simply states that points ${\bar {p}}$ are exactly at Euclidean distance $r$ from the axis ${\hat {a}}$ starting from point ${\bar {b}}$, where $r$ is measured in units of $\lVert {\hat {a}}\rVert$. Note that $\lVert {\hat {a}}\rVert =1$ if ${\hat {a}}$ is a unit vector. 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:
    ${\lVert {\hat {a}}\times ({\bar {p}}-{\bar {b}})\rVert }^{2}=r^{2}$
Point ${\bar {p}}$ is at signed distance
    $t={\hat {a}}\cdot ({\bar {p}}-{\bar {b}})$
from the base along the axis. Therefore, the two equations defining the cylinder, excluding the end caps, is
    ${\lVert {\hat {a}}\times ({\bar {p}}-{\bar {b}})\rVert }^{2}=r^{2}$
    $0\leq {\hat {a}}\cdot ({\bar {p}}-{\bar {b}})\leq h$

## The line

Let ${\bar {p}}={\hat {n}}d$ be a line through origin, ${\hat {n}}$ being the unit vector, and $d$ the distance from origin.
If your line does not pass through origin but point ${\bar {o}}$, i.e. your line is ${\bar {o}}+{\hat {n}}d$, replace ${\bar {b}}$ with $({\bar {b}}-{\bar {o}})$ everywhere; distance $d$ is then the distance from ${\bar {o}}$.

## The intersection problem

The intersection between the line and the cylinder is
    ${\lVert {\hat {a}}\times ({\hat {n}}d-{\bar {b}})\rVert }^{2}=r^{2}$
    $0\leq t\leq h$
where the signed distance along the axis $t$ is
    $t={\hat {a}}\cdot ({\hat {n}}d-{\bar {b}})$

## Solution

Rearranging the first equation gives a quadratic equation for $d$. Solving that for $d$ gives
    $d={\frac {({\hat {n}}\times {\hat {a}})\cdot ({\bar {b}}\times {\hat {a}})\pm {\sqrt {({\hat {n}}\times {\hat {a}})\cdot ({\hat {n}}\times {\hat {a}})r^{2}-({\hat {a}}\cdot {\hat {a}})({\bar {b}}\cdot ({\hat {n}}\times {\hat {a}}))^{2}}}}{({\hat {n}}\times {\hat {a}})\cdot ({\hat {n}}\times {\hat {a}})}}$
where $({\hat {a}}\cdot {\hat {a}})=1$ if ${\hat {a}}$ is a unit vector. If
    $\lVert {\hat {n}}\times {\hat {a}}\rVert =0$
the line is parallel to the axis, and there is no intersection, or the intersection is a line. If
    $({\hat {n}}\times {\hat {a}})\cdot ({\hat {n}}\times {\hat {a}})r^{2}-({\hat {a}}\cdot {\hat {a}})({\bar {b}}\cdot ({\hat {n}}\times {\hat {a}}))^{2}<0$
the line does not intersect the cylinder.
Solving $d$ 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 $t$ from the cylinder base ${\bar {b}}$ along the axis ${\hat {a}}$ to the intersection ${\bar {p}}={\hat {n}}d$ is within zero and the length of the cylinder:
    $0\leq t\leq h$
where $t$ is still
    $t={\hat {a}}\cdot ({\hat {n}}d-{\bar {b}})$

## End caps

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

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$ does not include the end caps. If $H$ is the cylinder height including both hemispherical end caps, then $h=H-2r$.
Check if the line ${\bar {p}}={\hat {n}}d$ intersects either sphere: center ${\bar {c}}={\bar {b}}$ or ${\bar {c}}={\bar {b}}+{\hat {a}}h$ and radius $r$:
    $d={\hat {n}}\cdot {\bar {c}}\pm {\sqrt {({\hat {n}}\cdot {\bar {c}})^{2}+r^{2}-({\bar {c}}\cdot {\bar {c}})}}$
If
    $({\hat {n}}\cdot {\bar {c}})^{2}+r^{2}-({\bar {c}}\cdot {\bar {c}})<0$
the line does not intersect the end cap sphere.
If there are solutions $d$, accept only those that hit the actual end cap hemisphere:
    $-r\leq t<0$ or $h<t\leq h+r$
where, once again,
    $t={\hat {a}}\cdot ({\hat {n}}d-{\bar {b}})$

### Planar end caps

Planar end caps are circular regions, radius $r$, in planes centered at ${\bar {c}}={\bar {b}}$ and ${\bar {c}}={\bar {b}}+{\hat {a}}h$, with unit normal vectors $-{\hat {a}}$ and ${\hat {a}}$, respectively. The line ${\hat {n}}d$ intersects the plane if and only if
    $({\hat {n}}d-{\bar {c}})\cdot {\hat {a}}=0$
Solving d is simple,
    $d={\frac {{\hat {a}}\cdot {\bar {c}}}{{\hat {a}}\cdot {\hat {n}}}}$
Note that if
    ${\hat {a}}\cdot {\hat {n}}=0$
the line is parallel to the end cap plane (and also perpendicular to the cylinder axis). Finally, if and only if
    $({\hat {n}}d-{\bar {c}})\cdot ({\hat {n}}d-{\bar {c}})<r^{2}$
the intersection point ${\hat {n}}d$ is within the actual end cap (the circular region in the plane).

## Unit normal vector at an intersection point

One of the many applications for this algorithm is in ray tracing, where the cylinder unit normal vector ${\hat {v}}$ at the intersection ${\hat {n}}d$ is needed for refracted and reflected rays and lighting.
The equations below use the signed distance $t$ to the intersection point ${\hat {n}}d$ from base ${\bar {b}}$ along the axis ${\hat {a}}$, which is always
    $t={\hat {a}}\cdot ({\hat {n}}d-{\bar {b}})$
For the cylinder surface (excluding the end caps, but including the seam), $0\leq t\leq h$:
    ${\hat {v}}={\frac {{\hat {n}}d-{\hat {a}}t-{\bar {b}}}{\lVert {\hat {n}}d-{\hat {a}}t-{\bar {b}}\rVert }}$
For a spherical end cap at the base, $-r\leq t<0$:
    ${\hat {v}}={\frac {{\hat {n}}d-{\bar {b}}}{r}}$
for a spherical end cap at the other end, $h<t\leq h+r$:
    ${\hat {v}}={\frac {{\hat {n}}d-{\bar {b}}-{\hat {a}}h}{r}}$
For a planar end cap at the base, $t=0$:
    ${\hat {v}}=-{\hat {a}}$
for a planar end cap at the other end, $t=h$:
    ${\hat {v}}={\hat {a}}$