I recently was asked to come up with an algorithm to find all the lines for the given set of points that have more than two points on them. For instance if I have:
[(0, 0), (1, 1), (0, 2), (1, 2), (2, 2)]
I need to find all the lines that I can create with these points that includes at least three of them.
I started by finding all the pair combination of points, which is 10 different combinations. Then iterate through each pair, find the slope and y-intercept and then check the remaining points to see if they fit the equation. Or in case of slope = inf
, I would just check the X
value.
Just wanna know if this, brute force, is the only way or is there any other magic I could use in terms of efficiency.
efficient
solution I was wondering if I provided one or failed to do so. – Sam R. Jun 07 '16 at 14:40