Total area of two overlapping rectangles | Python program to find total area of two overlapping Rectangles Returns Total Area of two overlap rectangles ; Area of 1 st Rectangle ; Area of 2 nd Rectangle ; 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 ; Driver 's Code ; Function call
def overlappingArea ( l1 , r1 , l2 , r2 ) : NEW_LINE INDENT x = 0 NEW_LINE y = 1 NEW_LINE area1 = abs ( l1 [ x ] - r1 [ x ] ) * abs ( l1 [ y ] - r1 [ y ] ) NEW_LINE area2 = abs ( l2 [ x ] - r2 [ x ] ) * abs ( l2 [ y ] - r2 [ y ] ) NEW_LINE x_dist = ( min ( r1 [ x ] , r2 [ x ] ) - max ( l1 [ x ] , l2 [ x ] ) ) NEW_LINE y_dist = ( min ( r1 [ y ] , r2 [ y ] ) - max ( l1 [ y ] , l2 [ y ] ) ) NEW_LINE areaI = 0 NEW_LINE if x_dist > 0 and y_dist > 0 : NEW_LINE INDENT areaI = x_dist * y_dist NEW_LINE DEDENT return ( area1 + area2 - areaI ) NEW_LINE DEDENT l1 = [ 2 , 2 ] NEW_LINE r1 = [ 5 , 7 ] NEW_LINE l2 = [ 3 , 4 ] NEW_LINE r2 = [ 6 , 9 ] NEW_LINE print ( overlappingArea ( l1 , r1 , l2 , r2 ) ) NEW_LINE