Given two overlapping rectangles on a plane. We are given bottom left and top right points of the two rectangles. We need to find the total area (Green and pink areas in the below diagram).

**Examples:**

Input :Point l1 = {2, 2}, r1 = {5, 7}; Point l2 = {3, 4}, r2 = {6, 9};Output :Total Area = 24Input :Point l1 = {2, 1}, r1 = {5, 5}; Point l2 = {3, 2}, r2 = {5, 7};Output :Total Area = 16

Asked in Juniper

We basically add areas of two rectangles. This includes the intersecting part twice, so we subtract the area of intersecting part.

Total Area =(Area of 1st rectangle + Area of 2nd rectangle) - Area of Intersecting part

Area of Rectangle = x_distance * y_distance

Where,

x_distance for 1st rectangle = abs(l1.x – r1.x)

y_distance for 1st rectangle = abs(l1.y – r1.y)

Similarly, we can compute area of 2nd rectangle.

For area of intersecting part,

x_distance for intersecting rectangle = min(r1.x, r2.x) – max(l1.x, l2.x)

y_distance for 1st rectangle = min(r1.y, r2.y) – max(l1.y, l2.y)

Below is the implementation of the above approach:

## C++

`// C++ program to find total area of two` `// overlapping Rectangles` `#include <bits/stdc++.h>` `using` `namespace` `std;` `struct` `Point {` ` ` `int` `x, y;` `};` `// Returns Total Area of two overlap` `// rectangles` `int` `overlappingArea(Point l1, Point r1, Point l2, Point r2)` `{` ` ` `// Area of 1st Rectangle` ` ` `int` `area1 = ` `abs` `(l1.x - r1.x) * ` `abs` `(l1.y - r1.y);` ` ` `// Area of 2nd Rectangle` ` ` `int` `area2 = ` `abs` `(l2.x - r2.x) * ` `abs` `(l2.y - r2.y);` ` ` `// Length of intersecting part i.e` ` ` `// start from max(l1.x, l2.x) of` ` ` `// x-coordinate and end at min(r1.x,` ` ` `// r2.x) x-coordinate by subtracting` ` ` `// start from end we get required` ` ` `// lengths` ` ` `int` `areaI = (min(r1.x, r2.x) - max(l1.x, l2.x))` ` ` `* (min(r1.y, r2.y) - max(l1.y, l2.y));` ` ` `return` `(area1 + area2 - areaI);` `}` `// Driver's Code` `int` `main()` `{` ` ` `Point l1 = { 2, 2 }, r1 = { 5, 7 };` ` ` `Point l2 = { 3, 4 }, r2 = { 6, 9 };` ` ` ` ` `// Function Call` ` ` `cout << overlappingArea(l1, r1, l2, r2);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to find total area of two` `// overlapping Rectangles` `class` `GFG {` ` ` `static` `class` `Point {` ` ` `int` `x, y;` ` ` `public` `Point(` `int` `x, ` `int` `y)` ` ` `{` ` ` `this` `.x = x;` ` ` `this` `.y = y;` ` ` `}` ` ` `};` ` ` `// Returns Total Area of two overlap` ` ` `// rectangles` ` ` `static` `int` `overlappingArea(Point l1, Point r1, Point l2,` ` ` `Point r2)` ` ` `{` ` ` `// Area of 1st Rectangle` ` ` `int` `area1` ` ` `= Math.abs(l1.x - r1.x) * Math.abs(l1.y - r1.y);` ` ` `// Area of 2nd Rectangle` ` ` `int` `area2` ` ` `= Math.abs(l2.x - r2.x) * Math.abs(l2.y - r2.y);` ` ` `// Length of intersecting part i.e` ` ` `// start from max(l1.x, l2.x) of` ` ` `// x-coordinate and end at min(r1.x,` ` ` `// r2.x) x-coordinate by subtracting` ` ` `// start from end we get required` ` ` `// lengths` ` ` `int` `areaI` ` ` `= (Math.min(r1.x, r2.x) - Math.max(l1.x, l2.x))` ` ` `* (Math.min(r1.y, r2.y)` ` ` `- Math.max(l1.y, l2.y));` ` ` `return` `(area1 + area2 - areaI);` ` ` `}` ` ` `// Driver Code` ` ` `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` `);` ` ` ` ` `// Function Call` ` ` `System.out.println(overlappingArea(l1, r1, l2, r2));` ` ` `}` `}` `// This code is contributed by PrinciRaj1992` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find total area of two` `# overlapping Rectangles` `# Returns Total Area of two overlap` `# rectangles` `def` `overlappingArea(l1, r1, l2, r2):` ` ` `x ` `=` `0` ` ` `y ` `=` `1` ` ` ` ` `# Area of 1st Rectangle` ` ` `area1 ` `=` `abs` `(l1[x] ` `-` `r1[x]) ` `*` `abs` `(l1[y] ` `-` `r1[y])` ` ` `# Area of 2nd Rectangle` ` ` `area2 ` `=` `abs` `(l2[x] ` `-` `r2[x]) ` `*` `abs` `(l2[y] ` `-` `r2[y])` ` ` `''' Length of intersecting part i.e ` ` ` `start from max(l1[x], l2[x]) of ` ` ` `x-coordinate and end at min(r1[x], ` ` ` `r2[x]) x-coordinate by subtracting ` ` ` `start from end we get required ` ` ` `lengths '''` ` ` `areaI ` `=` `((` `min` `(r1[x], r2[x]) ` `-` ` ` `max` `(l1[x], l2[x])) ` `*` ` ` `(` `min` `(r1[y], r2[y]) ` `-` ` ` `max` `(l1[y], l2[y])))` ` ` `return` `(area1 ` `+` `area2 ` `-` `areaI)` `# Driver's Code` `l1 ` `=` `[` `2` `, ` `2` `]` `r1 ` `=` `[` `5` `, ` `7` `]` `l2 ` `=` `[` `3` `, ` `4` `]` `r2 ` `=` `[` `6` `, ` `9` `]` `# Function call` `print` `(overlappingArea(l1, r1, l2, r2))` `# This code is contributed by Manisha_Ediga` |

