Reputation: 355
following this article to calculate the point from a lat and long with a bearing and distance when run results in NaN?
article below :
A method is given for calculating the destination point for ellipsoid earth models using Vincenty's formula:
Convert the starting point latitude 'lat1' (in the range -90 to 90) to radians. lat1 = lat1 * PI/180 Convert the starting point longitude 'lon1' (in the range -180 to 180) to radians. lon1 = lon1 * PI/180 Convert the bearing 'brg' (in the range 0 to 360) to radians. brg = brg * PI/180 Ellipsoid earth models
Note: the variable 'flat' below represents the earth's polar flattening used in various ellipsoid models. For the commonly used WGS-84, let flat = 298.257223563.
Given the distance s in meters, the semi-major axis 'a' in meters, the semi-minor axis 'b' in meters and the polar flattening 'flat'. Calculate the destination point useing Vincenty's formula. Shortened variable names are used.
f = 1/flat
sb=sin(brg)
cb=cos(brg)
tu1=(1-f)*tan(lat1)
cu1=1/sqrt((1+tu1*tu1))
su1=tu1*cu1
s2=atan2(tu1, cb)
sa = cu1*sb
csa=1-sa*sa
us=csa*(a*a - b*b)/(b*b)
A=1+us/16384*(4096+us*(-768+us*(320-175*us)))
B = us/1024*(256+us*(-128+us*(74-47*us)))
s1=s/(b*A)
s1p = 2*PI
Loop through the following while condition is true.
while (abs(s1-s1p) > 1e-12)
cs1m=cos(2*s2+s1)
ss1=sin(s1)
cs1=cos(s1)
ds1=B*ss1*(cs1m+B/4*(cs1*(-1+2*cs1m*cs1m)- B/6*cs1m*(-3+4*ss1*ss1)*(-3+4*cs1m*cs1m)))
s1p=s1
s1=s/(b*A)+ds1
Continue calculation after the loop.
t=su1*ss1-cu1*cs1*cb
lat2=atan2(su1*cs1+cu1*ss1*cb, (1-f)*sqrt(sa*sa + t*t))
l2=atan2(ss1*sb, cu1*cs1-su1*ss1*cb)
c=f/16*csa*(4+f*(4-3*csa))
l=l2-(1-c)*f*sa* (s1+c*ss1*(cs1m+c*cs1*(-1+2*cs1m*cs1m)))
d=atan2(sa, -t)
finalBrg=d+2*PI
backBrg=d+PI
lon2 = lon1+l;
Convert lat2, lon2, finalBrg and backBrg to degrees
lat2 = lat2 * 180/PI
lon2 = lon2 * 180/PI
finalBrg = finalBrg * 180/PI
backBrg = backBrg * 180/PI
If lon2 is outside the range -180 to 180, add or subtract 360 to bring it back into that range. If finalBrg or backBrg is outside the range 0 to 360, add or subtract 360 to bring them back into that range. Note: the variables 'a', 'b' and 'flat' above have the following relationships:
b = a - (a/flat)
flat = a / (a - b)
so porting this formula to c# i ended up with:
double Latitude = 50.390202;
double Longitude = -3.9204310000000078;
double Bearing = 225;
double lat1 = Latitude * (Math.PI / 180.0);
double lon1 = Longitude * (Math.PI / 180.0);
double brg = Bearing * (Math.PI / 180.0);
double s = 1000;
double a = 6378137.0;
double b = 6356752.314245;
double f = 1 / 298.257223563;
double sb = Math.Sin(brg);
double cb = Math.Cos(brg);
double tu1 = (1-f) * Math.Tan(lat1);
double cu1 = 1 / Math.Sqrt((1+tu1*tu1));
double su1 = tu1 * cu1;
double s2 = Math.Atan2(tu1, cb);
double sa = cu1 * sb;
double csa = 1 - sa * sa;
double us = csa * (a * a - b * b) / (b * b);
double A = 1 + us / 16384 * (4096 + us * (- 768 + us * (320 - 175 * us)));
double B = us / 1024 * (256 + us * (-128 + us * (74 - 47 * us)));
double s1 = s / (b * A);
double s1p = 2 * Math.PI;
while (Math.Abs(s1-s1p) > 1e-12)
{
cs1m = Math.Cos(2 * s2 + s1);
ss1 = Math.Sin(s1);
cs1 = Math.Cos(s1);
double ds1 = B * ss1 * (cs1m + B / 4 * (cs1 * (-1 + 2 * cs1m * cs1m) - B / 6 * cs1m * (-3 + 4 * ss1 * ss1) * (-3 + 4 * cs1m * cs1m)));
s1p = s1;
s1 = s / (b * A) + ds1;
}
double t = su1 * ss1 - cu1 * cs1 * cb;
double lat2 = Math.Atan2(su1 * cs1 + cu1 * ss1 * cb, (1 - f) * Math.Sqrt(sa * sa + t * t));
double l2 = Math.Atan2(ss1 * sb, cu1 * cs1 - su1 * ss1 * cb);
double c = f / 16 * csa * (4 + f * (4 - 3 * csa));
double l = l2 - (1 - c) * f * sa * (s1 + c * ss1 * (cs1m + c * cs1 * (-1 + 2 * cs1m * cs1m)));
double d = Math.Atan2(sa, -t);
double finalBrg = d + 2 * Math.PI;
double backBrg = d + Math.PI;
double lon2 = lon1 + l;
lat2 = lat2 * 180 / Math.PI;
lon2 = lon2 * 180/ Math.PI;
finalBrg = finalBrg * 180/ Math.PI;
backBrg = backBrg * 180 / Math.PI;
if (lon2 < -180)
{
lon2 = lon2 + 360;
}
if (lon2 > 180)
{
lon2 = lon2 - 360;
}
if (finalBrg < 0)
{
finalBrg = finalBrg + 360;
}
if (finalBrg > 360)
{
finalBrg = finalBrg - 360;
}
Console.WriteLine("{0} {1}", lat2, lon2);
but when run result in NaN for both lat2 and lon2?
expected result should be:
Latitude: 50°23′02″N 50.38384479 Longitude: 3°55′49″W -3.93037298 Final bearing: 224°59′32″ 224.99234101 Back bearing: 44°59′32″ 44.99234101
does c# calc do something different of have i made a mistake in there that i cant see :(
Thanks
Upvotes: 1
Views: 2786
Reputation: 6773
According to Wikipedia (https://en.wikipedia.org/wiki/Vincenty%27s_formulae) b = (1-f)a
so your 10th line of code should be :
double b = a * (1 - 1 / flat);
NOTE - I haven't checked for any other problems, but it does give values that are not NaN.
Upvotes: 2