# Program to determine if 3 points lie on a line # (user enters points) # Matt Bishop, May 1, 2009 # for ECS 10 Spring 2009 # this routine determines if 3 points are on a line # parameters: x1, y1: x, y co-ordinates of first point # x2, y2: x, y co-ordinates of second point # x3, y3: x, y co-ordinates of third point # side effect: prints message giving answer def isline(x1, y1, x2, y2, x3, y3): try: # now compute the slope and y-intercept for 2 points m = (float(y2)-float(y1))/(float(x2)-float(x1)); b = y2 - m * x2 # now see if (x3, y3) are on the same line if (y3 == m * x3 + b): print "The points are on the same (non-horizontal) line"; else: print "The points are not on the same line"; except ZeroDivisionError: # only way to get here is if you divide by 0 if (x2 == x3): print "The points are on the same horizontal line"; else: print "The points are not on the same line"; # this routine generates a prompt to ask for a point # parameters: what: which point (first, second, third) do you want? # returns: the prompt as a string def prompt(what): # generate the prompt and return it s = "Enter the " + what + " point's co-ordinates separated by a comma: "; return s; # now put it all together # calls: prompt: generates user prompt # peri: to compute perimeter def main(): try: x1, y1 = input(prompt("first")); x2, y2 = input(prompt("second")); x3, y3 = input(prompt("third")); except NameError: print"\nYou didn't enter two numbers" except TypeError: print "\nAt least one of your inputs was not a number" except SyntaxError: print "\nYour input was malformed (needs a comma)" # this is very bad style; it's for demonstration only! except: print "\nI don't know what went wrong, but something did!" isline(x1, y1, x2, y2, x3, y3); main()