# Check if a rectangle overlaps another one

Two rectangles overlap when they overlap on both axes.

Canonical: https://1loc.dev/math/check-if-a-rectangle-overlaps-other-one/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function overlaps({ a, b }) { return a.x1 < b.x2 && b.x1 < a.x2 && a.y1 < b.y2 && b.y1 < a.y2; }
```

## Input

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

## Expected output

```json
true
```

## Edge case

Input:
```json
{
  "a": {
    "x1": 0,
    "y1": 0,
    "x2": 4,
    "y2": 4
  },
  "b": {
    "x1": 5,
    "y1": 0,
    "x2": 6,
    "y2": 4
  }
}
```
Output:
```json
false
```

## 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 lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/check-if-a-rectangle-overlaps-other-one.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: 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.
