I cannot see my way to sorting an array of points using a simple float comparison.
Here’s the essence of what I’m seeking to do:
import java.util.Collections;
void setup(){
ArrayList<Point> points = new ArrayList();
PVector p0 = new PVector(100, -100);
PVector p1 = new PVector(-130, 65);
PVector p2 = new PVector(135, 155);
points.add( new Point(p0, p0.heading()) );
points.add( new Point(p1, p1.heading()) );
points.add( new Point(p2, p2.heading()) );
Collections.sort(points, (a,b) -> a.heading < b.heading ? -1 : 1);
}
class Point {
PVector loc;
float heading;
Point(PVector _loc, float _heading){
loc = _loc;
heading = _heading;
}
}
Apparently I’m only allowed to use lambda expressions at source level 1.8 or above, whatever that means, so that doesn’t work.
I’ve found many examples that tell me what I should be doing, but none of them seem to be working. For example this tells me this should work:
class Point implements Comparator<Point> {
PVector loc;
float heading;
Point(PVector _loc, float _heading){
loc = _loc;
heading = _heading;
}
// Overriding the compare method to sort the heading
public float compare(Point p0, Point p1) {
return p0.heading - p1.heading;
}
}
but apparently The return type is incompatible with Comparator<sketch_xxx.Point>.compare(sketch_xxx.Point,sketch_xxx.Point)
Other websites tell me to do other things, such as overriding Comparable<Point> compareTo()
, which I haven’t gotten to work either. (I’ll save everyone the trouble of the profusion of links I visited, unless asked for them.)
I would be grateful if someone can please let me know a way that works in processing.