Skip to content

Instantly share code, notes, and snippets.

@t3hnar
Created April 3, 2014 13:08
Show Gist options
  • Save t3hnar/9953986 to your computer and use it in GitHub Desktop.
Save t3hnar/9953986 to your computer and use it in GitHub Desktop.
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