tags:

views:

323

answers:

3

Hi!

I am trying to implement the equations presented here regarding finding a point on a given perpendicular distance from a line. Unfortunately the lines I receive are not straight. Is this due to me mixing Lat/long with regular x/y coordinates or have I done something else wrong?!

 double distPoint = 0.02;

 double latDiff = temp2.Latitude - temp.Latitude;  
 double longDiff = temp2.Longitude - temp.Longitude;
 double length = Math.Sqrt(latDiff * latDiff + longDiff * longDiff); 
 double uLat = latDiff / length; 
 double uLong = longDiff / length;

 double newLat1 = temp2.Latitude + (distPoint / 2) * uLat;  
 double newLong1 = temp2.Longitude - (distPoint / 2) * uLong;

 double newLat2 = temp2.Latitude -  (distPoint / 2) * uLat;  
 double newLong2 = temp2.Longitude + (distPoint / 2) * uLong;

Have changed the code now, and the variable names. Still receiving the fault :-(

double dist = 0.02;

double latDiff = secondTestPoint.Latitude - firstTestPoint.Latitude;
double longDiff = secondTestPoint.Longitude - firstTestPoint.Longitude;

double length = Math.Sqrt(latDiff * latDiff + longDiff * longDiff);
double uLat = latDiff / length;
double uLong = longDiff / length;

double newLat1 = secondTestPoint.Latitude + (dist / 2) * uLong;
double newLong1 = secondTestPoint.Longitude - (dist / 2) * uLat;

double newLat2 = secondTestPoint.Latitude - (dist / 2) * uLong;
double newLong2 = secondTestPoint.Longitude + (dist / 2) * uLat;

Here are the variable values:

latDiff = -0.0046187639236450195
longDiff = -0.0058203935623168945

length = 0.0074303405980227239
uLat = -0.62160864131505777
uLong = -0.78332796263279647

newLat1 = 58.39273776863341
newLong1 = 15.558675147558933

newLat2 = 58.408404327886061
newLong2 = 15.546242974732632

UPDATE: I have come to the conclusion that the fault is due to lat/long issues. It seems reasonable that it would generate faults to think that the lat/long are equivalent to squares when they in fact are not. Especially when working with northern europe.

A: 

Latitude and Longitude degrees are not 1:1.

CptSkippy
Note that this ratio is not accurate in all locations.
Brian
Actually, the ratio of surface distance between latitude and longitude depends on how far you are from the equator, so your numbers are wrong unless the asker's map is at the same latitude. This is even explained in the linked page. I am sorely tempted to downvote because of this, but this answer does have some value, as it is something the asker may want to consider.
John Y
1 degree longitude is about 69.13 miles on the equator, and 0 at the poles. It's not a simple ratio like that. That equation is true if you're in Hampshire, in the United Kingdom, or somewhere else of equivalent latitude. Most people are not.
Aric TenEyck
My location is in the south of Sweden.
svanerik
A: 

In the formula you linked, the last 4 lines have a multiplication sequence as:

[dy, dx, dy, dx] == [uLong, uLat, uLong, uLat]

however you've used:

[dx, dy, dx, dy] == [uLat, uLong, uLat, uLong]

Your last 4 lines should be this:

double newLat1 = temp2.Latitude + (distPoint / 2) * uLong;   //dy appears first
double newLong1 = temp2.Longitude - (distPoint / 2) * uLat;  //then dx

double newLat2 = temp2.Latitude -  (distPoint / 2) * uLong;  
double newLong2 = temp2.Longitude + (distPoint / 2) * uLat;
Gavin Miller
Hi, I tried implementing this but it didn't work. I've posted the values of the variables in the question so you can see if it has something to do with that. Thank you!
svanerik
+1  A: 

At such a small scale, the difference between x/y and lat/long isn't relevant. You've done something else wrong; what you should have is:

 double distPoint = 0.02;

 double latDiff = temp2.Latitude - temp.Latitude;
 double longDiff = temp2.Longitude - temp.Longitude;
 double length = Math.Sqrt(latDiff * latDiff + > longDiff * longDiff); 
 double uLat = latDiff / length; 
 double uLong = longDiff / length;

 double newLat1 = temp2.Latitude + (distPoint / 2) * uLong;
 double newLong1 = temp2.Longitude - (distPoint / 2) * uLat;

 double newLat2 = temp2.Latitude - (distPoint / 2) * uLong;
 double newLong2 = temp2.Longitude + (distPoint / 2) * uLat;

That is, the vector (uLat, uLong) is a unit vector in the direction of your line, so a perpendicular unit vector is (uLong, -uLat) - note that the coordinates swapped position, in addition to one being negated.

Daniel Martin
Hi, this seems to make sense. I tried implementing it but it doesnt work. The numbers are really small, can this be the reason?I posted the numbers above.
svanerik
The "end result" of your answer is basically equivalent to LFSR Consulting's, but I prefer your perpendicular vector explanation.
John Y