- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check two rectangular overlaps or not in Python
Suppose we have a rectangle that is represented as a list with four elements [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. Two rectangles overlap when the area of their intersection is positive. So, two rectangles that only touch at the corner or edges do not overlap.
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 −
Example
class Solution: def solve(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.solve([0,0,3,3],[1,1,4,4]))
Input
[0,0,3,3],[1,1,4,4]
Output
True
Advertisements