epsilon = 0.0001 # Homework Problem #2 # INFO: this problem is simpler is you use the vectorial product def isLeftOrRight(p=(0, 0), q=(0, 1), a=(1, 1)): """ Check if the point is on the line, on its left or on its right, by computing the z coordinate of the vector product. Return 0 if on the line, < 0 if on the left, > 0 if on the right.""" return ((q[1] - p[1]) * (a[0] - p[0])) - ((q[0] - p[0]) * (a[1] - p[1])) print "\n\n# Problem #2 : take two points P, Q, and take a third point A, then check if it is on the left, on or on the right of the line (P,Q)." # Input point P print "\nFor the first point P:" p_x = 0 p_y = 0 # Input point Q print "\nFor the second point Q:" q_x = 0 q_y = 1 # Input point A print "\nFor the third point A:" a_x = -9 a_y = 23 # Compute the coordinates of the vectors (P, Q) and (P, A) in order to do the vector product # And now check if the point is on the line or not # By computing the z coordinate of the vector product check_for_A = isLeftOrRight(p=(p_x, p_y), q=(q_x, q_y), a=(a_x, a_y)) if check_for_A == 0: print "The point A is on the line!" elif abs(check_for_A) < epsilon: print "The point A is very close to the line! (precision is not perfect here)..." elif check_for_A > 0: print "The point A is on the 'left' of the line (direction from P to Q)." elif check_for_A < 0: print "The point A is on the 'right' of the line (direction from P to Q)." else: print "Some weird thing is here!"