Rectangle Overlap

Updated on 08 July, 2025
Rectangle Overlap header image

Problem Statement

In this problem, we deal with axis-aligned rectangles, which are defined by the coordinates of their bottom-left and top-right corners. Each rectangle can be described using a list structure [x1, y1, x2, y2]. These rectangles have sides that are parallel to the X and Y axes, making the detection of overlap between two rectangles a problem of comparing their x and y coordinates.

Two rectangles overlap if any part of their area overlaps, meaning their intersection forms another rectangle with a positive area. Rectangles that just touch at the edges or corners are not considered overlapping as the intersecting area in those cases is zero.

Given two rectangles, denoted by rec1 and rec2, the task is to establish whether these rectangles overlap. The function should return true if there is any overlapping area, and false otherwise.

Examples

Example 1

Input:

rec1 = [0,0,2,2], rec2 = [1,1,3,3]

Output:

true

Example 2

Input:

rec1 = [0,0,1,1], rec2 = [1,0,2,1]

Output:

false

Example 3

Input:

rec1 = [0,0,1,1], rec2 = [2,2,3,3]

Output:

false

Constraints

  • rec1.length == 4
  • rec2.length == 4
  • -109 <= rec1[i], rec2[i] <= 109
  • rec1 and rec2 represent a valid rectangle with a non-zero area.

Approach and Intuition

To determine whether two rectangles overlap, one can follow a logical approach based on the coordinates:

  1. Consider Rectangle 1 (rec1) with coordinates [x1, y1, x2, y2] and Rectangle 2 (rec2) with [x3, y3, x4, y4].
  2. An overlap occurs if the following conditions are satisfied:
    • The left side of rec1 is to the left of the right side of rec2, and the right side of rec1 is to the right of the left side of rec2. Mathematically: x1 < x4 && x2 > x3
    • The bottom side of rec1 is below the top side of rec2, and the top side of rec1 is above the bottom side of rec2. Mathematically: y1 < y4 && y2 > y3
  3. If either of these conditions fails, the rectangles do not overlap.

Example Interpretations:

  • Example 1:

    • rec1 = [0,0,2,2]
    • rec2 = [1,1,3,3]
    • Both conditions are satisfied. There is a clear overlapping area between these two rectangles, thus the output is true.
  • Example 2:

    • rec1 = [0,0,1,1]
    • rec2 = [1,0,2,1]
    • The right edge of rec1 touches the left edge of rec2 but there's no positive intersection area (they only touch at the edge), and hence the output is false.
  • Example 3:

    • rec1 = [0,0,1,1]
    • rec2 = [2,2,3,3]
    • There's no intersection as one rectangle is entirely to the right of another; condition one fails (right side of rec1 is not to the right of the left side of rec2), resulting in false.

The constraints ensure that rectangle dimensions are valid and the range is sufficiently broad, encompassing possible coordinate values from -10^9 to 10^9, allowing the solution to handle edge cases of very large and very small rectangles. The approach should always handle these valid conditions, taking note that all input rectangles are valid and have non-zero area.

Solutions

  • Java
java
class Solution {
    public boolean rectanglesOverlap(int[] rectangle1, int[] rectangle2) {
        return (Math.min(rectangle1[2], rectangle2[2]) > Math.max(rectangle1[0], rectangle2[0]) &&
                Math.min(rectangle1[3], rectangle2[3]) > Math.max(rectangle1[1], rectangle2[1]));
    }
}

The provided Java solution determines whether two rectangles overlap on a 2D plane. Each rectangle's position and dimensions are represented by an array. The method rectanglesOverlap accepts two arrays: rectangle1 and rectangle2. Each array consists of four integers where:

  • The first integer ([0]) represents the x-coordinate of the left edge.
  • The second integer ([1]) symbolizes the y-coordinate of the bottom edge.
  • The third integer ([2]) denotes the x-coordinate of the right edge.
  • The fourth integer ([3]) signifies the y-coordinate of the top edge.

The solution employs mathematical comparisons to decide overlap:

  • It checks if the rightmost left edge is to the left of the leftmost right edge.
  • It verifies that the topmost bottom edge is below the bottommost top edge.

If both conditions are true, then the rectangles overlap; otherwise, they do not. This method efficiently assesses the spacial relationship between two rectangles with a constant time complexity, making it optimal for real-time applications where rapid computation is crucial.

Comments

No comments yet.