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!
chemicalx001Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Saqib Husain, SyedEngineerCommented:
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".
Saqib Husain, SyedEngineerCommented:
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

Your issues matter to us.

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

Start your 7-day free trial
chemicalx001Author 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?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

chemicalx001Author Commented:
ah, found it.

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

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

I answered my own additional question
chemicalx001Author Commented:
make that:

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

:)
Saqib Husain, SyedEngineerCommented:
You can reduce the parentheses


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

:)
sarabandeCommented:
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
evilrixSenior 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".
Saqib Husain, SyedEngineerCommented:
ozoCommented:
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.
Saqib Husain, SyedEngineerCommented:
"Midpoint of side"  seems perfect
Saqib Husain, SyedEngineerCommented:
Accept a40780458
ozoCommented:
Distances to corners in http:#a40780458 is slightly simpler than distances to midpoints,
requiring 2 comparisons instead of 3, and 8 fewer additions.
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.
chemicalx001Author Commented:
Hi,  as I posted on 2015-04-08,  I requested this question be closed as it was answered sufficiently.
Thanks!
Saqib Husain, SyedEngineerCommented:
As mentioned by the asker, #a40711893 was good enough for him
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.