Jump to content
  • Advertisement
IsItSharp

Check whether a Point lies in a line segment

This topic is 498 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,

 

i would like to know how to check as simple as possible if a point lies on a line segment (which is finite). My line has a start (x_s,y_s) and end (x_e,y_e) point and i want to check if point (x_p,y_p) is on the line, how do i do that? The only source code i found does only check if the point is on the line but not on the segment...Any help is very much appreciated.

Share this post


Link to post
Share on other sites
Advertisement
The probability of the point lying on the segment is 0, so any rounding errors would probably ruin your day. In other words, you probably don't want to be testing something like this.

If you can give the segment some thickness, now it's a rectangle, and you can try to find out if the point is inside the rectangle.

Share this post


Link to post
Share on other sites

Thanks for the rectangle-hint! I did it like this and i think it is working now:

        Line.prototype.isOnLine = function (pos) {
            var lowerXBound = Math.min(this.start.x, this.end.x);
            var upperXBound = Math.max(this.start.x, this.end.x);
            var lowerYBound = Math.min(this.start.y, this.end.y);
            var upperYBound = Math.max(this.start.y, this.end.y);
            return pos.x >= lowerXBound && pos.x <= upperXBound && pos.y >= lowerYBound && pos.y <= upperYBound;
        }

Have a nice day!

Share this post


Link to post
Share on other sites

First get the closest point p' on the infinite line going through s and e and let d = |p' - s|. We can calculate d with vector projection: d = ((e - s) · (p - s)) / |(e - s)|

Knowing d, we can easily get p' as follows: p' = s + d * ((e - s) / |(e - s)|)

Now we can check if p lies inside the rectangle as mentioned earlier by Álvaro. If ε < d < |(e - s)| + ε and |p - p'| < ε your point lies inside the rectangle.

Share this post


Link to post
Share on other sites

You have point A(x_s,y_s) and point B(x_e,y_e) and want to check if point C(x_p,y_p) is on the line segment L<x_e - x_s, y_e - y_s>.
You can check if vectors AB and AC are colinear. If their cross-product is a zero vector then they are and C is on L. That would be a fast check. If so now you have to check if it is in the bounds - you do this by using the dot product. If 0 < \(\overrightarrow{AB}\bullet\overrightarrow{AC}\) < \(\overrightarrow{AB}\bullet\overrightarrow{AB}\) then C in on L inbetween A and B

Edited by abominableCodeman

Share this post


Link to post
Share on other sites

  • Advertisement
×

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!