-
Notifications
You must be signed in to change notification settings - Fork 13
/
geofence.go
49 lines (37 loc) · 1.08 KB
/
geofence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"github.com/kellydunn/golang-geo"
)
// Geofence represents a point on the Earth with an accuracy radius in meters.
type Geofence struct {
Latitude, Longitude, Radius float64
}
// SetIntersection is a description of the relationship between two sets.
type SetIntersection uint
const (
// IsDisjoint means that the two sets have no common elements.
IsDisjoint SetIntersection = 1 << iota
// IsSubset means the first set is a subset of the second.
IsSubset
// IsSuperset means the second set is a subset of the first.
IsSuperset
)
// Intersection describes the relationship between two geofences
func (mi *Geofence) Intersection(tu *Geofence) (i SetIntersection) {
miPoint := geo.NewPoint(mi.Latitude, mi.Longitude)
tuPoint := geo.NewPoint(tu.Latitude, tu.Longitude)
distance := miPoint.GreatCircleDistance(tuPoint) * 1000
radiusSum := mi.Radius + tu.Radius
radiusDiff := mi.Radius - tu.Radius
if distance-radiusSum > 0 {
i = IsDisjoint
return
}
if -distance+radiusDiff >= 0 {
i |= IsSuperset
}
if -distance-radiusDiff >= 0 {
i |= IsSubset
}
return
}