Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions HaversineDistance/HaversineDistance.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

import UIKit

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

let haversin = { (angle: Double) -> Double in
return (1 - cos(angle))/2
}

let ahaversin = { (angle: Double) -> Double in
return 2*asin(sqrt(angle))
}

// Converts from degrees to radians
let dToR = { (angle: Double) -> Double in
return (angle / 360) * 2 * M_PI
}

let lat1 = dToR(la1)
let lon1 = dToR(lo1)
let lat2 = dToR(la2)
let lon2 = dToR(lo2)

return radius * ahaversin(haversin(lat2 - lat1) + cos(lat1) * cos(lat2) * haversin(lon2 - lon1))
}

let amsterdam = (52.3702, 4.8952)
let newYork = (40.7128, -74.0059)

// Google says it's 5857 km so our result is only off by 2km which could be due to all kinds of things, not sure how google calculates the distance or which latitude and longitude google uses to calculate the distance.
haversineDinstance(la1: amsterdam.0, lo1: amsterdam.1, la2: newYork.0, lo2: newYork.1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions HaversineDistance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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](https://en.wikipedia.org/wiki/Haversine_formula)

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`

- `la1` is the latitude of point 1 in degrees.
- `lo1` is the longitude of point 1 in degrees.
- `la2` is the latitude of point 2 in degrees.
- `lo2` is the longitude of point 2 in degrees.
- `radius` is the radius of the sphere considered in meters, which defaults to the mean radius of the earth (from [WolframAlpha](http://www.wolframalpha.com/input/?i=earth+radius)).

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.*