intersection method

Rectangle<T>? intersection (
  1. Rectangle<T> other
)
inherited

Computes the intersection of this and other.

The intersection of two axis-aligned rectangles, if any, is always another axis-aligned rectangle.

Returns the intersection of this and other, or null if they don't intersect.

Implementation

Rectangle<T>? intersection(Rectangle<T> other) {
  var x0 = max(left, other.left);
  var x1 = min(left + width, other.left + other.width);

  if (x0 <= x1) {
    var y0 = max(top, other.top);
    var y1 = min(top + height, other.top + other.height);

    if (y0 <= y1) {
      return Rectangle<T>(x0, y0, (x1 - x0) as T, (y1 - y0) as T);
    }
  }
  return null;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.13.0/dart-math/Rectangle/intersection.html