/** * If this method returns false, the region does not intersect the given cell. * Otherwise, either region intersects the cell, or the intersection * relationship could not be determined. */ public bool MayIntersect(S2Cell cell) { if (NumVertices == 0) { return(false); } // We only need to check whether the cell contains vertex 0 for correctness, // but these tests are cheap compared to edge crossings so we might as well // check all the vertices. for (var i = 0; i < NumVertices; ++i) { if (cell.Contains(Vertex(i))) { return(true); } } var cellVertices = new S2Point[4]; for (var i = 0; i < 4; ++i) { cellVertices[i] = cell.GetVertex(i); } for (var j = 0; j < 4; ++j) { var crosser = new EdgeCrosser(cellVertices[j], cellVertices[(j + 1) & 3], Vertex(0)); for (var i = 1; i < NumVertices; ++i) { if (crosser.RobustCrossing(Vertex(i)) >= 0) { // There is a proper crossing, or two vertices were the same. return(true); } } } return(false); }
/** * Returns true if this rectangle intersects the given cell. (This is an exact * test and may be fairly expensive, see also MayIntersect below.) */ public bool Intersects(S2Cell cell) { // First we eliminate the cases where one region completely contains the // other. Once these are disposed of, then the regions will intersect // if and only if their boundaries intersect. if (IsEmpty) { return(false); } if (Contains(cell.Center)) { return(true); } if (cell.Contains(Center.ToPoint())) { return(true); } // Quick rejection test (not required for correctness). if (!Intersects(cell.RectBound)) { return(false); } // Now check whether the boundaries intersect. Unfortunately, a // latitude-longitude rectangle does not have straight edges -- two edges // are curved, and at least one of them is concave. // Precompute the cell vertices as points and latitude-longitudes. var cellV = new S2Point[4]; var cellLl = new S2LatLng[4]; for (var i = 0; i < 4; ++i) { cellV[i] = cell.GetVertex(i); // Must be normalized. cellLl[i] = new S2LatLng(cellV[i]); if (Contains(cellLl[i])) { return(true); // Quick acceptance test. } } for (var i = 0; i < 4; ++i) { var edgeLng = S1Interval.FromPointPair( cellLl[i].Lng.Radians, cellLl[(i + 1) & 3].Lng.Radians); if (!_lng.Intersects(edgeLng)) { continue; } var a = cellV[i]; var b = cellV[(i + 1) & 3]; if (edgeLng.Contains(_lng.Lo)) { if (IntersectsLngEdge(a, b, _lat, _lng.Lo)) { return(true); } } if (edgeLng.Contains(_lng.Hi)) { if (IntersectsLngEdge(a, b, _lat, _lng.Hi)) { return(true); } } if (IntersectsLatEdge(a, b, _lat.Lo, _lng)) { return(true); } if (IntersectsLatEdge(a, b, _lat.Hi, _lng)) { return(true); } } return(false); }
// ////////////////////////////////////////////////////////////////////// // S2Region interface (see {@code S2Region} for details): /** * Return true if the cap intersects 'cell', given that the cap vertices have * alrady been checked. */ public bool Intersects(S2Cell cell, IReadOnlyList <S2Point> vertices) { // Return true if this cap intersects any point of 'cell' excluding its // vertices (which are assumed to already have been checked). // If the cap is a hemisphere or larger, the cell and the complement of the // cap are both convex. Therefore since no vertex of the cell is contained, // no other interior point of the cell is contained either. if (_height >= 1) { return(false); } // We need to check for empty caps due to the axis check just below. if (IsEmpty) { return(false); } // Optimization: return true if the cell contains the cap axis. (This // allows half of the edge checks below to be skipped.) if (cell.Contains(_axis)) { return(true); } // At this point we know that the cell does not contain the cap axis, // and the cap does not contain any cell vertex. The only way that they // can intersect is if the cap intersects the interior of some edge. var sin2Angle = _height * (2 - _height); // sin^2(capAngle) for (var k = 0; k < 4; ++k) { var edge = cell.GetEdgeRaw(k); var dot = _axis.DotProd(edge); if (dot > 0) { // The axis is in the interior half-space defined by the edge. We don't // need to consider these edges, since if the cap intersects this edge // then it also intersects the edge on the opposite side of the cell // (because we know the axis is not contained with the cell). continue; } // The Norm2() factor is necessary because "edge" is not normalized. if (dot * dot > sin2Angle * edge.Norm2) { return(false); // Entire cap is on the exterior side of this edge. } // Otherwise, the great circle containing this edge intersects // the interior of the cap. We just need to check whether the point // of closest approach occurs between the two edge endpoints. var dir = S2Point.CrossProd(edge, _axis); if (dir.DotProd(vertices[k]) < 0 && dir.DotProd(vertices[(k + 1) & 3]) > 0) { return(true); } } return(false); }