# 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])) """ Solve the forth problem. """ print "\n\n# Problem #4 : ask x,y, then define three points A(-x,-y), B(x, -y), and C(0, y), and take a second point P(x1, y1), then check if it is inside, on or outside the triangle (A, B, C)." # Input point P print "\nFor the two values x, y:" x = 4 y = 4 a_x, a_y = -x, -y b_x, b_y = x, -y c_x, c_y = 0, y # Input point A print "\nFor the second point P:" x1 = 2 y1 = 1 # And now check if the point is on the triangle or not check_ab = isLeftOrRight(p=(a_x, a_y), q=(b_x, b_y), a=(x1, y1)) check_bc = isLeftOrRight(p=(b_x, b_y), q=(c_x, c_y), a=(x1, y1)) check_ca = isLeftOrRight(p=(c_x, c_y), q=(a_x, a_y), a=(x1, y1)) if check_ab == 0: print "The point P is on [A, B] !" elif check_bc == 0: print "The point P is on [B, C] !" elif check_ca == 0: print "The point P is on [C, A] !" elif check_ab < 0 and check_bc < 0 and check_ca < 0: print "The point P is inside the triangle [A, B, C]." else: print "The point P is outside the triangle [A, B, C]." print "Done."