# Intersection over Union for 2 rectangles with the same orientation

I have 2 squares with the same orientation. I know the coordinate of their centers. They have the same size area.

They may intersect each other. I want to compute their area of their intersection as well as area of their union.

Let's say that their centers are (a, b) for the first square and (x, y) for the second square. The length of one of their sides is l. How can I compute their intersection and their union based on this?

-
Do both squares have side length 1? –  Tom Oldfield Oct 31 '12 at 14:17
They have the same length L. –  Simon Oct 31 '12 at 14:21
Call the distance between the centres $d=\sqrt{(x-a)^2+(y-b)^2}$ and the distance from the corner of one square to it's centre is $\frac{L}{\sqrt{2}}$. So if $$d\geq 2\frac{L}{\sqrt{2}} = \sqrt{2}L$$ Then the squares do not intersect and we are done. Otherwise, the squares intersect in a rectangle.
The base of the rectangle of intersection is $L-|x-a|$ and it's height is $L-|y-b|$ since, for example, the horizontal distance between the centres $(|x-a|)$ $+$ the horizontal overlap gives $L$ (draw a diagram if you can't see this.)