如果可以访问矩形的角点,则可以使用shapely模块并使用一些设置操作,如difference和union。
Documentation
说操作是高度优化的。
from shapely.geometry import Polygon, Point
# define rectangles as polygons using their border points
A = Polygon(((0, 0), (2, 0), (2, 2), (0, 2)))
B = Polygon(((-1, -1), (-1, 1), (1, 1), (1, -1)))
C = Polygon(((0, 0), (-2, 0), (-2, -2), (0, -2)))
print(A.area) # 4.0
print(B.area) # 4.0
print(C.area) # 4.0
b_without_a = B.difference(A)
b_without_c = B.difference(C)
print(b_without_a.area) # 3.0
print(b_without_c.area) # 3.0
total = b_without_a.intersection(b_without_c)
print(total.area) # 2.0