MIF_E31222379_BE/utils/havershine.go

40 lines
664 B
Go

package utils
import (
"math"
)
const (
earthRadiusMi = 3958
earthRaidusKm = 6371
)
type Coord struct {
Lat float64
Lon float64
}
func degreesToRadians(d float64) float64 {
return d * math.Pi / 180
}
func Distance(p, q Coord) (mi, km float64) {
lat1 := degreesToRadians(p.Lat)
lon1 := degreesToRadians(p.Lon)
lat2 := degreesToRadians(q.Lat)
lon2 := degreesToRadians(q.Lon)
diffLat := lat2 - lat1
diffLon := lon2 - lon1
a := math.Pow(math.Sin(diffLat/2), 2) + math.Cos(lat1)*math.Cos(lat2)*
math.Pow(math.Sin(diffLon/2), 2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
mi = c * earthRadiusMi
km = c * earthRaidusKm
return mi, km
}