#NEWLINE# def rectangles_from_points( S ) : #NEWLINE# #INDENT# 'How many rectangles can be formed from a set of points\n\n    :param S: list of points, as coordinate pairs\n    :returns: the number of rectangles\n    :complexity: :math:`O(n^2)`\n    ' #NEWLINE# answ = 0 #NEWLINE# pairs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for j in range ( len ( S ) ) : #NEWLINE# #INDENT# for i in range ( j ) : #NEWLINE# #INDENT# ( px , py ) = S [ i ] #NEWLINE# ( qx , qy ) = S [ j ] #NEWLINE# center = ( ( px + qx ) , ( py + i ) ) #NEWLINE# dist = ( ( ( px - qx ) * ( px - qx ) ) + ( ( py - qy ) * ( py - qy ) ) ) #NEWLINE# sign = ( center , dist ) #NEWLINE# if ( sign in pairs ) : #NEWLINE# #INDENT# answ += len ( pairs [ sign ] ) #NEWLINE# pairs [ sign ] . append ( ( i , j ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pairs [ sign ] = [ ( i , j ) ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return answ