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;
}
Il est assez difficile d'expliquer clairement ma question sans code. Je veux ajouter du code de sorte que quand currentPoint = 0
, il quittera la fonction addpoint()
. exit()
ne fonctionnera pas, car il quittera tout le programme et l'animation déjà jouée disparaîtra. Je ne peux pas trouver de bonne solution à cela, est-ce que quelqu'un a des idées à ce sujet?Je travaille sur un projet de coque convexe en utilisant processing3, quelqu'un pourrait-il me dire s'il y a toujours moyen de quitter une fonction (pas tout le programme)?
ajouter un 'retour,' ..that doit travailler (sans regarder le reste de votre mise en œuvre). – nullpointer
plus d'infos http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html – nullpointer
Dans le futur, essayez de mettre un [mcve] à la place de votre programme entier. Un programme simple est beaucoup plus facile à déboguer. Essayez également d'utiliser un formatage correct, car votre code est très difficile à lire. –