# 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.

## 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;}

##### 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.

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633311
• Total Posts
3011305
• ### Who's Online (See full list)

There are no registered users currently online

×