@@ -39,33 +39,36 @@ func polylineIntersectsPoint(pls []*s2.Polyline,
39
39
return false
40
40
}
41
41
42
+ // check if any of the polyline vertices lie inside or
43
+ // on the boundary of any of the polygons. Then check if
44
+ // any of the polylines intersect with any of the edges of
45
+ // the polygons
42
46
func polylineIntersectsPolygons (pls []* s2.Polyline ,
43
47
s2pgns []* s2.Polygon ) bool {
44
- // Early exit if the polygon contains any of the line's vertices.
48
+ idx := s2 .NewShapeIndex ()
49
+ for _ , pgn := range s2pgns {
50
+ idx .Add (pgn )
51
+ }
52
+
53
+ containsQuery := s2 .NewContainsPointQuery (idx , s2 .VertexModelClosed )
45
54
for _ , pl := range pls {
46
- for i := 0 ; i < pl .NumEdges (); i ++ {
47
- edge := pl .Edge (i )
48
- for _ , s2pgn := range s2pgns {
49
- if s2pgn .IntersectsCell (s2 .CellFromPoint (edge .V0 )) ||
50
- s2pgn .IntersectsCell (s2 .CellFromPoint (edge .V1 )) {
51
- return true
52
- }
55
+ for _ , point := range * pl {
56
+ if containsQuery .Contains (point ) {
57
+ return true
53
58
}
54
59
}
55
60
}
56
61
57
62
for _ , pl := range pls {
58
63
for _ , s2pgn := range s2pgns {
59
- for i := 0 ; i < pl .NumEdges (); i ++ {
60
- for i := 0 ; i < s2pgn .NumEdges (); i ++ {
61
- edgeB := s2pgn .Edge (i )
62
- latLng1 := s2 .LatLngFromPoint (edgeB .V0 )
63
- latLng2 := s2 .LatLngFromPoint (edgeB .V1 )
64
- pl2 := s2 .PolylineFromLatLngs ([]s2.LatLng {latLng1 , latLng2 })
65
-
66
- if pl .Intersects (pl2 ) {
67
- return true
68
- }
64
+ for i := 0 ; i < s2pgn .NumEdges (); i ++ {
65
+ edgeB := s2pgn .Edge (i )
66
+ latLng1 := s2 .LatLngFromPoint (edgeB .V0 )
67
+ latLng2 := s2 .LatLngFromPoint (edgeB .V1 )
68
+ pl2 := s2 .PolylineFromLatLngs ([]s2.LatLng {latLng1 , latLng2 })
69
+
70
+ if pl .Intersects (pl2 ) {
71
+ return true
69
72
}
70
73
}
71
74
}
@@ -143,24 +146,20 @@ func rectangleIntersectsWithLineStrings(s2rect *s2.Rect,
143
146
polylines []* s2.Polyline ) bool {
144
147
// Early exit path if the envelope contains any of the linestring's vertices.
145
148
for _ , pl := range polylines {
146
- for i := 0 ; i < pl .NumEdges (); i ++ {
147
- edge := pl .Edge (i )
148
- if s2rect .IntersectsCell (s2 .CellFromPoint (edge .V0 )) ||
149
- s2rect .IntersectsCell (s2 .CellFromPoint (edge .V1 )) {
149
+ for _ , point := range * pl {
150
+ if s2rect .ContainsPoint (point ) {
150
151
return true
151
152
}
152
153
}
153
154
}
154
155
155
156
for _ , pl := range polylines {
156
- for i := 0 ; i < pl .NumEdges (); i ++ {
157
- for j := 0 ; j < 4 ; j ++ {
158
- pl2 := s2 .PolylineFromLatLngs ([]s2.LatLng {s2rect .Vertex (j ),
159
- s2rect .Vertex ((j + 1 ) % 4 )})
157
+ for i := 0 ; i < 4 ; i ++ {
158
+ pl2 := s2 .PolylineFromLatLngs ([]s2.LatLng {s2rect .Vertex (i ),
159
+ s2rect .Vertex ((i + 1 ) % 4 )})
160
160
161
- if pl .Intersects (pl2 ) {
162
- return true
163
- }
161
+ if pl .Intersects (pl2 ) {
162
+ return true
164
163
}
165
164
}
166
165
}
0 commit comments