Jump to content
  • Advertisement
Sign in to follow this  
PAiNIC

Does a point lie within a cylinder?

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It seems such an easy question, but the only answer google came up with for it I couldn't quite understand. How do I detect if a point P is within the volume of a cylinder with endpoints A and B (all in R^3) and a given radius at each endpoint?

Share this post


Link to post
Share on other sites
Advertisement
Find the closest point Q on the line AB to P. First you check if the closest point on the line is outside the interval [A, B] on the line; if so, it's outside the cylinder. If it's inside the interval, then you check if the distance from Q to P exceeds the radius of the cylinder, it's outside. Otherwise, it's inside the cylinder. In pseudocode:


public bool PointInCylinder(Point A, Point B, double r, Point P)
{
bool result = false;

Line L = new Line(A, B);
Point Q = L.GetClosestPoint(P);

if (Q.IsBetween(A, B) && Distance(P, Q) < r)
{
result = true;
}

return result;
}

Share this post


Link to post
Share on other sites
Say P1 and P2 are the centers of the "caps" and P is the point of interest. Then (P2-P1)/||P2-P1|| is a unit vector in the direction of P1 to P2, call it U, where ||v|| is the magnitude of vector v. So ||U x (P-P1)|| is the distance of P from the axis of rotation, call it D, where x is the vector cross product. Further P1 + (U.(P-P1))*U is the closest point on the axis of rotation to P, call it CP. So if CP lies between P1 and P2 and D is less than the radius of the cylindar then the point lies in the cylindar.

Been awhile so might be a mistake or two, but the general approach is correct. Basically you are just checking that the point lies between two planes and is within a certain distance of a line.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!