class Point: def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return '({0}, {1})'.format(self.x, self.y) def lineFinder(myList): myPoints = [] for i in range(len(myList)): point = Point(myList[i][0], myList[i][1]) myPoints.append(point) # print(myPoints) result = [] for p1 in myPoints: for p2 in myPoints: if p2 is p1: continue for p3 in myPoints: if p3 is p2 or p3 is p1: continue # Test if 3 points are on a line: (x3−x1)(y2−y1)=(x2−x1)(y3−y1) if (p3.x - p1.x) * (p2.y - p1.y) == (p2.x - p1.x) * (p3.y - p1.y): m = (p2.y - p1.y) / (p2.x - p1.x) b = p1.y - m * p1.x newLine = 'y = {0}x + {1}'.format(m, b) # Make sure the new line we found doesn't already exist if newLine not in result: result.append(newLine) print(result) test_example = [(0, 0), (1, 1), (3, 4), (2, 2), (3, 2), (5, 3), (8, 6)] lineFinder(test_example)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question