intersects method

bool intersects(
  1. Rectangle other
)

Checks if this rectangle intersects with another rectangle

Mathematical Operation: Separating axis theorem implementation: Two rectangles DON'T intersect if:

  1. One is completely to the left of the other OR
  2. One is completely above the other OR
  3. One is completely to the right of the other OR
  4. One is completely below the other

Implementation

bool intersects(Rectangle other) {
  return !(other.x >= x + width || // Other is right of us
      other.x + other.width <= x || // Other is left of us
      other.y >= y + height || // Other is below us
      other.y + other.height <= y); // Other is above us
}