#NEWLINE# def is_convex( points ) : #NEWLINE# #INDENT# 'Test if a polygon (list of (x,y)) is convex or not\n    \n    :return: True if the polygon is convex, False otherwise\n    ' #NEWLINE# assert ( len ( points ) > 2 ) , 'need at least 3 points to form a polygon' #NEWLINE# p0 = points [ 0 ] #NEWLINE# p1 = points [ 1 ] #NEWLINE# p2 = points [ 2 ] #NEWLINE# ( xc , yc ) = ( 0 , 0 ) #NEWLINE# is_same_winding = is_left ( p0 , p1 , p2 ) #NEWLINE# for p2 in ( ( points [ 2 : ] + [ p0 ] ) + [ p1 ] ) : #NEWLINE# #INDENT# if ( is_same_winding != is_left ( p0 , p1 , p2 ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# a = ( ( p1 [ X ] - p0 [ X ] ) , ( p1 [ Y ] - p0 [ Y ] ) ) #NEWLINE# b = ( ( p2 [ X ] - p1 [ X ] ) , ( p2 [ Y ] - p1 [ Y ] ) ) #NEWLINE# if ( sign ( a [ X ] ) != sign ( b [ X ] ) ) : #NEWLINE# #INDENT# xc += 1 #NEWLINE# #UNINDENT# if ( sign ( a [ Y ] ) != sign ( b [ Y ] ) ) : #NEWLINE# #INDENT# yc += 1 #NEWLINE# #UNINDENT# ( p0 , p1 ) = ( p1 , p1 ) #NEWLINE# #UNINDENT# return ( ( xc <= 2 ) and ( yc <= 2 ) )