-1
    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?

roshan_nazareth
  • 311
  • 5
  • 16
Amir Bashir
  • 1
  • 1
  • 1
  • 2
  • add a `return;`..that shall work(without looking at the rest of your implementation). – Naman Sep 12 '17 at 04:38
  • more info http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html – Naman Sep 12 '17 at 04:52
  • 1
    In the future, please try to post a [mcve] instead of your whole program. A simple program is much easier to debug. Also please try to use proper formatting, as right now your code is very hard to read. – Kevin Workman Sep 12 '17 at 05:07

2 Answers2

1

From the reference:

The keyword return may also be used to break out of a function, thus not allowing the program to the remaining statements.

void draw() {
  background(204);
  line(0, 0, width, height);
  if (mousePressed) {
    return;  // Break out of draw(), skipping the line statement below
  }
  line(0, height, width, 0);  // Executed only if mouse is not pressed
}

So in your case:

I want to add some code so that when currentPoint = 0, it will quit the addpoint() function.

if(currentPoint == 0){
  return;
}

Taking a step back, you should really get into the habit of doing some research. A quick google search of "java quit function" returns a bunch of results, including:

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0

Use

return;     

or

return <optional object>;
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189