Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

I have spent the last couple of days looking for the code for a Nautical Calculator that will do 2 things... 1 Calculate the Bearing between 2 WayPoints (2 given Lat & Long's)... and 2 Calculate the distance between 2 WayPoints (2 given Lat & Long's).

I have found lots of solutions using Java but none in C# or VB. I need to put this in a Windows Forms application and would prefer to not load a browser just to show these tools.

Does anyone know where I can find such tools or where I can find the code in C# that I can use or modify?

Thanks
Phill
Posted

I don't know where you can find examples already written, but math is math, regardless of the language you're using. It should be a minor matter to convert it, especially to C#, since Java and C# so closely resemble each other (some of the objects in the frameworks are even called the same thing).
 
Share this answer
 
v2
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:

Java
/*JAVA*/
var R = 6371; // km
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#
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
C#
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:
C#
//lat1,lat2,lon1, and lon2 are already in radians
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
C#
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:
Java
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);  // E-W line gives dPhi=0
// if dLon over 180° take shorter rhumb across 180° meridian:
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);
 
Share this answer
 
I agree math is math. But it is a lot of math using Trig. Sin, Co-Sign etc and that Java has those functions built in.

From what I have been reading the built in math functions are what makes it easier in Java. I am not sure if C# supports all the trig. tables.

I will give it a go at translating Java into C#.

Thanks
Phill

--------------

From JSOP: Do you honestly think that Microsoft would omit what amounts to standard math functions from an enterprise-level programming language?



 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900