Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Since you didn't mention memory, I'll assume O(N^2) is acceptable. Setup pseudocode: for every p1, p2: ln = line(p1, p2) points_on_line[ln.m, ln.c] += [p1, p2] If there are at least ...
Answer
#1: Initial revision
Since you didn't mention memory, I'll assume O(N^2) is acceptable. Setup pseudocode: ``` for every p1, p2: ln = line(p1, p2) points_on_line[ln.m, ln.c] += [p1, p2] ``` If there are at least 2 colinear points on your line, you can get them in O(1) with `points_on_line[m, c]`. If not, you'll have to do brute force. The initialization takes O(N^2) so it is practical if the number of searches you need to do is much more than `N^2` (eg. 5 points, 1000 searches). Note that you can add points: ``` def add_point(p): for m, c in points_on_line: if p.y == m * p.x + c: points_on_line[m, c] += [p] ``` This will be O(N), so adding new points is okay so long as you are doing a lot of searches for each point you add.