This is a Wikipediauser page. This is not an encyclopedia article or the talk page for an encyclopedia article. If you find this page on any site other than Wikipedia, you are viewing a mirror site. Be aware that the page may be outdated and that the user whom this page is about may have no personal affiliation with any site other than Wikipedia. The original page is located at https://en.wikipedia.org/wiki/User:Nominal_animal.
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.
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
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:
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 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:
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).
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), :