Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your formula looks different to my implementation. However mine's in .NET but I've unit tested it and it works well.</p> <p>It's a slightly rewritten version of this: <a href="http://megocode3.wordpress.com/2008/02/05/haversine-formula-in-c/" rel="nofollow">http://megocode3.wordpress.com/2008/02/05/haversine-formula-in-c/</a></p> <pre><code>/// &lt;summary&gt; /// Implementation of the Haversine formula /// For calculating the distance between 2 points on a sphere /// http://en.wikipedia.org/wiki/Haversine_formula /// &lt;/summary&gt; public class Haversine { /// &lt;summary&gt; /// Calculate the distance between 2 points in miles or kilometers /// http://megocode3.wordpress.com/2008/02/05/haversine-formula-in-c/ /// /// This assumes sea level /// &lt;/summary&gt; public double Distance(LatLon pos1, LatLon pos2, DistanceType type) { const double RADIUS_OF_EARTH_IN_MILES = 3963.1676; const double RADIUS_OF_EARTH_IN_KILOMETERS = 6378.1; //radius of the earth double R = (type == DistanceType.Miles) ? RADIUS_OF_EARTH_IN_MILES : RADIUS_OF_EARTH_IN_KILOMETERS; //Deltas double dLat = ToRadian(pos2.Lat - pos1.Lat); double dLon = ToRadian(pos2.Lon - pos1.Lon); double a = Math.Sin(dLat/2)*Math.Sin(dLat/2) + Math.Cos(ToRadian(pos1.Lat))*Math.Cos(ToRadian(pos2.Lat)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2); double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a))); double d = R*c; return d; } /// &lt;summary&gt; /// Convert to Radians. /// &lt;/summary&gt; private double ToRadian(double val) { return (Math.PI / 180) * val; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload