distanceTo method

int distanceTo(
  1. Rect other
)

Returns the distance between this Rect and other. This is minimum length that a corridor would have to be to go from one Rect to the other. If the two Rects are adjacent, returns zero. If they overlap, returns -1.

Implementation

int distanceTo(Rect other) {
  var vertical = switch (null) {
    _ when top >= other.bottom => top - other.bottom,
    _ when bottom <= other.top => other.top - bottom,
    _ => -1
  };

  var horizontal = switch (null) {
    _ when left >= other.right => left - other.right,
    _ when right <= other.left => other.left - right,
    _ => -1,
  };

  if (vertical == -1 && horizontal == -1) return -1;
  if (vertical == -1) return horizontal;
  if (horizontal == -1) return vertical;
  return horizontal + vertical;
}