Reputation: 123
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.
Upvotes: 3
Views: 3012
Reputation: 43875
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;
Upvotes: 0
Reputation: 23578
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.
Upvotes: 2