intersection method

bool intersection(
  1. Rect rect
)

Computes the intersection of this rectangle and the rectangle parameter. If there is no intersection, returns false and leaves this rectangle as is. @param {goog.math.Rect} rect A Rectangle. @return {bool} True iff this rectangle intersects with the parameter.

Implementation

bool intersection(Rect rect)
{
  double x0 = Math.max(this.left, rect.left);
  double x1 = Math.min(this.left + this.width, rect.left + rect.width);

  if (x0 <= x1) {
    double y0 = Math.max(this.top, rect.top);
    double y1 = Math.min(this.top + this.height, rect.top + rect.height);

    if (y0 <= y1) {
      this.left = x0;
      this.top = y0;
      this.width = x1 - x0;
      this.height = y1 - y0;

      return true;
    }
  }
  return false;
}