We have two rectangles. These are represented by coordinate pairs at the bottom-left (L) and top-right coordinates (R). In the following diagram, the second rect is translated x+ and y+, but the shape is the same. This will not always be the case. It could be translated any amount x or y and can have a different size.
We know both rectangles will always be parallel to the axis.
We know they if they are overlapping with the following formula:
function doOverlap(L1, R1, L2, R2) {
// check if one rect is to the left of the other
if (R1.x < L2.x OR R2.x < L1.x)
return false;
// check if one rect is to the below of the other
if (R1.y < L2.y OR R2.y < L1.y)
return false;
// If both of the above checks are not true, the two rectangles overlap
return true;
As depicted in the second part of the diagram above, I'd like to figure out a way to determine the two boxes that represent the area of the second rectangle that isn't part of the overlapping area.
If the second rectangle completely resides within the first, we don't need to do anything as the whole second rectangle is overlap.
For these calculations, we don't need to worry about whether the second rectangle fully encapsulates the first.
The main issue I'm trying to figure out is when part of the second rectangle resides within the first.