# Author : Steven Miller # Course Number : 22C:016 SCA # Student ID : 4784653 def slope(p1, p2) : ''' Returns the slope of points p1 and p2 ''' slope = None # assume points are on a vertical line if float(p2[0])-float(p1[0]) != 0.0 : # if not on a vertical line # compute their slope slope = (float(p2[1]) - float(p1[1])) / (float(p2[0]) - float(p1[0])) return slope def areCollinear(p1, p2, p3) : ''' Returns true if points p1, p2, and p3 are collinear''' # p1, p2, and p3 are collinear if slope(p1,p2) == slope(p2,p3) return slope(p1,p2) == slope(p2,p3) def collinearityTest(pointList) : ''' Returns a list of three collinear points in pointList, otherwise returns the empty list ''' n = len(pointList) # compute the length of pointList once for p1 in range(0,n) : # let p1 be each point in list for p2 in range(p1+1,n): # let p2 be each point beyond p1 for p3 in range(p2+1,n): # let p3 be each point beyond p2 if areCollinear(pointList[p1],pointList[p2],pointList[p3]): # return [ p1, p2, p3] if they are collinear return [ pointList[p1], pointList[p2], pointList[p3] ] return [] # three collinear points not found - return empty list