- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose there is a rectangle that is represented as a list [x1, y1, x2, y2], where (x1, y1) is the coordinates of its bottom-left corner, and (x2, y2) is the coordinates of its top-right corner. Now two rectangles overlap if the area of their intersection is positive. So, we can understand that two rectangles that only touch at the corner or edges do not overlap.

If we have two (axis-aligned) rectangles, we have to check whether they overlap or not.

So, if the input is like R1 = [0,0,2,2], R2 = [1,1,3,3], then the output will be True.

To solve this, we will follow these steps −

- if R1[0]>=R2[2] or R1[2]<=R2[0] or R1[3]<=R2[1] or R1[1]>=R2[3], then
- return False

- otherwise,
- return True

Let us see the following implementation to get better understanding −

class Solution: def isRectangleOverlap(self, R1, R2): if (R1[0]>=R2[2]) or (R1[2]<=R2[0]) or (R1[3]<=R2[1]) or (R1[1]>=R2[3]): return False else: return True ob = Solution() print(ob.isRectangleOverlap([0,0,2,2],[1,1,3,3]))

[0,0,2,2],[1,1,3,3]

True

- Related Questions & Answers
- How do you overlap widgets/frames in Python tkinter?
- Python Pandas - Check whether two Interval objects overlap
- Largest Rectangle in Histogram in Python
- Overlap Elements with CSS
- 8085 Block movement without overlap
- 8085 Block movement with overlap
- Block movement without overlap in Z-80
- Python Pandas IntervalIndex - Check if Intervals that share closed endpoints overlap
- Rectangle Area in C++
- Maximal Rectangle in C++
- Perfect Rectangle in C++
- Python Pandas IntervalArray - Check Intervals that share closed endpoints overlap or not
- Python Pandas - Check whether two Interval objects that share closed endpoints overlap
- Find if two rectangles overlap using C++.
- Program to find largest rectangle area under histogram in python

Advertisements