Jump to content
  • Advertisement
Sign in to follow this  
Genjix

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

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

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!