Assuming you have rectangles r1
and r2
, with .left, .right, .top, and .bottom
edges, then
left = max(r1.left, r2.left);
right = min(r1.right, r2.right);
top = max(r1.top, r2.top);
bottom = min(r1.bottom, r2.bottom);
(with the usual convention that coordinates increase top to bottom and left to right). Finally, check that left<right
and top<bottom
, and compute the area:
Area = (right - left) * (top - bottom);
Alternatively, you can use the clip()
function. From the docs you linked in your question:
clip(Rect) -> Rect Returns a new rectangle that is cropped to be completely inside the argument Rect. If the two rectangles do not overlap to begin with, a Rect with 0 size is returned.