views:

376

answers:

4

Hi,

I have two gps coordinates which link together to make a line. I also have a gps point which is near to, but never exactly on, the line. My question is how do I find the nearest point along the line to the given point?

Many thanks

+2  A: 

Use projection.

Do you have any examples? That wiki page is not particularly usable to someone who hasn't used projection before. Thanks
Mylo
Since your question sounds like homework, I think you should solve the problem yourself.
Christ sake, I have a simple question (and no it's not homework) and all you do is tag it as homework... very helpful.Thanks to those who posted useful answers.
Mylo
You could at least have posted some code to show you tried to solve the problem. Since you didn't, your question smells like homework.
So does everyone who posts a question have to supply some code to prove their not a student? Rediculous.
Mylo
It would help in judging if a question is worth an answer or not.
You don’t have to prove that you’re not a student, you just have to prove that you’re willing (and capable) to do the work yourself after you’ve been given a couple of pointers. That’s what programming is.
Bombe
+4  A: 

A quick Google search brought up the following link:

Minimum Distance between a Point and a Line

It shows the explanation with a diagram and the mathematical explanation, along with source code in a few languages, including Java.

coobird
Thanks, this should allow me to get what I wanted.
Mylo
Glad I could help :)
coobird
A: 

http://www.gamedev.net/community/forums/topic.asp?topic%5Fid=444154&whichpage=1&#2941160

It is in C++ but it should be easy to port over.

mlk
A: 

Try this:

ratio = (((x1-x0)^2+(y1-y0)^2)*((x2-x1)^2 + (y2-y1)^2) - ((x2-x1)(y1-y0) - (x1-x0)(y2-y1))^2)^0.5
        -----------------------------------------------------------------------------------------
                                            ((x2-x1)^2 + (y2-y1)^2)

xc = x1 + (x2-x1)*ratio;
yc = y1 + (y2-y1)*ratio;

Where:
    x1,y1 = point#1 on the line
    x2,y2 = point#2 on the line
    x0,y0 = Another point near the line
    xc,yx = The nearest point of x0,y0 on the line
    ratio = is the ratio of distance of x1,y1 to xc,yc and distance of x1,y1 to x2,y2
    ^2    = square
    ^0.5  = square root

The formular is derived after we find the distant from point x0,y0 to line (x1,y1 -> x2,y3). See here

I've test this code here (this particular one I gave you above) but I've used it similar method years ago and it work so you may try.

NawaMan