forked from akhenakh/regionagogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverer.go
53 lines (46 loc) · 1.31 KB
/
coverer.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
50
51
52
53
package regionagogo
import "github.com/golang/geo/s2"
// LoopFence Making s2.Loop implements methods needed for coverage
type LoopFence struct {
*s2.Loop
}
// LoopFenceFromPoints creates a LoopFence from a list of s2.Point
func LoopFenceFromPoints(points []s2.Point) *LoopFence {
loop := s2.LoopFromPoints(points)
return &LoopFence{loop}
}
// CapBound returns the cap that contains this loop
func (l *LoopFence) CapBound() s2.Cap {
return l.Loop.CapBound()
}
// ContainsCell checks whether the cell is completely enclosed by this loop.
// Does not count for loop interior and uses raycasting.
func (l *LoopFence) ContainsCell(c s2.Cell) bool {
for i := 0; i < 4; i++ {
v := c.Vertex(i)
if !l.ContainsPoint(v) {
return false
}
}
return true
}
// IntersectsCell checks if any edge of the cell intersects the loop or if the cell is contained.
// Does not count for loop interior and uses raycasting.
func (l *LoopFence) IntersectsCell(c s2.Cell) bool {
// if any of the cell's vertices is contained by the loop
// they intersect
for i := 0; i < 4; i++ {
v := c.Vertex(i)
if l.ContainsPoint(v) {
return true
}
}
// missing case from the above implementation
// where the loop is fully contained by the cell
for _, v := range l.Vertices() {
if c.ContainsPoint(v) {
return true
}
}
return false
}