int currentPoint = 2;
int direction = 1;
PVector copyOf(PVector p){
return new PVector(p.x, p.y);
}
void addPoint() {
hull.add(points.get(currentPoint));
// look at the turn direction in the last three points
// (we have to work with copies of the points because Java)
p1 = copyOf(hull.get(hull.size() - 3));
p2 = copyOf(hull.get(hull.size() - 2));
p3 = copyOf(hull.get(hull.size() - 1));
while (!crossprod (p1, p2, p3) && hull.size() > 2) {
// if crossproduct is <= 0, then remove the middle point
// if crossproduct is >= 0, do nothing because we have add the point before
hull.remove(hull.size() - 2);
if (hull.size() >= 3) {
//in case of null pointer error
p1 = copyOf(hull.get(hull.size() - 3));
}
p2 = copyOf(hull.get(hull.size() - 2));
p3 = copyOf(hull.get(hull.size() - 1));
}
//you'll see information in the console
println("currentPoint: " + currentPoint + " numPoints: " + points.size() + " hullSize: " + hull.size()+" direction:"+direction);
if (currentPoint == points.size() -1 || currentPoint == 0) {
//when direction = 1, it's a traversal of all points with O(n)
//when direction = -1, the construction of convex hull began which is O(nlogn)
direction = direction * -1;
if (currentPoint == 0){
/****add code here*****/
}
}
currentPoint+= direction;
}
It's quite hard to explain my question clearly without code. I want to add some code so that when currentPoint = 0
, it will quit the addpoint()
function. exit()
won't work, for it will quit the whole program and the animation already played will disappear. I can't think up any good solution to that, do anybody has ideas about that?