This is an interview question: "Find all collinear points in a given set".
As I understand, they ask to print out the points, which lie in the same line (and every two points are always collinear). I would suggest the following.
- Let's introduce two types
Line
(pair of doubles) andPoint
(pair of integers). - Create a multimap :
HashMap<Line, List<Point>)
- Loop over all pairs of points and for each pair: calculate the
Line
connecting the points and add the line with those points to the multimap.
Finally, the multimap contains the lines as the keys and a list collinear points for each line as its value.
The complexity is O(N^2). Does it make sense ? Are there better solutions ?