check to see if a point lies on a line _segment_ without sqrt

Started by
3 comments, last by rosewell 15 years, 8 months ago
Hi! So right now to find the closest point on a line segment I project the vectors. Then to see if the point is outside the segment I do,

c = C - A
if (c^ = AB^)
{
  if (|c| < |AB|) => lies inside segment
  else
    return B
}
else
  return A

Is there a way to do it without a sqrt? the one needed to calculate the unit vectors?
Advertisement
Quote:Then to see if the point is outside the segment I do...
When you say 'the point', are you talking about the query point? Or the computed closest point?

In either case, no square roots should be required. If you can clarify what you're trying to do a bit, it should be easy to post some pseudocode showing how to do it without any square roots.
aha,

AB is the line segment
C is the closest point
^ is for unit vectors
Since your point is already on the infinite line overlapping the segment, you only need to check if the point lies within the bounds of your segment endpoints. This can be done easily by using the dot product, which will give a number above zero if one vector points 'in front of' another vector, or a number less than zero if the first vector points 'behind' the second. Thus:

Vector a = s1 - s0;
Vector b = cp - s0;
Vector c = cp - s1;

if dot( b, a ) >= 0 and dot( c, a ) <= 0
point is on segment

Where s0, s1 is the segment and cp is the closest point as you've previously calculated.

Hope this helps
Checking the bounding box against the two segment endpoints seems like the fastest way to me.
Does not require any multiplications, additions, or subtractions. Just conditional checks.

This topic is closed to new replies.

Advertisement