Length of intersecting part i . e start from max ( l1 . x , l2 . x ) of x - coordinate and end at min ( r1 . x , r2 . x ) x - coordinate by subtracting start from end we get required lengths
int x_dist = min ( r1 . x , r2 . x ) - max ( l1 . x , l2 . x ) ; int y_dist = ( min ( r1 . y , r2 . y ) - max ( l1 . y , l2 . y ) ) ; int areaI = 0 ; if ( x_dist > 0 && y_dist > 0 ) { areaI = x_dist * y_dist ; } return ( area1 + area2 - areaI ) ; }