MCPcopy
hub / github.com/golang/geo / IntersectsCell

Method IntersectsCell

s2/rect.go:369–430  ·  view source on GitHub ↗

IntersectsCell reports whether this rectangle intersects the given cell. This is an exact test and may be fairly expensive.

(c Cell)

Source from the content-addressed store, hash-verified

367// IntersectsCell reports whether this rectangle intersects the given cell. This is an
368// exact test and may be fairly expensive.
369func (r Rect) IntersectsCell(c Cell) bool {
370 // First we eliminate the cases where one region completely contains the
371 // other. Once these are disposed of, then the regions will intersect
372 // if and only if their boundaries intersect.
373 if r.IsEmpty() {
374 return false
375 }
376 if r.ContainsPoint(Point{c.id.rawPoint()}) {
377 return true
378 }
379 if c.ContainsPoint(PointFromLatLng(r.Center())) {
380 return true
381 }
382
383 // Quick rejection test (not required for correctness).
384 if !r.Intersects(c.RectBound()) {
385 return false
386 }
387
388 // Precompute the cell vertices as points and latitude-longitudes. We also
389 // check whether the Cell contains any corner of the rectangle, or
390 // vice-versa, since the edge-crossing tests only check the edge interiors.
391 vertices := [4]Point{}
392 latlngs := [4]LatLng{}
393
394 for i := range vertices {
395 vertices[i] = c.Vertex(i)
396 latlngs[i] = LatLngFromPoint(vertices[i])
397 if r.ContainsLatLng(latlngs[i]) {
398 return true
399 }
400 if c.ContainsPoint(PointFromLatLng(r.Vertex(i))) {
401 return true
402 }
403 }
404
405 // Now check whether the boundaries intersect. Unfortunately, a
406 // latitude-longitude rectangle does not have straight edges: two edges
407 // are curved, and at least one of them is concave.
408 for i := range vertices {
409 edgeLng := s1.IntervalFromEndpoints(latlngs[i].Lng.Radians(), latlngs[(i+1)&3].Lng.Radians())
410 if !r.Lng.Intersects(edgeLng) {
411 continue
412 }
413
414 a := vertices[i]
415 b := vertices[(i+1)&3]
416 if edgeLng.Contains(r.Lng.Lo) && intersectsLngEdge(a, b, r.Lat, s1.Angle(r.Lng.Lo)) {
417 return true
418 }
419 if edgeLng.Contains(r.Lng.Hi) && intersectsLngEdge(a, b, r.Lat, s1.Angle(r.Lng.Hi)) {
420 return true
421 }
422 if intersectsLatEdge(a, b, s1.Angle(r.Lat.Lo), r.Lng) {
423 return true
424 }
425 if intersectsLatEdge(a, b, s1.Angle(r.Lat.Hi), r.Lng) {
426 return true

Callers

nothing calls this directly

Calls 15

IsEmptyMethod · 0.95
ContainsPointMethod · 0.95
CenterMethod · 0.95
IntersectsMethod · 0.95
VertexMethod · 0.95
ContainsLatLngMethod · 0.95
IntervalFromEndpointsFunction · 0.92
AngleTypeAlias · 0.92
PointFromLatLngFunction · 0.85
LatLngFromPointFunction · 0.85
intersectsLngEdgeFunction · 0.85
intersectsLatEdgeFunction · 0.85

Tested by

no test coverage detected