Check if a rectangle overlaps another one

Two rectangles overlap when they overlap on both axes.

JavaScript Version 1.0.0 · MIT · .md

check-if-a-rectangle-overlaps-other-one.js
function overlaps({ a, b }) { return a.x1 < b.x2 && b.x1 < a.x2 && a.y1 < b.y2 && b.y1 < a.y2; }

Input & expected output

Input

{
  "a": {
    "x1": 0,
    "y1": 0,
    "x2": 4,
    "y2": 4
  },
  "b": {
    "x1": 2,
    "y1": 2,
    "x2": 6,
    "y2": 6
  }
}

Expected output

true

The edge case

Input

{
  "a": {
    "x1": 0,
    "y1": 0,
    "x2": 4,
    "y2": 4
  },
  "b": {
    "x1": 5,
    "y1": 0,
    "x2": 6,
    "y2": 4
  }
}

Expected output

false

Know the limits

  • The original combined the axes with ||, so rectangles side by side at different heights counted as overlapping. Both axes must overlap.
  • Touching edges do not overlap.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Check if a rectangle overlaps other one from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Requires overlap on both axes; the original combined the axes with ||, reporting side-by-side rectangles as overlapping. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.