Click here to Skip to main content
15,885,952 members
Articles / Programming Languages / C#

GPS Receivers, Geodesy, and Geocaching: Vincenty’s Formula

Rate me:
Please Sign up or sign in to vote.
5.00/5 (21 votes)
9 Jan 2008CPOL5 min read 94.4K   698   79  
Vincenty's Formula is an iterative solution for calculating the distance and direction between two points along the surface of Earth.
/* Gavaghan.Geodesy by Mike Gavaghan
 * 
 * http://www.gavaghan.org/blog/free-source-code/geodesy-library-vincentys-formula/
 * 
 * This code may be freely used and modified on any personal or professional
 * project.  It comes with no warranty.
 */
using System;
using System.Text;

namespace Gavaghan.Geodesy
{
  /// <summary>
  /// This is the outcome of a three dimensional geodetic calculation.  It represents
  /// the path a between two GlobalPositions for a specified reference ellipsoid.
  /// </summary>
  [Serializable]
  public struct GeodeticMeasurement
  {
    /// <summary>The average geodetic curve.</summary>
    private readonly GeodeticCurve mCurve;

    /// <summary>The elevation change, in meters, going from the starting to the ending point.</summary>
    private readonly double mElevationChange;

    /// <summary>The distance travelled, in meters, going from one point to the next.</summary>
    private readonly double mP2P;

    /// <summary>
    /// Creates a new instance of GeodeticMeasurement.
    /// </summary>
    /// <param name="averageCurve">the geodetic curve as measured at the average elevation between two points</param>
    /// <param name="elevationChange">the change in elevation, in meters, going from the starting point to the ending point</param>
    public GeodeticMeasurement(GeodeticCurve averageCurve, double elevationChange)
    {
      double ellDist = averageCurve.EllipsoidalDistance;

      mCurve = averageCurve;
      mElevationChange = elevationChange;
      mP2P = Math.Sqrt(ellDist * ellDist + mElevationChange * mElevationChange);
    }

    /// <summary>
    /// Get the average geodetic curve.  This is the geodetic curve as measured
    /// at the average elevation between two points.
    /// </summary>
    public GeodeticCurve AverageCurve
    {
      get { return mCurve; }
    }

    /// <summary>
    /// Get the ellipsoidal distance (in meters).  This is the length of the average geodetic
    /// curve.  For actual point-to-point distance, use PointToPointDistance property.
    /// </summary>
    public double EllipsoidalDistance
    {
      get { return mCurve.EllipsoidalDistance; }
    }

    /// <summary>
    /// Get the azimuth.  This is angle from north from start to end.
    /// </summary>
    public Angle Azimuth
    {
      get { return mCurve.Azimuth; }
    }

    /// <summary>
    /// Get the reverse azimuth.  This is angle from north from end to start.
    /// </summary>
    public Angle ReverseAzimuth
    {
      get { return mCurve.ReverseAzimuth; }
    }

    /// <summary>
    /// Get the elevation change, in meters, going from the starting to the ending point.
    /// </summary>
    public double ElevationChange
    {
      get { return mElevationChange; }
    }

    /// <summary>
    /// Get the distance travelled, in meters, going from one point to the next.
    /// </summary>
    public double PointToPointDistance
    {
      get { return mP2P; }
    }

    /// <summary>
    /// Get the GeodeticMeasurement as a string
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
      StringBuilder builder = new StringBuilder();

      builder.Append(mCurve.ToString());
      builder.Append(";elev12=");
      builder.Append(mElevationChange);
      builder.Append(";p2p=");
      builder.Append(mP2P);

      return builder.ToString();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
United States United States
Mike Gavaghan opines on C# and .Net in his blog Talk Nerdy To Me[^]. He is a Microsoft Certified Professional Developer working as a C#/.Net software consultant based in Dallas, Texas.

Since 1992, he has supported clients in diverse businesses including financial services, travel, airlines, and telecom. He has consulted at both mammoth enterprises and small startups (and sees merits and problems in both).

You may also view his profile on LinkedIn[^].

Comments and Discussions