*chevron_right*

*filter_none*

## C#

`// C# program to find total area of two` `// overlapping Rectangles` `using` `System;` `class` `GFG {` ` ` `public` `class` `Point {` ` ` `public` `int` `x, y;` ` ` `public` `Point(` `int` `x, ` `int` `y)` ` ` `{` ` ` `this` `.x = x;` ` ` `this` `.y = y;` ` ` `}` ` ` `};` ` ` `// Returns Total Area of two overlap` ` ` `// rectangles` ` ` `static` `int` `overlappingArea(Point l1, Point r1, Point l2,` ` ` `Point r2)` ` ` `{` ` ` `// Area of 1st Rectangle` ` ` `int` `area1` ` ` `= Math.Abs(l1.x - r1.x) * Math.Abs(l1.y - r1.y);` ` ` `// Area of 2nd Rectangle` ` ` `int` `area2` ` ` `= Math.Abs(l2.x - r2.x) * Math.Abs(l2.y - r2.y);` ` ` `// Length of intersecting part i.e` ` ` `// start from max(l1.x, l2.x) of` ` ` `// x-coordinate and end at min(r1.x,` ` ` `// r2.x) x-coordinate by subtracting` ` ` `// start from end we get required` ` ` `// lengths` ` ` `int` `areaI` ` ` `= (Math.Min(r1.x, r2.x) - Math.Max(l1.x, l2.x))` ` ` `* (Math.Min(r1.y, r2.y)` ` ` `- Math.Max(l1.y, l2.y));` ` ` `return` `(area1 + area2 - areaI);` ` ` `}` ` ` `// Driver Code` ` ` `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);` ` ` ` ` `// Function Call` ` ` `Console.WriteLine(overlappingArea(l1, r1, l2, r2));` ` ` `}` `}` `// This code is contributed by PrinciRaj1992` |

*chevron_right*

*filter_none*

**Output**

24

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum area of square holding two identical rectangles
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Calculate Volume, Curved Surface Area and Total Surface Area Of Cylinder
- Total number of unit cells covered by all given Rectangles
- Count of Rectangles with area K made up of only 1s from given Binary Arrays
- Find if two rectangles overlap
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Number of unique rectangles formed using N unit squares
- Number of rectangles in N*M grid
- Sum of Areas of Rectangles possible for an array
- Number of rectangles in a circle of radius R
- Smallest square formed with given rectangles
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths
- Count the number of rectangles such that ratio of sides lies in the range [a,b]
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Count of distinct rectangles inscribed in an equilateral triangle
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Missing vertex among N axis-parallel rectangles

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.