That’s easy. First compute coordinates of intersection, which is also a rectangle.
left = max(r1.left, r2.left)
right = min(r1.right, r2.right)
bottom = max(r1.bottom, r2.bottom)
top = min(r1.top, r2.top)
Then, if intersection is not empty (left < right && bottom < top), subtract it from the common area of two rectangles: r1.area + r2.area - intersection.area.
PS:
- Assumption 1: rectangles are aligned by the coordinate axes, that’s usually the case.
- Assumption 2: y axis here increases upwards, for example, in a graphics application, the y axis increases downwards, you may need to use:
bottom = min(r1.bottom, r2.bottom)
top = max(r1.top, r2.top)