Total area of two overlapping rectangles
Given the coordinates of two rectangles in a 2D plane, the first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2) and the second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2). The task is to find the total area covered by the two rectangles.
Examples:
Input: L1= {2, 1}, R1={5, 5}, L2= {3, 2}, R2={5, 7}
Output: Total Area = 16
Explanation: In the below image we can observe that total area of two overlapping rectangles is 16 units.
Input: L1= {-3, 0}, R1={3, 5}, L2= {0, -2}, R2={6, 3}
Output: Total Area = 51
Explanation: In the below image we can observe that total area of two overlapping rectangles is 51 units.
The area of any rectangle can be calculated using the formula: (x_distance) * (y_distance). Since the rectangles may overlap, we can use Inclusion Exclusion principle to calculate the area as follows:
Total Area = Area of Rectangle1 + Area of Rectangle2 – Intersecting area of both the rectangles
For Rectangle1:
- x_distance = abs(L1.x – R1.x)
- y_distance = abs(L1.y – R1.y)
- Area of Rectangle1 = x_distance * y_distance
For Rectangle2:
- x_distance = abs(L2.x – R2.x)
- y_distance = abs(L2.y – R2.y)
- Area of Rectangle2 = x_distance * y_distance
For area of overlapping Rectangle:
- x_distance = min(R1.x, R2.x) – max(L1.x, L2.x)
- y_distance = min(R1.y, R2.y) – max(L1.y, L2.y)
- Area of overlapping rectange = x_distance * y_distance
- If the x_distance or y_distance is negative, then the two rectangles do not intersect. In that case, overlapping area is 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
};
int overlappingArea(Point l1, Point r1,
Point l2, Point r2)
{
int area1 = abs (l1.x - r1.x)
* abs (l1.y - r1.y);
int area2 = abs (l2.x - r2.x)
* abs (l2.y - r2.y);
int x_dist = min(r1.x, r2.x)
- max(l1.x, l2.x);
int y_dist = (min(r1.y, r2.y)
- max(l1.y, l2.y));
int areaI = 0;
if ( x_dist > 0 && y_dist > 0 )
{
areaI = x_dist * y_dist;
}
return (area1 + area2 - areaI);
}
int main()
{
Point l1 = { 2, 2 }, r1 = { 5, 7 };
Point l2 = { 3, 4 }, r2 = { 6, 9 };
cout << overlappingArea(l1, r1, l2, r2);
return 0;
}
|
Java
class GFG {
static class Point {
int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
};
static int overlappingArea(Point l1, Point r1,
Point l2, Point r2)
{
int area1
= Math.abs(l1.x - r1.x)
* Math.abs(l1.y - r1.y);
int area2
= Math.abs(l2.x - r2.x)
* Math.abs(l2.y - r2.y);
int x_dist = (Math.min(r1.x, r2.x)
- Math.max(l1.x, l2.x);
int y_dist = (Math.min(r1.y, r2.y)
- Math.max(l1.y, l2.y);
int areaI = 0 ;
if ( x_dist > 0 && y_dist > 0 )
{
areaI = x_dist * y_dist;
}
return (area1 + area2 - areaI);
}
public static void main(String[] args)
{
Point l1 = new Point( 2 , 2 ), r1 = new Point( 5 , 7 );
Point l2 = new Point( 3 , 4 ), r2 = new Point( 6 , 9 );
System.out.println(overlappingArea(l1, r1, l2, r2));
}
}
|
Python3
def overlappingArea(l1, r1, l2, r2):
x = 0
y = 1
area1 = abs (l1[x] - r1[x]) * abs (l1[y] - r1[y])
area2 = abs (l2[x] - r2[x]) * abs (l2[y] - r2[y])
x_dist = ( min (r1[x], r2[x]) -
max (l1[x], l2[x]))
y_dist = ( min (r1[y], r2[y]) -
max (l1[y], l2[y]))
areaI = 0
if x_dist > 0 and y_dist > 0 :
areaI = x_dist * y_dist
return (area1 + area2 - areaI)
l1 = [ 2 , 2 ]
r1 = [ 5 , 7 ]
l2 = [ 3 , 4 ]
r2 = [ 6 , 9 ]
print (overlappingArea(l1, r1, l2, r2))
|
C#
using System;
class GFG {
public class Point {
public int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
};
static int overlappingArea(Point l1, Point r1, Point l2,
Point r2)
{
int area1
= Math.Abs(l1.x - r1.x) * Math.Abs(l1.y - r1.y);
int area2
= Math.Abs(l2.x - r2.x) * Math.Abs(l2.y - r2.y);
int x_dist
= (Math.Min(r1.x, r2.x) - Math.Max(l1.x, l2.x));
int y_dist
= (Math.Min(r1.y, r2.y) - Math.Max(l1.y, l2.y));
int areaI = 0;
if (x_dist > 0 && y_dist > 0) {
areaI = x_dist * y_dist;
}
return (area1 + area2 - areaI);
}
public static void Main(String[] args)
{
Point l1 = new Point(2, 2), r1 = new Point(5, 7);
Point l2 = new Point(3, 4), r2 = new Point(6, 9);
Console.WriteLine(overlappingArea(l1, r1, l2, r2));
}
}
|
Javascript
<script>
function overlappingArea(l1, r1, l2, r2)
{
let x = 0
let y = 1
let area1 = Math.abs(l1[x] - r1[x]) * Math.abs(l1[y] - r1[y])
let area2 = Math.abs(l2[x] - r2[x]) * Math.abs(l2[y] - r2[y])
let x_dist = (Math.min(r1[x], r2[x]) -
Math.max(l1[x], l2[x]))
let y_dist = (Math.min(r1[y], r2[y]) -
Math.max(l1[y], l2[y]))
let areaI = 0
if (x_dist > 0 && y_dist > 0)
areaI = x_dist * y_dist
return (area1 + area2 - areaI)
}
let l1 = [2, 2]
let r1 = [5, 7]
let l2 = [3, 4]
let r2 = [6, 9]
document.write(overlappingArea(l1, r1, l2, r2))
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
20 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...