You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
918 B
47 lines
918 B
package util
|
|
|
|
import model.Coord
|
|
import scala.math._
|
|
|
|
object Util {
|
|
private val r = 6471.00 // km
|
|
|
|
def toRad(num: Double): Double = {
|
|
val a = num * Pi / 180
|
|
a
|
|
}
|
|
|
|
def getHaversineDistance(c1: Coord, c2: Coord): Double = {
|
|
val x1 = c1.latitude - c2.latitude
|
|
val x2 = c1.longitude - c2.longitude
|
|
|
|
val dlat = toRad(x1)
|
|
val dlon = toRad(x2)
|
|
|
|
val a = pow(sin(dlat / 2), 2) +
|
|
cos(toRad(c1.latitude)) *
|
|
cos(toRad((c2.latitude))) *
|
|
pow(sin(dlon / 2), 2)
|
|
|
|
val c = 2 * atan2(sqrt(a), sqrt(1 - a))
|
|
val d = r * c
|
|
d
|
|
}
|
|
|
|
def getHaversineDistance(lat: Double, lon: Double): Double = {
|
|
val x1 = lat
|
|
val x2 = lon
|
|
|
|
val dlat = toRad(x1)
|
|
val dlon = toRad(x2)
|
|
|
|
val a = pow(sin(dlat / 2), 2) +
|
|
cos(toRad(x1)) *
|
|
cos(toRad((0))) *
|
|
pow(sin(dlon / 2), 2)
|
|
|
|
val c = 2 * atan2(sqrt(a), sqrt(1 - a))
|
|
val d = r * c
|
|
d
|
|
}
|
|
}
|