Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
/ geo Public archive
forked from golang/geo

Commit

Permalink
Apply loop region patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Sargeant committed Jan 17, 2017
1 parent f819552 commit 934b2e6
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions s2/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ func LoopFromCell(c Cell) *Loop {
return l
}

// LoopFromDegrees constructs a loop from the given lat/lng degree pairs.
func LoopFromDegrees(degs ...[]float64) *Loop {
pts := make([]Point, len(degs))
for i, d := range degs {
ll := LatLngFromDegrees(d[0], d[1])
pts[i] = PointFromLatLng(ll)
}
return LoopFromPoints(pts)
}

// EmptyLoop returns a special "empty" loop.
func EmptyLoop() *Loop {
return LoopFromPoints([]Point{{r3.Vector{X: 0, Y: 0, Z: 1}}})
Expand Down Expand Up @@ -259,6 +269,35 @@ func (l Loop) CapBound() Cap {
return l.bound.CapBound()
}

// ContainsCell checks whether the cell is completely enclosed by this loop.
// Does not count for loop interior and uses raycasting.
func (l Loop) ContainsCell(c 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 Loop) IntersectsCell(c Cell) bool {
for i := 0; i < 4; i++ {
crosser := NewChainEdgeCrosser(c.Vertex(i), c.Vertex((i+1)%4), l.Vertex(0))
for _, v := range l.Vertices()[1:] {
if crosser.EdgeOrVertexChainCrossing(v) {
return true
}
}
if crosser.EdgeOrVertexChainCrossing(l.Vertex(0)) { //close the loop
return true
}
}
return l.ContainsCell(c)
}

// Vertex returns the vertex for the given index. For convenience, the vertex indices
// wrap automatically for methods that do index math such as Edge.
// i.e., Vertex(NumEdges() + n) is the same as Vertex(n).
Expand Down

0 comments on commit 934b2e6

Please sign in to comment.