svipxu / winforms-geplugin-control-library

Automatically exported from code.google.com/p/winforms-geplugin-control-library
GNU General Public License v3.0
0 stars 0 forks source link

Add Destination method to Coordinate class #49

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What should this feature enhancement do?
Calculate a destination coordinate given a distance and bearing.

My implementation, cleanup as required:
        public Coordinate Destination(double distance, double bearing, bool kilometers = false)
        {
            double lat1 = Maths.ConvertDegreesToRadians(this.Latitude);
            double lon1 = Maths.ConvertDegreesToRadians(this.Longitude);
            bearing = Maths.ConvertDegreesToRadians(bearing);

            if (kilometers)
            {
                distance = Maths.ConvertKilometresToMiles(distance);
            }
            double DoverR = distance / Maths.EarthMeanRadiusMiles;

            double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(DoverR) + Math.Cos(lat1) * Math.Sin(DoverR) * Math.Cos(bearing));
            double lon2 = lon1 + Math.Atan2(Math.Sin(bearing) * Math.Sin(DoverR) * Math.Cos(lat1), Math.Cos(DoverR) - Math.Sin(lat1) * Math.Sin(lat2));

            return new Coordinate(Maths.ConvertRadiansToDegrees(lat2), Maths.ConvertRadiansToDegrees(lon2));
        }

Original issue reported on code.google.com by joe.wake...@gmail.com on 22 Aug 2011 at 2:44

GoogleCodeExporter commented 8 years ago
Hey, thanks a lot for this, looks great - I am planning to commit a new version 
real soon and I am pretty sure I'll pop this in. Although the logic will go in 
the Geo.Maths class as a static method, I will add a method with the same 
signature as yours to the Coordinate class.

// Maths.cs
public static double[] Destination(Coordinate origin, double distance, double 
bearing, bool kilometers = true)
{

  double lat1 = Maths.ConvertDegreesToRadians(origin.Latitude);
  double lon1 = Maths.ConvertDegreesToRadians(origin.Longitude);
  bearing = Maths.ConvertDegreesToRadians(bearing);
  double angularDistance = 0;

  if (kilometers)
  {
    angularDistance = distance / Maths.EarthMeanRadiusKilometres;
  }
  else
  {
    angularDistance = Maths.ConvertKilometresToMiles(distance) / Maths.EarthMeanRadiusMiles;
  }

  double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(angularDistance) + Math.Cos(lat1) * Math.Sin(angularDistance) * Math.Cos(bearing));
  double lon2 = lon1 + Math.Atan2(Math.Sin(bearing) * Math.Sin(angularDistance) * Math.Cos(lat1), Math.Cos(angularDistance) - Math.Sin(lat1) * Math.Sin(lat2));

  return new double[] { Maths.ConvertRadiansToDegrees(lat2), Maths.ConvertRadiansToDegrees(lon2) };
}

public static double[] Destination(double[] origin, double distance, double 
bearing, bool kilometers = true)
{
  return Destination(new Coordinate(origin), distance, bearing, kilometers);
}

// Coordinate.cs
public Coordinate Destination(double distance, double bearing, bool kilometers 
= true)
{
  return new Coordinate(Maths.Destination(this, distance, bearing, kilometers));
}

Original comment by fraser.c...@gmail.com on 24 Aug 2011 at 10:41

GoogleCodeExporter commented 8 years ago
This has been added to the latest commit

Original comment by fraser.c...@gmail.com on 8 Oct 2011 at 12:45