Perimeter of the Union of Two Rectangles | Function to check if two rectangles are intersecting or not ; If one rectangle is to the right of other 's right edge ; If one rectangle is on the top of other 's top edge ; Function to return the perimeter of the Union of Two Rectangles ; Stores the resultant perimeter ; If rectangles do not interesect ; Perimeter of Rectangle 1 ; Perimeter of Rectangle 2 ; If the rectangles intersect ; Get width of combined figure ; Get length of combined figure ; Return the perimeter ; Driver Code
def doIntersect ( X , Y ) : NEW_LINE INDENT if ( X [ 0 ] > X [ 3 ] or X [ 2 ] > X [ 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( Y [ 0 ] > Y [ 3 ] or Y [ 2 ] > Y [ 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def getUnionPerimeter ( X , Y ) : NEW_LINE INDENT perimeter = 0 NEW_LINE if ( not doIntersect ( X , Y ) ) : NEW_LINE INDENT perimeter += 2 * ( abs ( X [ 1 ] - X [ 0 ] ) + abs ( Y [ 1 ] - Y [ 0 ] ) ) NEW_LINE perimeter += 2 * ( abs ( X [ 3 ] - X [ 2 ] ) + abs ( Y [ 3 ] - Y [ 2 ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT w = max ( X ) - min ( X ) NEW_LINE l = max ( Y ) - min ( Y ) NEW_LINE perimeter = 2 * ( l + w ) NEW_LINE DEDENT return perimeter NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ - 1 , 2 , 4 , 6 ] NEW_LINE Y = [ 2 , 5 , 3 , 7 ] NEW_LINE print ( getUnionPerimeter ( X , Y ) ) NEW_LINE DEDENT