Rectangle Area - Problem
Rectangle Area Calculator - A Geometric Challenge
Imagine you're designing a floor plan application where users can place rectangular carpets on a floor. Sometimes these carpets overlap, and you need to calculate the total area covered by both carpets combined.
Given two rectangles in a 2D coordinate system, your task is to find the total area covered by both rectangles. Each rectangle is defined by:
โข Bottom-left corner: (x1, y1)
โข Top-right corner: (x2, y2)
The key challenge is handling overlapping regions - you should count overlapped areas only once!
Input: 8 integers representing coordinates of two rectangles
Output: Single integer representing the total covered area
Imagine you're designing a floor plan application where users can place rectangular carpets on a floor. Sometimes these carpets overlap, and you need to calculate the total area covered by both carpets combined.
Given two rectangles in a 2D coordinate system, your task is to find the total area covered by both rectangles. Each rectangle is defined by:
โข Bottom-left corner: (x1, y1)
โข Top-right corner: (x2, y2)
The key challenge is handling overlapping regions - you should count overlapped areas only once!
Input: 8 integers representing coordinates of two rectangles
Output: Single integer representing the total covered area
Rectangle A: (ax1, ay1) to (ax2, ay2)Rectangle B: (bx1, by1) to (bx2, by2) Input & Output
example_1.py โ Basic Overlap
$
Input:
ax1=-3, ay1=0, ax2=3, ay2=4, bx1=0, by1=-1, bx2=9, by2=2
โบ
Output:
45
๐ก Note:
Rectangle A has area (3-(-3))*(4-0) = 24. Rectangle B has area (9-0)*(2-(-1)) = 27. Overlap area is (3-0)*(2-0) = 6. Total = 24 + 27 - 6 = 45.
example_2.py โ No Overlap
$
Input:
ax1=-2, ay1=-2, ax2=2, by2=2, bx1=3, by1=3, bx2=7, by2=7
โบ
Output:
32
๐ก Note:
Rectangle A has area 4*4 = 16. Rectangle B has area 4*4 = 16. No overlap since rectangles don't intersect. Total = 16 + 16 - 0 = 32.
example_3.py โ Complete Overlap
$
Input:
ax1=0, ay1=0, ax2=4, ay2=4, bx1=1, by1=1, bx2=3, by2=3
โบ
Output:
16
๐ก Note:
Rectangle A has area 4*4 = 16. Rectangle B has area 2*2 = 4. Rectangle B is completely inside A, so overlap = 4. Total = 16 + 4 - 4 = 16.
Constraints
- -104 โค ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 โค 104
- ax1 < ax2 and ay1 < ay2 (Rectangle A is valid)
- bx1 < bx2 and by1 < by2 (Rectangle B is valid)
- All coordinates are integers
Visualization
Tap to expand
Understanding the Visualization
1
Measure Each Carpet
Calculate the area of each rectangular carpet individually using length ร width
2
Find Overlap Region
Determine where the carpets overlap by finding intersection boundaries
3
Apply Inclusion-Exclusion
Total covered area = Carpet A area + Carpet B area - Overlapped area
Key Takeaway
๐ฏ Key Insight: Use inclusion-exclusion principle to avoid double-counting overlapped areas when calculating total coverage of geometric shapes.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code