2

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.

Community
  • 1
  • 1
svanerik
  • 113
  • 1
  • 2
  • 5
  • 1
    image here: http://img266.imageshack.us/img266/9550/mapu.gif The longer line is the one that should be perpendicular. – svanerik Jul 27 '09 at 13:57
  • For those of us who are visual types - thank you for the image! – Matt Ball Jul 27 '09 at 14:06
  • On that note, I think the image could be made even more helpful by annotating it with which points (or their x-y values) come from with values in your pseudocode, since I'm not sure of that from your variable names. – Matt Ball Jul 27 '09 at 14:09
  • I think your question is incomplete - or the result undefined. It appears that you have two positions specified in spherical polar coordinates (yet the question is 2D, not 3D?). It appears that you want to determine the polar coordinates of a point that is a given distance from the line connecting the two points. You have not specified the radius of the sphere (which matters because the distance is given). And, in general, there are many points (two lines worth of them) that are the given distance from a line. Hence you need to provide more conditions on the solution. – Jonathan Leffler Jul 27 '09 at 14:13
  • @svanerik: In your comments to Daniel Martin and LFSR Consulting, you mentioned that their solution still doesn't work. How is it not working? Can you post another picture? Despite CptSkippy's incorrect numbers, I believe the principle is still relevant: That is, I think you have to account for your latitude (which is even higher than his Hampshire, UK example). – John Y Jul 27 '09 at 15:01

3 Answers3

1

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
  • 23,083
  • 6
  • 50
  • 70
  • 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 Jul 27 '09 at 14:35
  • The "end result" of your answer is basically equivalent to LFSR Consulting's, but I prefer your perpendicular vector explanation. – John Y Jul 27 '09 at 14:39
  • Depends if you want perpendicular on the earth's surface or perpendicular using coordinates. Remember 90deg latitude is thousands of km/miles near the equator, and a few cm/inches near the poles. This makes lines have less of a gradient if you plot them from a local Mercator projection to a 1:1 lat long projection. – AER Aug 05 '19 at 00:25
0

Latitude and Longitude degrees are not 1:1.

Community
  • 1
  • 1
MyItchyChin
  • 13,733
  • 1
  • 24
  • 44
  • Note that this ratio is not accurate in all locations. – Brian Jul 27 '09 at 14:14
  • 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 Jul 27 '09 at 14:29
  • 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 Jul 27 '09 at 14:30
  • My location is in the south of Sweden. – svanerik Jul 27 '09 at 14:37
0

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
  • 43,168
  • 21
  • 122
  • 188
  • 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 Jul 27 '09 at 14:36