Skip to content

Instantly share code, notes, and snippets.

@sminogue
Created April 13, 2017 13:45
Show Gist options
  • Save sminogue/751c837f516572f62a10e1dc75bac667 to your computer and use it in GitHub Desktop.
Save sminogue/751c837f516572f62a10e1dc75bac667 to your computer and use it in GitHub Desktop.
public static List<Point2D_F64> sortPoints( List<Point2D_F64> pts ) {
//Copy points into a working list
List<Point2D_F64> points = new ArrayList<Point2D_F64>();
points.addAll(pts);
//Create list of points to be returned.
List<Point2D_F64> returns = new ArrayList<Point2D_F64>();
//Sort the points by Y value
Collections.sort(points, new Comparator<Point2D_F64>() {
@Override
public int compare( Point2D_F64 a, Point2D_F64 b ) {
if (a.y < b.y) {
return -1;
} else if (a.y == b.y) {
return 0;
} else {
return 1;
}
}
});
// First 2 elements in the list are the top of the page. The one with
// the lower X is TL the other is TR
Point2D_F64 a, b;
a = points.get(0);
b = points.get(1);
if (a.x < b.x) {
returns.add(a);
returns.add(b);
} else {
returns.add(b);
returns.add(a);
}
// Second 2 elements are the bottom points
a = points.get(2);
b = points.get(3);
if (a.x < b.x) {
returns.add(b);
returns.add(a);
} else {
returns.add(a);
returns.add(b);
}
return returns;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment