How to detect Line segment intersection with Cylinder?

12,872

Solution 1

Try this. Google for the win - the answer is even here on SO. This even has source code and links to more information for you to use. With just a little bit of searching, you could have got this yourself.

This is based on @DuckQueen's answer - it just adds projecting the intersection point - if any - to see if it falls within the height of the cylinder.

Solution 2

Let x = (y-a2)/b2 = (z-a3)/b3 be the equation for line.

Let (x-c1)^2 + (y-c2)^2 = d^2 be the equation for the cylinder.

Substitute x from the line equation into the cylinder equation.

You can solve for y using the quadratic equation. You can have 0 solutions (cylinder and line does not intersect), 1 solution or 2 solutions.

Substitute the value of y into the line equation to get x and z coordinates.

For getting line equation from two points see: http://www.nabla.hr/Z_CGLinesAndPlanesIn3DSpace-A.htm

The solution I provided works for cylinder of infinite height. To restrict to a cylinder of height H: Find the points of intersection, if the points are within the height (i.e. z is within limits), output points.

Share:
12,872

Related videos on Youtube

myWallJSON
Author by

myWallJSON

Updated on September 14, 2022

Comments

  • myWallJSON
    myWallJSON over 1 year

    Say we have a 3d space, Line segment defined by its start and end points (A {Ax, Ay, Az}, B {Bx, By, Bz}) and cylinder defined by its center position C {Cx, Cy, Cz}, radius R and height H. How to get a fact of intersection and if intersection happened than where?

  • DuckQueen
    DuckQueen about 11 years
    Could you please explain where you define cilinder height? Also please use provided points A, B, with some notation alike Ax, By, Cz, etc alike author provided.
  • ElKamina
    ElKamina about 11 years
    @DuckQueen The provided answer finds the points of intersection between a line and a cylinder. It is trivial to convert between different representations of lines and cylinder which I leave to the OP. Please let me know if you have any specific questions.
  • myWallJSON
    myWallJSON about 11 years
    Where is cilinder height in cilinder equation? I have no equation - all I have is an array of points and values. Please provide answer for general case presented in question, not part of it. If you use formulas and line equations please provide how to get tham from given data (do it as wall for cilinder).
  • ElKamina
    ElKamina about 11 years
    @myWallJSON See my explanation.