Does a point lie within a cylinder?

Started by
1 comment, last by LilBudyWizer 18 years, 8 months ago
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?
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;}
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement