Find minimum area of rectangle with given set of coordinates | ''Python Implementation of above approach ; function to find minimum area of Rectangle ; creating empty columns ; fill columns with coordinates ; check if rectangle can be formed ; Driver code
import collections NEW_LINE def minAreaRect ( A ) : NEW_LINE INDENT columns = collections . defaultdict ( list ) NEW_LINE for x , y in A : NEW_LINE INDENT columns [ x ] . append ( y ) NEW_LINE DEDENT lastx = { } NEW_LINE ans = float ( ' inf ' ) NEW_LINE for x in sorted ( columns ) : NEW_LINE INDENT column = columns [ x ] NEW_LINE column . sort ( ) NEW_LINE for j , y2 in enumerate ( column ) : NEW_LINE INDENT for i in range ( j ) : NEW_LINE INDENT y1 = column [ i ] NEW_LINE if ( y1 , y2 ) in lastx : NEW_LINE INDENT ans = min ( ans , ( x - lastx [ y1 , y2 ] ) * ( y2 - y1 ) ) NEW_LINE DEDENT lastx [ y1 , y2 ] = x NEW_LINE DEDENT DEDENT DEDENT if ans < float ( ' inf ' ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT A = [ [ 1 , 1 ] , [ 1 , 3 ] , [ 3 , 1 ] , [ 3 , 3 ] , [ 2 , 2 ] ] NEW_LINE print ( minAreaRect ( A ) ) NEW_LINE