I adapted Anderson's monotone chain algorithm for finding a convex hull but after doing so I discovered the resulting points are in x-order, not in merry-go-round order. Is there a convex hull algorithm that generates the points in merry-go-round order, meaning in order around the perimeter of the hull?
This is my monotone chain implementation which does not satisfy my problem:
// monotone chain
public static ComparablePoint[] convex_hull( ComparablePoint[] points ){
if( points.length > 1 ){
int ctPoints = points.length;
int k = 0;
ComparablePoint[] hull = new ComparablePoint[ 2 * ctPoints ];
java.util.Arrays.sort( points );
// Build lower hull
for (int i = 0; i < ctPoints; ++i) {
while (k >= 2 && crossProduct(hull[k - 2], hull[k - 1], points[i]) <= 0)
k--;
hull[k++] = points[i];
}
// Build upper hull
for (int i = ctPoints - 2, t = k + 1; i >= 0; i--) {
while (k >= t && crossProduct(hull[k - 2], hull[k - 1], points[i]) <= 0)
k--;
hull[k++] = points[i];
}
if (k > 1) {
hull = java.util.Arrays.copyOfRange(hull, 0, k - 1); // remove non-hull vertices after k; remove k - 1 which is a duplicate
}
return hull;
} else if( points.length <= 1 ){
return points;
} else{
return null;
}
}
To be clear what I mean by merry-go-round order: the points on a convex hull are in a perimeter which is a convex polygon. I need those points to be in order as you go around the perimeter of the polygon.
The monotone chain algorithm shown above does not do this, it returns the points in order of their x-coordinate. The point with the lowest x-coordinate is first, then the point with the second-lowest x, and so on.