Jump to content
• Advertisement

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

This topic is 3793 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

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

Share on other sites
aha,

AB is the line segment
C is the closest point
^ is for unit vectors

Share this 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

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

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
13
5. 5
• Advertisement

• 13
• 26
• 10
• 11
• 44
• Forum Statistics

• Total Topics
633744
• Total Posts
3013657
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!