Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@
* [Test Pascals Triangle](https://github.com/BrianLusina/PythonSnips/blob/master/pymath/pascals_triangle/test_pascals_triangle.py)
* Perfect Square
* [Test Perfect Squares](https://github.com/BrianLusina/PythonSnips/blob/master/pymath/perfect_square/test_perfect_squares.py)
* Rectangle Area
* [Test Compute Area](https://github.com/BrianLusina/PythonSnips/blob/master/pymath/rectangle_area/test_compute_area.py)
Comment thread
BrianLusina marked this conversation as resolved.
* Super Size
* [Test Super Size](https://github.com/BrianLusina/PythonSnips/blob/master/pymath/super_size/test_super_size.py)
* Triangles
Expand Down
24 changes: 24 additions & 0 deletions pymath/rectangle_area/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Rectangle Area

You are given the coordinates of two axis-aligned rectangles in a 2D plane. Your task is to calculate the total area
covered by both rectangles.

The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ay1, ay2).

Similarly, the second rectangle is defined by its bottom-left corner (bx1, by1) and top-right corner (bx2, by2).
Comment on lines +3 to +8
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix coordinate typo in rectangle definition (ax2 vs ay1).

Line 6 currently says top-right is (ay1, ay2); that should be (ax2, ay2).

-The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ay1, ay2).
+The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ax2, ay2).

Also applies to: 14-17

🤖 Prompt for AI Agents
In pymath/rectangle_area/README.md around lines 3-8 and again around lines
14-17, the rectangle coordinate descriptions misuse ax2 as ay1: change the
top-right coordinate of the first rectangle from (ay1, ay2) to (ax2, ay2) in
both occurrences so the first rectangle is defined by bottom-left (ax1, ay1) and
top-right (ax2, ay2).


> Note: The rectangles may overlap.

## Constraints

1. -10^4 ≤ ax1 ≤ ax2 ≤ 10^4
2. −10^4 ≤ ay1 ≤ ay2 ≤ 10^4
3. −10^4 ≤ bx1 ≤ bx2 ≤10^4
4. −10^4 ≤ by1 ≤ by2 ≤ 10^4

## Examples

![Example 1](./images/examples/rectangle_area_example_1.png)
![Example 2](./images/examples/rectangle_area_example_2.png)
![Example 3](./images/examples/rectangle_area_example_3.png)
![Example 4](./images/examples/rectangle_area_example_4.png)
33 changes: 33 additions & 0 deletions pymath/rectangle_area/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def compute_area(
ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int
):
# 1. Calculate the area of each individual rectangle
area_a = (ax2 - ax1) * (ay2 - ay1)
area_b = (bx2 - bx1) * (by2 - by1)

# 2. Calculate the area of the overlap (A intersect B)

# Determine the coordinates of the overlap rectangle: (ix1, iy1) to (ix2, iy2)

# The left edge of the overlap is the max of the two left edges
ix1 = max(ax1, bx1)
# The right edge of the overlap is the min of the two right edges
ix2 = min(ax2, bx2)

# The bottom edge of the overlap is the max of the two bottom edges
iy1 = max(ay1, by1)
# The top edge of the overlap is the min of the two top edges
iy2 = min(ay2, by2)

# Calculate the width and height of the overlap
overlap_width = max(0, ix2 - ix1)
overlap_height = max(0, iy2 - iy1)

# The max(0, ...) ensures that if the rectangles do not overlap
# (e.g., ix2 < ix1), the width/height is 0, and the overlap_area is 0.
overlap_area = overlap_width * overlap_height

# 3. Apply the Inclusion-Exclusion Principle
total_area = area_a + area_b - overlap_area

return total_area
Comment on lines +1 to +33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add return type + docstring/doctest + reference URL (per repo checklist).

compute_area is missing a return type annotation and (per the PR checklist you referenced) a doctest + a URL comment pointing to Wikipedia/similar.

-def compute_area(
+def compute_area(
     ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int
-):
+) -> int:
+    """
+    Compute total area covered by two axis-aligned rectangles.
+
+    Reference: https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle
+
+    >>> compute_area(0, 0, 2, 2, 1, 1, 3, 3)
+    7
+    """

(Optional hardening): consider guarding against reversed coordinates (e.g., ax2 < ax1) to avoid negative areas if callers violate constraints.

33 changes: 33 additions & 0 deletions pymath/rectangle_area/test_compute_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from parameterized import parameterized
from pymath.rectangle_area import compute_area


class RectangleAreaTestCase(unittest.TestCase):
@parameterized.expand(
[
(0, 0, 1, 1, 2, 2, 3, 3, 2),
(0, 0, 2, 2, 1, 1, 3, 3, 7),
(-1, -1, 2, 2, 0, 0, 1, 1, 9),
(0, 0, 0, 0, 1, 1, 2, 2, 1),
(-8918, -419, -7715, 577, -8918, -419, -7715, 577, 1198188),
]
)
def test_compute_area(
self,
ax1: int,
ay1: int,
ax2: int,
ay2: int,
bx1: int,
by1: int,
bx2: int,
by2: int,
expected,
):
actual = compute_area(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)
self.assertEqual(expected, actual)


if __name__ == "__main__":
unittest.main()
Loading