Slightly Technical Question......using Lat/Long coords to find equation of lines

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
I'm not sure if this fits into this forum, but I'll try anyway since it has the most traffic.

I would like to know if I can use two lat/long coordinates to find the equation of the line they form, the same way we would use normal x/y coord values. GPS coordinate values for lat/long are different from regular decimal values since they have degrees.

I ask, because I'm trying to create a simple algorithm that performs "snap-to-grid" values that will plot a coordinate to one on the given line.

Thanks.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
How far apart are the points and what's your error tolerance?
If they are relatively close ... 10s of miles maybe, your error won't be to bad. To do it right, you need to us spherical trig.
 

Bootprint

Diamond Member
Jan 11, 2002
9,847
0
0
Originally posted by: Armitage
How far apart are the points and what's your error tolerance?
If they are relatively close ... 10s of miles maybe, your error won't be to bad. To do it right, you need to us spherical trig.

Yep, you'll need to use Great Circle calculations.
link example. ahhhh.
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: Armitage
How far apart are the points and what's your error tolerance?
If they are relatively close ... 10s of miles maybe, your error won't be to bad. To do it right, you need to us spherical trig.

oh I know. It's much less than 10 miles, maybe 400m at the most.

Error at 400m will be neglible...what I want to do is snap the actual gps coord to the road, which is defined by two gps coords given to me.

I just want to know how I can use gps coords to find equations..I don't think I can treat them as simple x/y coordinates.
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: Bootprint
Originally posted by: Armitage
How far apart are the points and what's your error tolerance?
If they are relatively close ... 10s of miles maybe, your error won't be to bad. To do it right, you need to us spherical trig.

Yep, you'll need to use Great Circle calculations.
link example. ahhhh.

thanks for your input, but that isn't needed since I'm dealing with small distances, I can treat it as a flat 2D plane.

Also that gives me distance, not equation.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
For 400m, I think the effect of the Earth's curvature will be negligeable. I am assuming you want this to measure distance?
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: RaynorWolfcastle
For 400m, I think the effect of the Earth's curvature will be negligeable. I am assuming you want this to measure distance?

no I don't. I already did that earlier.

I want the equation for the line formed by two points.

I'm not sure how to explain it in typed words, but I'll try anyway.

Say I'm driving on a road between two main intersections. The GPS coord I get for my position won't be 100% accurate so it won't be exactly on the road I'm travelling, it could be off the road. So I need to get an equation for the road I'm on (for now we are assuming it'll be a straight line) and find the closest point on the line to the actual gps coordinate.

