From 6fb8b3651a620f4c427eb0f85763fb1a422a239e Mon Sep 17 00:00:00 2001 From: Daniel Cheng Date: Tue, 9 Jun 2015 11:14:26 +0800 Subject: [PATCH] Implements Region for Polyline --- s2/polyline.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/s2/polyline.go b/s2/polyline.go index 39d8e1c..770e0f1 100644 --- a/s2/polyline.go +++ b/s2/polyline.go @@ -17,3 +17,49 @@ func (pl *Polyline) initPoints(vertices []Point) { func (pl Polyline) NumVertices() int { return len(pl.vertices) } func (pl Polyline) Vertex(k int) Point { return pl.vertices[k] } + +func (pl *Polyline) GetRectBound() *Rect { + rb := NewRectBounder() + for _, pt := range pl.vertices { + rb.AddPoint(&pt) + } + bound := rb.Bound() + return &bound +} + +func (pl *Polyline) MayIntersect(cell Cell) bool { + if pl.NumVertices() == 0 { + return false + } + + for _, pt := range pl.vertices { + if cell.ContainsPoint(pt) { + return true + } + } + + cellVertices := []Point{ + cell.Vertex(0), + cell.Vertex(1), + cell.Vertex(2), + cell.Vertex(3), + } + + for j := 0; j < 4; j++ { + crosser := NewEdgeCrosser(&cellVertices[j], &cellVertices[(j+1)&3], &pl.vertices[0]) + for _, vetex := range pl.vertices { + if crosser.RobustCrossing(&vetex) >= 0 { + return true + } + } + } + + return false +} + +func (pl *Polyline) ContainsCell(cell Cell) bool { return false } +func (pl *Polyline) ContainsPoint(p Point) bool { return false } + +func (pl *Polyline) CapBound() Cap { + return pl.GetRectBound().CapBound() +}