Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Program to find total area covered by two rectangles in Python
Suppose we want to find the total area covered by two rectilinear rectangles in a 2D plane. Here each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

To solve this, we will follow these steps −
- width_1 := |C-A|, height_1 := |D-B|
- width_2 := |G-E|, height_2 := |H-F|
- area := width_1*height_1 + width_2*height_2
- if (GC) or (F>D) or (H
- return area
- p := maximum of A, E
- q := maximum of B, F
- r := minimum of C, G
- s := minimum of D, H
- width_3 := |r-p|
- height_3 := |s-q|
- return area - (width_3*height_3)
Example
Let us see the following implementation to get better understanding −
def solve(A, B, C, D, E, F, G, H): width_1 = abs(C-A) height_1 = abs(D-B) width_2 = abs(G-E) height_2 = abs(H-F) area = width_1*height_1 + width_2*height_2 if (GC) or (F>D) or (HInput
-3, 0, 3, 4, 0, -1, 9, 2Output
45
Advertisements