Dunno if that made sense.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
On that scale you can assume that everything is flat, since you'll be within a very tight radius (curvature only starts taking a significant effect if your points are > 20 miles apart, or so IIRC; even then the effect won't be huge. If I understand correctly, you just want to fit a line to 2 points and (each point being and "end" of the street) and then project a third point (the car's location) onto that line. This is really simple linear algebra stuff, but I don't really want to go into a detailed explanation until I know for sure this is what you're trying to do.
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: RaynorWolfcastle
On that scale you can assume that everything is flat, since you'll be within a very tight radius (curvature only starts taking a significant effect if your points are > 20 miles apart, or so IIRC; even then the effect won't be huge. If I understand correctly, you just want to fit a line to 2 points and (each point being and "end" of the street) and then project a third point (the car's location) onto that line. This is really simple linear algebra stuff, but I don't really want to go into a detailed explanation until I know for sure this is what you're trying to do.

that's exactly what I want to do.

As simple as it is, I forgot all my HS algebra stuff :(

I'm trying to do it with regualr x,y coords on paper for now..and trying to remember all the stuff I forgot a few years ago...once I figure out how lat/long coords play into it...then I'll do it in code.

Thanks again.

To the ppl in high school right now, remember the stuff you learn...it might come handy one day!
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Yea, you can treat it as simple x/y coords on the scale you're considering, as long as you stay away from the poles. But there aren't any roads up there anyway :p Careful near the prime meridian also.

Equation of a line is y = mx + b

m = dy/dx
b = y intercept
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: Armitage
Yea, you can treat it as simple x/y coords on the scale you're considering, as long as you stay away from the poles. But there aren't any roads up there anyway :p Careful near the prime meridian also.

Equation of a line is y = mx + b

m = dy/dx
b = y intercept

hehe i know that.

What I did was find equation of the line given two static points.

Then to find map the dynamic point to the line, I found the vector perpendicular to the line. And created a second line using that vector and the dynamic point. The point at which this line intersects the static line gives me its mapped point.

That sounds right doesn't it?
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
Originally posted by: Armitage
Yea, you can treat it as simple x/y coords on the scale you're considering, as long as you stay away from the poles. But there aren't any roads up there anyway :p Careful near the prime meridian also.

Equation of a line is y = mx + b

m = dy/dx
b = y intercept
yep. I prefer the linear algebra way though :p
Here's a quick and easy linear algebra way to solve this
row reduce this matrix to row echelon form (using some linear algebra package), we'll call this matrix A
[x1 1 y1]
[x2 1 y2]

Your line will be of form y = A[1,1]*x + A[2,2]. If you don't have access to any matrix libraries, it can also be done without them, but it's a little longer.
Once you have the line, this is a projection problem.
I'll edit my post once I've drafted that up
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: RaynorWolfcastle
Originally posted by: Armitage
Yea, you can treat it as simple x/y coords on the scale you're considering, as long as you stay away from the poles. But there aren't any roads up there anyway :p Careful near the prime meridian also.

Equation of a line is y = mx + b

m = dy/dx
b = y intercept
yep. I prefer the linear algebra way though :p
Here's a quick and easy linear algebra way to solve this
row reduce this matrix to row echelon form (using some linear algebra package), we'll call this matrix A
[x1 1 y1]
[x2 1 y2]

Your line will be of form y = A[1,1]*x + A[2,2]. If you don't have access to any matrix libraries, it can also be done without them, but it's a little longer.
Once you have the line, this is a projection problem.
I'll edit my post once I've drafted that up

look at my above post b4 you draft up your solution.

note i'll be using VB.net
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: RaynorWolfcastle
Originally posted by: Armitage
Yea, you can treat it as simple x/y coords on the scale you're considering, as long as you stay away from the poles. But there aren't any roads up there anyway :p Careful near the prime meridian also.

Equation of a line is y = mx + b

m = dy/dx
b = y intercept
yep. I prefer the linear algebra way though :p
Here's a quick and easy linear algebra way to solve this
row reduce this matrix to row echelon form (using some linear algebra package), we'll call this matrix A
[x1 1 y1]
[x2 1 y2]

Your line will be of form y = A[1,1]*x + A[2,2]. If you don't have access to any matrix libraries, it can also be done without them, but it's a little longer.
Once you have the line, this is a projection problem.
I'll edit my post once I've drafted that up

Yea, I prefer that also, but y=mx+b tends to be easier to explain to people if they don't follow linear algebra.

Anyway ... to the OP ... sounds like a good approach.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
yep, that works. I won't bother typing up my orthogonal basis, dot product solution, then :D
Armitage, IIRC Madd Indian is a Comp. Eng. I sure as heck hope he can follow linear algebra :shocked: :p
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: RaynorWolfcastle
yep, that works. I won't bother typing up my orthogonal basis, dot product solution, then :D

actually could you help me figure out how to put that in code?

cause it would be a bitch to code in the math to intersect both lines and find out the x,y coords??

Getting the original line equation and all is easy, but how would equate both euqations, collect like terms, and solve in code?

k, I'll be back in 15mins..gotta go buy milk.

thanks for your help guys..you rock!
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: RaynorWolfcastle
It doesn't look like VS.NET has a built in linear algebra library. :(

so there's nothing I can do?

I don't mind writing my own bit of code to handle it. But I'd need some refresher tips in lin alg pertaining to this topic.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
Originally posted by: mAdD INDIAN
Originally posted by: RaynorWolfcastle
It doesn't look like VS.NET has a built in linear algebra library. :(

so there's nothing I can do?

I don't mind writing my own bit of code to handle it. But I'd need some refresher tips in lin alg pertaining to this topic.

no, it can be done without it, it'll just be longer. Let me see if I can come up with something.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
ok, this'll work. Talk about not seeing the forest for the trees ;)
I assume you've programmed your initial function to y = m1*x+b form, where m is the slope variable. the perp will have slope m2 = -1/m1
so you now have to solve for the intersection of m1*x+b1 and m2*x+b2.
solve for b2, by saying that b2 = y2 - x2*m2
m1*x+b1 = m2*x+b2, solve for x
so you get x_sol = (b2 - b1)/(m1 - m2)
y_sol = m1*x_sol+b1

There you go, that can be translated to code easily, I bolded the only parts you need for the code.
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: RaynorWolfcastle
ok, this'll work. Talk about not seeing the forest for the trees ;)
I assume you've programmed your initial function to y = m1*x+b form, where m is the slope variable. the perp will have slope m2 = -1/m1
so you now have to solve for the intersection of m1*x+b1 and m2*x+b2.
solve for b2, by saying that b2 = y2 - x2*m2
m1*x+b1 = m2*x+b2, solve for x
so you get x_sol = (b2 - b1)/(m1 - m2)
y_sol = m1*x_sol+b1

There you go, that can be translated to code easily, I bolded the only parts you need for the code.

dammit I'm dumb.

Thanks a lot man. I should have thought of that too instead of being retarded and trying to figure how to move stuff around.