# 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?
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".
0
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
0

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?
0
Author Commented:
ah, found it.

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

thanks again!
0
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:

0
Author Commented:
make that:

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

:)
0
EngineerCommented:
You can reduce the parentheses

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

:)
0
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
0
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".
0
EngineerCommented:
0
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.
0
EngineerCommented:
"Midpoint of side"  seems perfect
0
EngineerCommented:
Accept a40780458
0
Commented:
Distances to corners in http:#a40780458 is slightly simpler than distances to midpoints,
Both require 8 squaring operations.
But since it only depends on comparing distances, not adding them (like http:#a40711893) , the square root is not needed.
0
Author Commented:
Hi,  as I posted on 2015-04-08,  I requested this question be closed as it was answered sufficiently.
Thanks!
0
EngineerCommented:
As mentioned by the asker, #a40711893 was good enough for him
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.