3

I'm creating a distance calculator in c# using the haversine equation to calculate the distance between longitudes and latitudes but it is giving the wrong output can anyone see why? the first long and lat values are for a place in Wales (Bangor) and the other is for a place in England (Manchester) Here is the code:

using System;

public static class Program
{
    static double toRadians(double angle)
    {
        return (angle * Math.PI) / 180;
    }

    static double CalcDistance(double lon1, double lon2, double lat1, double lat2)
    {
        lon1 = toRadians(lon1);
        lon2 = toRadians(lon2);
        lat1 = toRadians(lat1);
        lat2 = toRadians(lat2);
        //haversine formula
        double dlat, dlon;
        dlat = lat2 - lat1;
        dlon = lon2 - lon1;
        double a = Math.Pow(Math.Sin(dlat / 2), 2) *
            Math.Cos(lat1) * Math.Cos(lat2) *
            Math.Pow(Math.Sin(dlon / 2), 2);
        double c = 2 * Math.Asin(Math.Sqrt(a));
        // earths radius is KM, use 3956 for miles
        double earthRadius = 6371;
        return (c * earthRadius);
    }



    static void Main(String[] args)
    {
        double lat1, lat2, lon1, lon2;
        lon1= 53.222469;
        lat1 = -4.129424;
        lon2 = 53.244697;
        lat2 = -2.13195;
        Console.WriteLine(CalcDistance(lon1, lon2, lat1, lat2) + " KM");
    }
}

The output given is 0.04301075336978381 KM when the output should be roughly 130KM

canton7
  • 37,633
  • 3
  • 64
  • 77
kian5749
  • 65
  • 8
  • 1
    There are a variety of operations being performed here. When you step through the code in a debugger, which operation first returns an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? – David Nov 06 '19 at 14:23
  • 1
    Can you indicate the original mathematical formula you use, please? –  Nov 06 '19 at 14:24
  • 3
    shouldn't the first `*` in `CalcDistance` be a `+`? – Marc Gravell Nov 06 '19 at 14:24
  • Bingo. [@MarcGravell's right](https://dotnetfiddle.net/1XwZsS) – canton7 Nov 06 '19 at 14:29
  • 1
    @OlivierRogier If you google "haversine formula" from the comment in the code, [you end up here](https://en.wikipedia.org/wiki/Haversine_formula) – canton7 Nov 06 '19 at 14:29

1 Answers1

6

The error is a * vs + (the first one in CalcDistance), but here's a direct conversion from https://www.movable-type.co.uk/scripts/latlong.html, for reference (also adding this to the static double toRadians(this double angle) so it works as an extension method):

static double CalcDistance(double lon1, double lon2, double lat1, double lat2)
{
    const double R = 6371;
    var φ1 = lat1.toRadians();
    var φ2 = lat2.toRadians();
    var Δφ = (lat2 - lat1).toRadians();
    var Δλ = (lon2 - lon1).toRadians();

    var a = Math.Sin(Δφ / 2) * Math.Sin(Δφ / 2) +
            Math.Cos(φ1) * Math.Cos(φ2) *
            Math.Sin(Δλ / 2) * Math.Sin(Δλ / 2);
    var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

    var d = R * c;
    return d;
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • greek characters are allowed in c#? – ina Jan 23 '23 at 03:18
  • 1
    @ina yes, basically; there are rules on which unicode categories/sub-categories are valid as, say, identifiers, but most things that you'd expect to work: work – Marc Gravell Jan 23 '23 at 10:05