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.


1 Answer 1


Create a function that returns the bottom left and upper right corner of the overlapping rectangle (in pseudocode):

function intersection(L1, R1, L2, R2) {
    L3.x = max(L1.x, L2.x)
    L3.y = max(L1.y, L2.y)
    R3.x = min(R1.x, R2.x)
    R3.y = min(R1.y, R2.y)
    if(L3.x > R3.x or L3.y > R3.y)
        return NULL

Now you can calculate the area outside of overlapping rectangle by subtracting the area of overlapping rectangle from the the area of the first or the second rectangle.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .