Check if given polygon is a convex polygon or not | Utility function to find cross product of two vectors ; Stores coefficient of X direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of X direction of vector A [ 2 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 2 ] A [ 0 ] ; Return cross product ; Function to check if the polygon is convex polygon or not ; Stores count of edges in polygon ; Stores direction of cross product of previous traversed edges ; Stores direction of cross product of current traversed edges ; Traverse the array ; Stores three adjacent edges of the polygon ; Update curr ; If curr is not equal to 0 ; If direction of cross product of all adjacent edges are not same ; Update curr ; Driver code
def CrossProduct ( A ) : NEW_LINE INDENT X1 = ( A [ 1 ] [ 0 ] - A [ 0 ] [ 0 ] ) NEW_LINE Y1 = ( A [ 1 ] [ 1 ] - A [ 0 ] [ 1 ] ) NEW_LINE X2 = ( A [ 2 ] [ 0 ] - A [ 0 ] [ 0 ] ) NEW_LINE Y2 = ( A [ 2 ] [ 1 ] - A [ 0 ] [ 1 ] ) NEW_LINE return ( X1 * Y2 - Y1 * X2 ) NEW_LINE DEDENT def isConvex ( points ) : NEW_LINE INDENT N = len ( points ) NEW_LINE prev = 0 NEW_LINE curr = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp = [ points [ i ] , points [ ( i + 1 ) % N ] , points [ ( i + 2 ) % N ] ] NEW_LINE curr = CrossProduct ( temp ) NEW_LINE if ( curr != 0 ) : NEW_LINE INDENT if ( curr * prev < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT prev = curr NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT points = [ [ 0 , 0 ] , [ 0 , 1 ] , [ 1 , 1 ] , [ 1 , 0 ] ] NEW_LINE if ( isConvex ( points ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT