intersects method

bool intersects(
  1. Geometry g
)

Tests whether this geometry intersects the argument geometry.

The intersects predicate has the following equivalent definitions:

  • The two geometries have at least one point in common
  • The DE-9IM Intersection Matrix for the two geometries matches at least one of the patterns
    • [T********]
    • [*T*******]
    • [***T*****]
    • [****T****]
  • ! g.disjoint(this) = true
    (intersects is the inverse of disjoint)

@param g the Geometry with which to compare this Geometry @return true if the two Geometrys intersect

@see Geometry#disjoint

Implementation

bool intersects(Geometry g) {
  // short-circuit envelope test
  if (!getEnvelopeInternal().intersectsEnvelope(g.getEnvelopeInternal()))
    return false;

  /**
   * TODO: (MD) Add optimizations:
   *
   * - for P-A case:
   * If P is in env(A), test for point-in-poly
   *
   * - for A-A case:
   * If env(A1).overlaps(env(A2))
   * test for overlaps via point-in-poly first (both ways)
   * Possibly optimize selection of point to test by finding point of A1
   * closest to centre of env(A2).
   * (Is there a test where we shouldn't bother - e.g. if env A
   * is much smaller than env B, maybe there's no point in testing
   * pt(B) in env(A)?
   */

  // optimization for rectangle arguments
  if (isRectangle()) {
    return RectangleIntersects.intersectsStatic(this as Polygon, g);
  }
  if (g.isRectangle()) {
    return RectangleIntersects.intersectsStatic(g as Polygon, this);
  }
  if (isGeometryCollection() || g.isGeometryCollection()) {
    for (int i = 0; i < getNumGeometries(); i++) {
      for (int j = 0; j < g.getNumGeometries(); j++) {
        if (getGeometryN(i).intersects(g.getGeometryN(j))) {
          return true;
        }
      }
    }
    return false;
  }
  // general case
  return relate(g).isIntersects();
}