I have the following code for my convex hull: The first section sorts the array by value of X which is required for convex hull, the second part is the actual convex hull code we were given, how do i get my array to pass through the convex hull code? I'm fairly new to java, sorry if this is a simple question. Thanks!
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class FastConvexHull implements ConvexHullAlgorithm
{
public static void main(String args[]){
int[][] ArrayList={{1,5},{3,9},{5,5},{4,1},{9,5},{6,9},{10,5}};
Arrays.sort(ArrayList, new Comparator<int[]>(){
public int compare(int[] a, int[] b){
//assumes array length is 2
int x,y;
if (a[0]!=b[0]) {
x=a[0];y=b[0];
}
else{
x=a[1];y=b[1];
}
if (x<y) return -1;
else if (x==y) return 0;
else return +1;
}
});
for(int[] term: ArrayList){
System.out.println(Arrays.toString(term));
}
}
public ArrayList<Point> execute(ArrayList<Point> points)
{
ArrayList<Point> xSorted = (ArrayList<Point>) points.clone();
Collections.sort(xSorted, new XCompare());
int n = xSorted.size();
Point[] lUpper = new Point[n];
lUpper[0] = xSorted.get(0);
lUpper[1] = xSorted.get(1);
int lUpperSize = 2;
for (int i = 2; i < n; i++)
{
lUpper[lUpperSize] = xSorted.get(i);
lUpperSize++;
while (lUpperSize > 2 && !rightTurn(lUpper[lUpperSize - 3], lUpper[lUpperSize - 2], lUpper[lUpperSize - 1]))
{
// Remove the middle point of the three last
lUpper[lUpperSize - 2] = lUpper[lUpperSize - 1];
lUpperSize--;
}
}
Point[] lLower = new Point[n];
lLower[0] = xSorted.get(n - 1);
lLower[1] = xSorted.get(n - 2);
int lLowerSize = 2;
for (int i = n - 3; i >= 0; i--)
{
lLower[lLowerSize] = xSorted.get(i);
lLowerSize++;
while (lLowerSize > 2 && !rightTurn(lLower[lLowerSize - 3], lLower[lLowerSize - 2], lLower[lLowerSize - 1]))
{
// Remove the middle point of the three last
lLower[lLowerSize - 2] = lLower[lLowerSize - 1];
lLowerSize--;
}
}
ArrayList<Point> result = new ArrayList<Point>();
for (int i = 0; i < lUpperSize; i++)
{
result.add(lUpper[i]);
}
for (int i = 1; i < lLowerSize - 1; i++)
{
result.add(lLower[i]);
}
return result;
}
private boolean rightTurn(Point a, Point b, Point c)
{
return (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x) > 0;
}
private class XCompare implements Comparator<Point>
{
public int compare(Point o1, Point o2)
{
return (new Integer(o1.x)).compareTo(new Integer(o2.x));
}
}
}