Created
April 3, 2014 13:08
-
-
Save t3hnar/9953986 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Coordinates(x: Double, y: Double) | |
object HaversineDistance { | |
def apply(a: Coordinates, b: Coordinates): Double = { | |
val deltaLat = math.toRadians(b.x - a.x) | |
val deltaLong = math.toRadians(b.y - a.y) | |
val x = math.pow(math.sin(deltaLat / 2), 2) + math.cos(math.toRadians(a.x)) * math.cos(math.toRadians(b.x)) * math.pow(math.sin(deltaLong / 2), 2) | |
val greatCircleDistance = 2 * math.atan2(math.sqrt(x), math.sqrt(1 - x)) | |
3958.761 * greatCircleDistance | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment