intersect method

Bounds<T>? intersect(
  1. Bounds<T> b
)

Calculates the intersection of two Bounds. The return value will be null if there is no intersection. The returned bounds may be zero size (bottomLeft == topRight).

Implementation

Bounds<T>? intersect(Bounds<T> b) {
  final leftX = math.max(min.x, b.min.x);
  final rightX = math.min(max.x, b.max.x);
  final topY = math.max(min.y, b.min.y);
  final bottomY = math.min(max.y, b.max.y);

  if (leftX <= rightX && topY <= bottomY) {
    return Bounds._(Point(leftX, topY), Point(rightX, bottomY));
  }

  return null;
}