How can I check if two rectangles overlap and if they do, calculate the area of the overlap? I'm interested in the case where:

  1. all lines in both rectangles are either vertical or horizontal
  2. the general case for any two rectangles

but the only answer I really need is case 1. I'm thinking along the lines of:

double areaOfOverlap( Rect A, Rect B)
{
    if ( A.Intersects(B) )
    {
        // calculate area
        // return area
    }
    return 0;
}

For A.Intersects() I was thinking of using the separating axis test, but if the rectangles have only horizontal and vertical lines is there an even simpler (faster) way? And for calculating the area where they intersect, is there a quick way if the rectangles are only horizontal and vertical lines? This seems complicated and doesn't directly answer the question.

Edit
Report