Find a Horizontal Cut That Bisects Rectangle Area
Several non-overlapping rectangular cakes lie on a plane. A cake is represented by [x, y, width, height], where (x, y) is its lower-left corner. Find the height of a horizontal line y = k such that the total cake area strictly below the line equals the total cake area strictly above it. Cake material crossed by the line is split between the two sides.
def horizontal_bisector(rectangles: list[list[float]]) -> float:
...
The input guarantees a unique answer. Return k; an absolute or relative error of at most 1e-6 is accepted. The rectangles can be very far apart, so an approach whose range depends on a small board dimension is not appropriate.
Constraints
-
1 <= len(rectangles) <= 200_000
-
-10^9 <= x, y <= 10^9
-
0 < width, height <= 10^9
-
Rectangles have disjoint interiors.
-
All input numbers are finite.
Examples
Input: rectangles = [[0, 0, 4, 2]]
Output: 1.0
Input: rectangles = [[0, 0, 2, 4], [10, 1, 2, 2]]
Output: 2.0
Explanation: At y = 2, the first cake contributes area 4 below the line and
the second contributes area 2 below it. The total area is 12, so both sides
contain area 6.