• 9
• 16
• 15
• 12
• 9

Does a point lie within a cylinder?

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

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 on other sites
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;}