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

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

## 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 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 on other sites
aha,

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

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