I want to calculate the overlapped area \"THE GRAY REGION\" between red and bl
As this question has a shapely tag, here is a solution using it. I will use the same rectangles as in the tom10 answer:
from shapely.geometry import Polygon
polygon = Polygon([(3, 3), (5, 3), (5, 5), (3, 5)])
other_polygon = Polygon([(1, 1), (4, 1), (4, 3.5), (1, 3.5)])
intersection = polygon.intersection(other_polygon)
print(intersection.area)
# 0.5
This is much more concise than the version in the accepted answer. You don't have to construct your own Rectangle
class as Shapely already provides the ready ones. It's less error-prone (go figure out the logic in that area
function). And the code itself is self-explanatory.
References:
Docs for object.intersection(other) method