Question
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.
Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.
Show Tags
Solution
Analysis
The coordination of the corner node of the intersection area is the the min and max of those coordinations.
Code
class Solution(object):
def computeArea(self, A, B, C, D, E, F, G, H):
"""
:type A: int
:type B: int
:type C: int
:type D: int
:type E: int
:type F: int
:type G: int
:type H: int
:rtype: int
"""
return (C-A)*(D-B) + (G-E)*(H-F) - max(0,min(C,G)-max(E,A))*max(0,min(D,H)-max(F,B))
本文介绍了一种计算二维平面上两个矩形重叠部分面积的方法,并提供了一个Python类实现,该方法通过求解最小最大坐标来确定重叠区域。
877

被折叠的 条评论
为什么被折叠?



