# find a point on a line?

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.

c++

edit retag close merge delete

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

( 2015-04-07 15:56:41 -0600 )edit

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

thanks!

( 2015-04-07 16:31:18 -0600 )edit

Sort by ยป oldest newest most voted

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.

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.

( 2015-04-08 03:42:41 -0600 )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.

( 2015-04-08 03:48:46 -0600 )edit

Never(!!!!) compare float/double values: http://www.cygnus-software.com/papers... You have to find the minimum as the exact comparison will (almost) never be true.

( 2015-04-08 04:23:42 -0600 )edit

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

( 2015-04-08 14:58:55 -0600 )edit

Official site

GitHub

Wiki

Documentation