# 836. Rectangle Overlap

A rectangle is represented as a list `[x1, y1, x2, y2]`, where `(x1, y1)` are the coordinates of its bottom-left corner, and `(x2, y2)` are the coordinates of its top-right corner.

Two rectangles overlap if the area of their intersection is positive.  To be clear, two rectangles that only touch at the corner or edges do not overlap.

Given two (axis-aligned) rectangles, return whether they overlap.

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
``````

Notes:

1. Both rectangles `rec1` and `rec2` are lists of 4 integers.
2. All coordinates in rectangles will be between `-10^9 `and` 10^9`.

``````           x3             x4
|--------------|
|--------------|
x1             x2
``````

x1 < x3 < x2 && x3 < x2 < x4

x1 < x4 && x3 < x2

``````           x1             x2
|--------------|
|--------------|
x3             x4
``````

x3 < x2 && x1 < x4

1）两个矩形在矩形1的右上角重叠：

``````           ____________________x4,y4
|                   |
_______|______x2,y2        |
|       |______|____________|
|      x3,y3   |
|______________|
x1,y1
``````

2）两个矩形在矩形1的左上角重叠：

``````   ___________________  x4,y4
|                   |
|            _______|____________x2,y2
|___________|_______|           |
x3,y3         |                   |
|___________________|
x1,y1
``````

3）两个矩形在矩形1的左下角重叠：

``````           ____________________x2,y2
|                   |
_______|______x4,y4        |
|       |______|____________|
|      x1,y1   |
|______________|
x3,y3
``````

4）两个矩形在矩形1的右下角重叠：

``````   ___________________  x2,y2
|                   |
|            _______|____________x4,y4
|___________|_______|           |
x1,y1         |                   |
|___________________|
x3,y3
``````

``````class Solution {
public:
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3];
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/836

Rectangle Area

https://leetcode.com/problems/rectangle-overlap/

https://leetcode.com/problems/rectangle-overlap/discuss/132319/My-One-Line-C%2B%2B-Solution

https://leetcode.com/problems/rectangle-overlap/discuss/133175/C%2B%2B-Solution-with-easy-explanation

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation