Line-cylinder intersection edit

I'd like to create an article Line-cylinder intersection describing a simple algorithm to calculate the intersection between a line and a right circular cylinder. The algorithm is similar to Line–sphere intersection.

I have not found a complete reference for these equations, only partial ones, although the method is more or less described in various discussion forums. The equations below I solved using Maple. Early testing (in a C program) indicates the algorithms are correct, but I intend to do some further testing.


Cylinder equation, end caps excluded edit

Let   be the cylinder base (or one endpoint),   be the cylinder axis unit vector, cylinder radius  , and height (or axis length)  . The cylinder may be in any orientation.

The equation for an infinite cylinder can be written as

 

where   is any point on the cylinder surface. The equation simply states that points   are exactly at Euclidean distance   from the axis   starting from point  , where   is measured in units of  . Note that   if   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:

 

Point   is at signed distance

 

from the base along the axis. Therefore, the two equations defining the cylinder, excluding the end caps, is

 
 

The line edit

Let   be a line thorough origin,   being the unit vector, and   the distance from origin.

If your line does not pass thorough origin but point  , i.e. your line is  , replace   with   everywhere; distance   is then the distance from  .


The intersection problem edit

The intersection between the line and the cylinder is

 
 

where the signed distance along the axis   is

 


Solution edit

Rearranging the first equation gives a quadratic equation for  . Solving that for   gives

 

where   if   is a unit vector. If

 

the line is parallel to the axis, and there is no intersection, or the intersection is a line. If

 

the line does not intersect the cylinder.


Solving   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   from the cylinder base   along the axis   to the intersection   is within zero and the length of the cylinder:

 

where   is still

 


End caps edit

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 edit

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   does not include the end caps. If   is the cylinder height including both hemispherical end caps, then  .

Check if the line   intersects either sphere: center   or   and radius  :

 

If

 

the line does not intersect the end cap sphere.

If there are solutions  , accept only those that hit the actual end cap hemisphere:

    or    

where, once again,

 


Planar end caps edit

Planar end caps are circular regions, radius  , in planes centered at   and  , with unit normal vectors   and  , respectively. The line   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). Finally, if and only if

 

the intersection point   is within the actual end cap (the circular region in the plane).


Unit normal vector at an intersection point edit

One of the many applications for this algorithm is in ray tracing, where the cylinder unit normal vector   at the intersection   is needed for refracted and reflected rays and lighting.

The equations below use the signed distance   to the intersection point   from base   along the axis  , 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 spherical end cap at the other end,  :

 


For a planar end cap at the base,  :

 

for a planar end cap at the other end,  :

 


Nominal animal (talk) 23:38, 26 June 2010 (UTC)