- 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

# Find if two rectangles overlap using C++.

We know that a rectangle can be represented using two coordinates, the top left corner, and the bottom right corner. Suppose there are two rectangles, we have to check whether these two overlap or not. There are four coordinate points (l1, r1) and (l2, r2).

- l1 is the top-left corner of first rectangle
- r1 is the bottom-right corner of the first rectangle
- l2 is the top-left corner of second rectangle
- r2 is the bottom-right corner of the second rectangle

We have assumed that the rectangles are parallel to the coordinate axes. To solve this, we have to check a few conditions.

- One rectangle is above the top edge of another rectangle
- One rectangle is on the left side of the left edge of another rectangle.

## Example

#include<iostream> using namespace std; class Point { public: int x, y; }; bool isOverlapping(Point l1, Point r1, Point l2, Point r2) { if (l1.x > r2.x || l2.x > r1.x) return false; if (l1.y < r2.y || l2.y < r1.y) return false; return true; } int main() { Point l1 = {0, 10}, r1 = {10, 0}; Point l2 = {5, 5}, r2 = {15, 0}; if (isOverlapping(l1, r1, l2, r2)) cout << "Rectangles are Overlapping"; else cout << "Rectangles are not Overlapping"; }

## Output

Rectangles are Overlapping

- Related Questions & Answers
- Program to find total area covered by two rectangles in Python
- Check if any two intervals overlap among a given set of intervals in C++
- Python Pandas - Check whether two Interval objects overlap
- Find all rectangles filled with 0 in Python
- Find area of triangle if two vectors of two adjacent sides are given using C++
- Program to find out if two expression trees are equivalent using Python
- Python Pandas IntervalIndex - Check if Intervals that share closed endpoints overlap
- Overlap Elements with CSS
- Rectangle Overlap in Python
- Python Pandas - Check whether two Interval objects that share closed endpoints overlap
- Find area of parallelogram if vectors of two adjacent sides are given using C++.
- Python Pandas - Check whether two Interval objects that share an open endpoint overlap
- 8085 Block movement without overlap
- 8085 Block movement with overlap
- How can axis aligned rectangles be visualized using Python and Bokeh?

Advertisements