Skip to content

Commit 18c3315

Browse files
committed
Haversine Distance
1 parent 94fddfb commit 18c3315

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import UIKit
3+
4+
func haversineDinstance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double {
5+
6+
let haversin = { (angle: Double) -> Double in
7+
return (1 - cos(angle))/2
8+
}
9+
10+
let ahaversin = { (angle: Double) -> Double in
11+
return 2*asin(sqrt(angle))
12+
}
13+
14+
// Converts from degrees to radians
15+
let dToR = { (angle: Double) -> Double in
16+
return (angle / 360) * 2 * M_PI
17+
}
18+
19+
let lat1 = dToR(la1)
20+
let lon1 = dToR(lo1)
21+
let lat2 = dToR(la2)
22+
let lon2 = dToR(lo2)
23+
24+
return radius * ahaversin(haversin(lat2 - lat1) + cos(lat1) * cos(lat2) * haversin(lon2 - lon1))
25+
}
26+
27+
let amsterdam = (52.3702, 4.8952)
28+
let newYork = (40.7128, -74.0059)
29+
30+
// 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.
31+
haversineDinstance(la1: amsterdam.0, lo1: amsterdam.1, la2: newYork.0, lo2: newYork.1)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

HaversineDistance/HaversineDistance.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HaversineDistance/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Haversine Distance
2+
3+
Calculates the distance on a sphere between two points given in latitude and longitude using the haversine formula.
4+
5+
The haversine formula can be found on [Wikipedia](https://en.wikipedia.org/wiki/Haversine_formula)
6+
7+
The Haversine Distance is implemented as a function as a class would be kind of overkill.
8+
9+
`haversineDinstance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double`
10+
11+
- `la1` is the latitude of point 1 in degrees.
12+
- `lo1` is the longitude of point 1 in degrees.
13+
- `la2` is the latitude of point 2 in degrees.
14+
- `lo2` is the longitude of point 2 in degrees.
15+
- `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)).
16+
17+
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.
18+
19+
1. `haversine` implements the haversine, a trigonometric function.
20+
2. `ahaversine` the inverse function of the haversine.
21+
3. `dToR` a closure converting degrees to radians.
22+
23+
The result of `haversineDistance` is returned in meters.

0 commit comments

Comments
 (0)