Skip to content

Commit bc200ae

Browse files
committed
feat(math, rectangle-area): compute area
1 parent 6b8bcc5 commit bc200ae

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

pymath/rectangle_area/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Rectangle Area
2+
3+
You are given the coordinates of two axis-aligned rectangles in a 2D plane. Your task is to calculate the total area
4+
covered by both rectangles.
5+
6+
The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ay1, ay2).
7+
8+
Similarly, the second rectangle is defined by its bottom-left corner (bx1, by1) and top-right corner (bx2, by2).
9+
10+
> Note: The rectangles may overlap.
11+
12+
## Constraints
13+
14+
1. -10^4 ≤ ax1 ≤ ax2 ≤ 10^4
15+
2. −10^4 ≤ ay1 ≤ ay2 ≤ 10^4
16+
3. −10^4 ≤ bx1 ≤ bx2 ≤10^4
17+
4. −10^4 ≤ by1 ≤ by2 ≤ 10^4
18+
19+
## Examples
20+
21+
![Example 1](./images/examples/rectangle_area_example_1.png)
22+
![Example 2](./images/examples/rectangle_area_example_2.png)
23+
![Example 3](./images/examples/rectangle_area_example_3.png)
24+
![Example 4](./images/examples/rectangle_area_example_4.png)

pymath/rectangle_area/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def compute_area(
2+
ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int
3+
):
4+
# 1. Calculate the area of each individual rectangle
5+
area_a = (ax2 - ax1) * (ay2 - ay1)
6+
area_b = (bx2 - bx1) * (by2 - by1)
7+
8+
# 2. Calculate the area of the overlap (A intersect B)
9+
10+
# Determine the coordinates of the overlap rectangle: (ix1, iy1) to (ix2, iy2)
11+
12+
# The left edge of the overlap is the max of the two left edges
13+
ix1 = max(ax1, bx1)
14+
# The right edge of the overlap is the min of the two right edges
15+
ix2 = min(ax2, bx2)
16+
17+
# The bottom edge of the overlap is the max of the two bottom edges
18+
iy1 = max(ay1, by1)
19+
# The top edge of the overlap is the min of the two top edges
20+
iy2 = min(ay2, by2)
21+
22+
# Calculate the width and height of the overlap
23+
overlap_width = max(0, ix2 - ix1)
24+
overlap_height = max(0, iy2 - iy1)
25+
26+
# The max(0, ...) ensures that if the rectangles do not overlap
27+
# (e.g., ix2 < ix1), the width/height is 0, and the overlap_area is 0.
28+
overlap_area = overlap_width * overlap_height
29+
30+
# 3. Apply the Inclusion-Exclusion Principle
31+
total_area = area_a + area_b - overlap_area
32+
33+
return total_area
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
from parameterized import parameterized
3+
from pymath.rectangle_area import compute_area
4+
5+
6+
class RectangleAreaTestCase(unittest.TestCase):
7+
@parameterized.expand(
8+
[
9+
(0, 0, 1, 1, 2, 2, 3, 3, 2),
10+
(0, 0, 2, 2, 1, 1, 3, 3, 7),
11+
(-1, -1, 2, 2, 0, 0, 1, 1, 9),
12+
(0, 0, 0, 0, 1, 1, 2, 2, 1),
13+
(-8918, -419, -7715, 577, -8918, -419, -7715, 577, 1198188),
14+
]
15+
)
16+
def test_compute_area(
17+
self,
18+
ax1: int,
19+
ay1: int,
20+
ax2: int,
21+
ay2: int,
22+
bx1: int,
23+
by1: int,
24+
bx2: int,
25+
by2: int,
26+
expected,
27+
):
28+
actual = compute_area(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)
29+
self.assertEqual(expected, actual)
30+
31+
32+
if __name__ == "__main__":
33+
unittest.main()

0 commit comments

Comments
 (0)