How to check if given four points form a square | A Python3 program to check if four given points form a square or not . ; Structure of a point in 2D space ; A utility function to find square of distance from point ' p ' to point 'q ; This function returns true if ( p1 , p2 , p3 , p4 ) form a square , otherwise false ; d2 = distSq ( p1 , p2 ) from p1 to p2 d3 = distSq ( p1 , p3 ) from p1 to p3 d4 = distSq ( p1 , p4 ) from p1 to p4 ; If lengths if ( p1 , p2 ) and ( p1 , p3 ) are same , then following conditions must be met to form a square . 1 ) Square of length of ( p1 , p4 ) is same as twice the square of ( p1 , p2 ) 2 ) Square of length of ( p2 , p3 ) is same as twice the square of ( p2 , p4 ) ; The below two cases are similar to above case ; Driver Code
class Point : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT ' NEW_LINE def distSq ( p , q ) : NEW_LINE INDENT return ( p . x - q . x ) * ( p . x - q . x ) + ( p . y - q . y ) * ( p . y - q . y ) NEW_LINE DEDENT def isSquare ( p1 , p2 , p3 , p4 ) : NEW_LINE INDENT if d2 == 0 or d3 == 0 or d4 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT if d2 == d3 and 2 * d2 == d4 and 2 * distSq ( p2 , p4 ) == distSq ( p2 , p3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if d3 == d4 and 2 * d3 == d2 and 2 * distSq ( p3 , p2 ) == distSq ( p3 , p4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if d2 == d4 and 2 * d2 == d3 and 2 * distSq ( p2 , p3 ) == distSq ( p2 , p4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p1 = Point ( 20 , 10 ) NEW_LINE p2 = Point ( 10 , 20 ) NEW_LINE p3 = Point ( 20 , 20 ) NEW_LINE p4 = Point ( 10 , 10 ) NEW_LINE if isSquare ( p1 , p2 , p3 , p4 ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT