Ask Your Question

find a point on a line?

asked 2015-04-07 15:06:48 -0500

antithing gravatar image

Hi! I have 5 points, stored in a vector<point2f>, that i know the coordinates of. The 5th point is on a line between 2 of the others. What is the best way to find out which two? And which one of the two it is closest to?

Many thanks.


edit retag flag offensive close merge delete


Do all of the 5 points fall on the same line?

Potato gravatar imagePotato ( 2015-04-07 15:56:41 -0500 )edit

no, four of them are corners of a square, and the other is somewhere along the edge of the square.


antithing gravatar imageantithing ( 2015-04-07 16:31:18 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-04-08 02:50:18 -0500

one way (of at least hundreds of possibilities): compute the distance to all corner points and the distance between these points. Select the pair of border points where d(C_i,P)+d(C_j,P) - d(C_i,C_j) is minimal.

edit flag offensive delete link more


sorted. Thanks! i am using this function:

    float dist(Point p1, Point p2)
       return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));

then looping through the results.

antithing gravatar imageantithing ( 2015-04-08 03:42:41 -0500 )edit

..alongside of this: (psuedo)

     if (distance(A, C) + distance(B, C) == distance(A, B))
        return true; // C is on the line.
        return false;    // C is not on the line.
antithing gravatar imageantithing ( 2015-04-08 03:48:46 -0500 )edit

Never(!!!!) compare float/double values: You have to find the minimum as the exact comparison will (almost) never be true.

FooBar gravatar imageFooBar ( 2015-04-08 04:23:42 -0500 )edit

ah i see. Consider it noted. :) Thanks!

antithing gravatar imageantithing ( 2015-04-08 14:58:55 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-04-07 15:06:48 -0500

Seen: 130 times

Last updated: Apr 08 '15