# 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

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

thanks!

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.

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


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.

Official site

GitHub

Wiki

Documentation