First, I assume that you're talking about a "rhumb" line, which means a path of constant bearing, which crosses all meridians at the same angle.
C# has the same built in math functions. I suggest looking at:
Calculate distance, bearing and more between Latitude/Longitude points[
^]
For distance, it shows:
var R = 6371;
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
Yes, some of the java stuff is nice like the
.toRad()
, but it's pretty easy to convert that to C#
int r = 6371;
double dLat = DegreeToRadian(Lat2 - Lat1);
double dLon = DegreeToRadian(Lon2 - Lon1);
double a = Math.Pow(Math.Sin(dLat / 2), 2) +
Math.Pow(Math.Cos(DegreeToRadian(Lat1)), 2) *
Math.Pow(Math.Sin(dLon / 2), 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double d = r * c;
with
private double DegreeToRadian(double angle)
{
return Math.PI * angle / 180.0;
}
If you wanted to do the rhumb line and distance, you can use a slightly different calculation which is also shown on that page...
in C# it's:
double R = 6371;
double dLat = lat2 - lat1;
double dLon = lon2 - lon1;
var dPhi = Math.Log(Math.Tan((lat2 / 2) + (Math.PI / 4)) /
Math.Tan((lat1 / 2) + (Math.PI / 4)));
double q;
if (!double.IsNaN(dLat / dPhi))
{
q = dLat / dPhi;
}
else
{
q = Math.Cos(lat1);
}
if (Math.Abs(dLon) > Math.PI)
{
if (dLon > 0)
{
dLon = -(2 * Math.PI - dLon);
}
else
{
dLon = (2 * Math.PI + dLon);
}
}
double d = Math.Sqrt(dLat * dLat + q * q * dLon * dLon) * R;
double brng = (RadianToDegree(Math.Atan2(dLon, dPhi))+ 360) % 360;
with
private double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}
The last one did take a little bit of conversion over the java code which was:
var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
var q = (!isNaN(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1);
if (Math.abs(dLon) > Math.PI) {
dLon = dLon>0 ? -(2*Math.PI-dLon) : (2*Math.PI+dLon);
}
var d = Math.sqrt(dLat*dLat + q*q*dLon*dLon) * R;
var brng = Math.atan2(dLon, dPhi);