I am Solving this problem in which I am supposed to find line segments containing points more than three from a given plane of points. I am using a comparator to sort the points considering a point as a reference and sorting other according to the slope with reference, and i am having problems with large Inputs. I am getting am an error saying "Comparison method violates its general contract!".
I have tried modifying the comparator, but It seems to work for small inputs.
`public Comparator<Point> slopeOrder() {
return new Comparator<Point>() {
@Override
public int compare(Point p1, Point p2) {
double slope1 = slopeTo(p1);
double slope2 = slopeTo(p2);
return slope1 == slope2 ? 0 : (slope1 > slope2 ? 1 : -1);
}
};
}`
This code written in Point class, My code should output the Line segments, but The error in sorting I get is,
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.base/java.util.TimSort.mergeHi(TimSort.java:899)
at java.base/java.util.TimSort.mergeAt(TimSort.java:516)
at java.base/java.util.TimSort.mergeCollapse(TimSort.java:441)
at java.base/java.util.TimSort.sort(TimSort.java:245)
at java.base/java.util.Arrays.sort(Arrays.java:1440)
at FastCollinearPoints.<init>(FastCollinearPoints.java:28)
at FastCollinearPoints.main(FastCollinearPoints.java:87)