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
let x_dist = ( Math . min ( r1 [ x ] , r2 [ x ] ) - Math . max ( l1 [ x ] , l2 [ x ] ) ) let y_dist = ( Math . min ( r1 [ y ] , r2 [ y ] ) - Math . max ( l1 [ y ] , l2 [ y ] ) ) let areaI = 0 if ( x_dist > 0 && y_dist > 0 ) areaI = x_dist * y_dist return ( area1 + area2 - areaI ) }