Sign in to follow this  
Genjix

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this