Haversine Distance

Calculates the distance on a sphere between two points given in latitude and longitude using the haversine formula.

The haversine formula can be found on Wikipedia

The Haversine Distance is implemented as a function as a class would be kind of overkill.

haversineDinstance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double

The function contains 3 closures in order to make the code more readable and comparable to the Haversine formula given by the Wikipedia page mentioned above.

  1. haversine implements the haversine, a trigonometric function.
  2. ahaversine the inverse function of the haversine.
  3. dToR a closure converting degrees to radians.

The result of haversineDistance is returned in meters.

Written for Swift Algorithm Club by Jaap Wijnen.