# Find a point between 2 points...

Hi! I am using opencv and c++.  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?

The four are corners of a square,  the fifth is somewhere on the edge of the square.

It seems like it would be simple,  but I can't get my head around it.

Thanks!
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EngineerCommented:
Find the slope between the 5th point and all the other points. The slope should be the same for the two points sought. Furthermore the difference between the 5th point and the two points should have opposite signs to lie "within".
EngineerCommented:
Another way is

If the distance P1-P5 and P2-P5 is equal to the distance P1-P2 then the point lies between P1 and P2

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks! So using your second comment, I have it working, BUT, it turns out that the line is not exact. Which is fine.

So, can i add a tolerance to this formula?

if (distance1 + distance2 == distance12)
{
cout << "distance12 wins: " << endl;
}

eg,  if (a 'ALMOST' = b) or, if a is within a certain range of b?
Author Commented:
ah, found it.

if (std::abs(distance1 + distance2 == distance12) < 0.01)
{
}

thanks again!
Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Saqib Husain, Syed's comment #a40711893
Assisted answer: 0 points for chemicalx001's comment #a40712000

for the following reason:

Author Commented:
make that:

if (std::abs((distance1 + distance2) - distance12) < 0.01)
{
}

:)
EngineerCommented:
You can reduce the parentheses

if (std::abs(distance1 + distance2 - distance12) < 0.01)
{
}

:)
Commented:
take the point with minimum x-coordinate as the start point P1. search P2 as next to P1 in x direction.
if P2 == P5 find P2 as next point in x-direction and done.
if not,  set P1 = P2 and search P2 as next to P1 in y-direction.
if P2 == P5 find P2 as next point in y-direction and done.
if not,  set P1 = P2 and search P2 as next to P1 in x-direction.
if P2 == P5 find P2 as next point in x-direction and done.

to handle tolerances you may round the coordinates before.

Sara
Senior Software Engineer (Avast)Commented:
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
EngineerCommented:
Commented:
If you know that the 5th point is on a line between  two adjacent corners of a square, then it suffices to determine
which side it is closest to.
This can be determined from which of the 4 midpoints it is closest to,
Or, which quadrant it is in can be determined from which side of the two diagonals it is on.
which can be determined from which of the two diagonal corners the point is closest to.
EngineerCommented:
"Midpoint of side"  seems perfect
EngineerCommented:
Accept a40780458
Commented:
Distances to corners in http:#a40780458 is slightly simpler than distances to midpoints,