- 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
Total area of two overlapping rectangles
An overlapping area is an area that is shared by two objects. In the case of rectangles, it is the area of the rectangles that belong to both rectangles. In order to find the total areas of two overlapping rectangles, first er need to add the area of both rectangles respectively but in this total, the overlapping area is counted twice. Thus we need to subtract the overlapping area too.
Problem Statement
Given the bottom left and top right vertices of two rectangles. Find the total area covered by the two rectangles.
Sample Example 1
Input
bl_x1 = 0 bl_y1 = 0 tr_x1 = 5 tr_y1 = 5 bl_x2 = 3 bl_y2 = 3 tr_x2 = 8 tr_y2 = 8
Output
46
Explanation
Area of rectangle1 = (5 - 0) * (5 - 0) = 25 Area of rectangle2 = (8 - 3) * (8 - 3) = 25 Overlapping area = 4 Total area = 25 + 25 - 4 = 46
Sample Example 2
Input
bl_x1 = -5 bl_y1 = -5 tr_x1 = 2 tr_y1 = 2 bl_x2 = 4 bl_y2 = 4 tr_x2 = 2 tr_y2 = 2
Output
53
Explanation
Area of rectangle1 = (-5 - 2) * (-5 - 2) = 49 Area of rectangle2 = (4 - 2) * (4 - 2) = 4 Overlapping area = 0 Total area = 49 + 4 - 0 =53
Approach 1: Brute Force Approach
In this approach, we find the area of two rectangles and the overlap area separately. The area of two rectangles can be found by the coordinates given by the rectangle. For finding the overlap area, we find the intersection points of the overlap area and then calculate the area.
Pseudocode
procedure overlapArea (rect1[], rect2[]) x = max(0, min(rect1[2], rect2[2]) - max(rect1[0], rect2[0])) y = max(0, min(rect1[3], rect2[3]) - max(rect1[1], rect2[1])) ans = x * y end procedure procedure totalArea (rect1[], rect2[]) area1 = rect1[2] - rect1[0]) * abs(rect1[3] - rect1[1] area2 = rect2[2] - rect2[0]) * abs(rect2[3] - rect2[1] overlap = overlapArea(rect1, rect2) ans = area1 + area2 - overlap end procedure
Example: C++ Implementation
In the following program, the area of rectangles and overlap area is found to get the total area.
#include <bits/stdc++.h> using namespace std; // Function to calculate the overlapping area int overlapArea(int rect1[], int rect2[]){ //Finding the length and width of the overlap area int x = max(0, min(rect1[2], rect2[2]) - max(rect1[0], rect2[0])); int y = max(0, min(rect1[3], rect2[3]) - max(rect1[1], rect2[1])); int area = x * y; return area; } // Function to calculate the total area of two rectangles int totalArea(int rect1[], int rect2[]){ // Area of rectangle 1 int area1 = abs(rect1[2] - rect1[0]) * abs(rect1[3] - rect1[1]); // Area of rectangle 2 int area2 = abs(rect2[2] - rect2[0]) * abs(rect2[3] - rect2[1]); int overlap = overlapArea(rect1, rect2); // Total area is the area of two rectangles minus the common overlap area int total = area1 + area2 - overlap; return total; } int main(){ int rect1[4] = {0, 0, 5, 5}, rect2[4] = {3, 3, 8, 8}; cout << "Total area = " << totalArea(rect1, rect2); return 0; }
Output
Total area = 46
Approach 2: Confirm Overlap
In order to reduce the computation effort in case the rectangles are not overlapping, we can first confirm if the rectangles are overlapping and then calculate the overlap area.
Pseudocode
procedure overlapArea (rect1[], rect2[]) x = max(0, min(rect1[2], rect2[2]) - max(rect1[0], rect2[0])) y = max(0, min(rect1[3], rect2[3]) - max(rect1[1], rect2[1])) ans = x * y end procedure procedure totalArea (rect1[], rect2[]) area1 = rect1[2] - rect1[0]) * abs(rect1[3] - rect1[1] area2 = rect2[2] - rect2[0]) * abs(rect2[3] - rect2[1] if no overlap ans = area1 + area2 else overlap = overlapArea(rect1, rect2) ans = area1 + area2 - overlap end procedure
Example: C++ Implementation
In the following program, we check if the rectangles are overlapping first and then compute the overlap area accordingly.
#include <bits/stdc++.h> using namespace std; // Function to calculate the overlapping area int overlapArea(int rect1[], int rect2[]){ //Finding the length and width of overlap area int x = max(0, min(rect1[2], rect2[2]) - max(rect1[0], rect2[0])); int y = max(0, min(rect1[3], rect2[3]) - max(rect1[1], rect2[1])); int area = x * y; return area; } int totalArea(int rect1[], int rect2[]){ int area1 = abs(rect1[2] - rect1[0]) * abs(rect1[3] - rect1[1]); int area2 = abs(rect2[2] - rect2[0]) * abs(rect2[3] - rect2[1]); // Checking for overlap if (rect1[0] > rect2[2] || rect2[0] > rect1[2] || rect1[1] > rect2[3] || rect2[1] > rect1[3]) { // No overlap return area1 + area2; } else { // Overlap int overlap = overlapArea(rect1, rect2); return area1 + area2 - overlap; } } int main(){ int rect1[4] = {0, 0, 5, 5}, rect2[4] = {6, 6, 8, 8}; cout << "Total area = " << totalArea(rect1, rect2); return 0; }
Output
Total area = 29
Approach 3: Object-Oriented Program
In this approach, we represent rectangles as objects and calculate their area and overlapping areas as methods for these objects. This approach makes the code more readable, reusable and efficient.
Pseudocode
Define Rectangle Class Define Rectangle Define area() Define overlapArea() Define totalArea()
Example: C++ Implementation
In the following program, we use the concept of OOPs to create Rectangle class and its methods.
#include <bits/stdc++.h> using namespace std; class Rectangle{ public: int x1, y1, x2, y2; Rectangle(int x1, int y1, int x2, int y2){ this->x1 = x1; this->y1 = y1; this->x2 = x2; this->y2 = y2; } int area(){ return abs(x2 - x1) * abs(y2 - y1); } int overlapArea(Rectangle second){ int x = max(0, min(x2, second.x2) - max(x1, second.x1)); int y = max(0, min(y2, second.y2) - max(y1, second.y1)); int area = x * y; return area; } int totalArea(Rectangle second){ int overlap = overlapArea(second); int total = area() + second.area() - overlap; return total; } }; int main(){ Rectangle rect1(1, 1, 5, 5); Rectangle rect2(3, 3, 6, 6); std::cout << "Total area = " << rect1.totalArea(rect2); return 0; }
Output
Total area = 21
Conclusion
In conclusion, for finding the total area of overlapping rectangles we discussed brute force and some other optimised approaches to enhancing user-friendliness. Each approach had a time and space complexity of O(1).