We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Breaking a 'for' loop
Page Index Toggle Pages: 1
Breaking a 'for' loop (Read 571 times)
Breaking a 'for' loop
May 14th, 2009, 1:04am
 
I have a function in a class running a for loop iterating another of my functions. I'd like to be able to break it before the loop has finished. My attempt has been to put something like this into the for loop, but that's not working:

(PSEUDO-CODE)

Code:

for (int i = 0; i < 256; i++) {
if (mousePressed) {
println("User cancelled");
i = 256;
}
runtheotherfunction();
}


Pressing the mouse doesn't abort the for loop nor does it print my line. It seems that no input is accepted while the loop is running.

Any suggestions as to how I go about making my function user interruptible? I'm very much a code n00b, so it's probably something trivial.

Thanks,

Thor
Re: Breaking a 'for' loop
Reply #1 - May 14th, 2009, 1:39am
 
Well the first suggestion I'd make is to use 'break' instead of assigning i=256:

http://processing.org/reference/break.html

though I don't think that's going to solve your problem...  Maybe you need to listen for a mousePress outside of your function; though again don't know how well that will fit with your class.

http://processing.org/reference/mousePressed_.html
Re: Breaking a 'for' loop
Reply #2 - May 14th, 2009, 1:58am
 
Thanks, blindfish. Break is the correct way to end the loop. I'll use that.

My problem, though, is that I'm not receiving any mousePressed (nor any keyPressed) events while my for loop is running. I've tried both from inside my function in the class and outside. No input is received/evaluated until that loop has run its course. Could it be due to high cpu load? I must be missing something fundamental.
Re: Breaking a 'for' loop
Reply #3 - May 14th, 2009, 4:58am
 
If you press mouse, I think the event is put in a queue. Then Processing politely waits for the end of draw() to display the result on screen and to process the various enqueued events.
The usual answer to your problem is to treat draw() as a loop itself. You have to add a logic inside to manage the counter and the end or restart of loop.
Re: Breaking a 'for' loop
Reply #4 - May 14th, 2009, 5:44am
 
@PhiLho: Thanks for your interest.

I think I understand what you're saying. In my case I have noLoop() in setup() and then call redraw() at the end of the loop I'm trying to make interruptible. That way draw() cycles to the end and should allow for the mousePressed event to register. But I can't get anything to register while that loop is running. The issue is likely due to my incomplete comprehension of loop(), noLoop() and redraw().

I'm pasting in my code below.

Quote:
/**
 * Peter de Jong attractor render
 * by Thor Froelich.
 * <br>
 * Hold mouse button and drag to adjust parameters.<br>
 * Release mouse to render high quality.
 */


deJongAttractor dj;
boolean stop;

void setup() {
  size(600, 600);
  dj = new deJongAttractor();
  stop = false;
  noFill();
  noLoop();
  smooth();
  colorMode(HSB, 255);
  dj.reparam();
  frameRate(30);
}

void draw() {
  if (keyPressed) {
     stop = true;
     println(stop);
  }
  background(0, 0, 0);
  image(dj.pi, 0, 0, width, height);
}

void mouseDragged() {
  dj.reparam();
}

void mouseReleased() {
  dj.updateloop();
}


(Rest will follow in next post due to character limit per post)
Re: Breaking a 'for' loop
Reply #5 - May 14th, 2009, 5:44am
 
Quote:
class deJongAttractor {
  PImage pi;
  float pa, pb, pc, pd, newx, newy, oldx, oldy, logmaxd;
  int N = width;
  int maxdense = 0;
  int[][] density = new int[N][N];
  float[][] previousx = new float[N][N];

  void construct() {
    //Produces the four variables to pass to the attractor
    float sensitivity = 0.017;
    pa = map(mouseX, 0, width, -2, 2) * sensitivity;
    pb = map(mouseY, 0, height, -2, 2) * sensitivity;
    pc = map(mouseX, 0, width, 2, -2) * sensitivity;
    pd = map(mouseY, 0, height, 2, -2) * sensitivity;
    oldx = width/2;
    oldy = height/2;
  }

  void populate(int s, boolean c) {
    //Populate array with density info with s number of samples
    int samples = s;
    boolean clear = c; //Clear array or not
    if (clear) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          density[i][j] = 0;
          previousx[i][j] = 0;
        }
      }
    }
    for (int i = 0; i < samples; i++) {
      for (int j = 0; j < 10000; j++) {
        //De Jong's attractor
        newx = ((sin(pa * oldy) - cos(pb * oldx)) * N * 0.2) + N/2;
        newy = ((sin(pc * oldx) - cos(pd * oldy)) * N * 0.2) + N/2;
        //Smoothie
        newx += random(-0.001, 0.001);
        newy += random(-0.001, 0.001);
        //If coordinates are within range, up density count at its position
        if ((newx > 0) && (newx < N) && (newy > 0) && (newy < N) ) {
          density[int(newx)][int(newy)] += 1;
          previousx[int(newx)][int(newy)] = oldx;
        }
        oldx = newx;
        oldy = newy;
      }
    }
    //Put maximum density and its log()-value into variables
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (density[i][j] > maxdense) {
          maxdense = density[i][j];
          logmaxd = log(maxdense);
        }
      }
    }
  }

  void updateloop() {
    //Loops the non-clearing update and plotting to produce low-noise render
    for (int i = 0; i < 128; i++) {
      if (stop) {
        println("User cancelled");
        break;
      }
      populate(16, false);
      plot(0);
      pi.blend(0, 0, dj.pi.width, dj.pi.height, 0, 0, dj.pi.width, dj.pi.height, SOFT_LIGHT);
      redraw();
    }
  }

  void reparam() {
    //Fast reparametrization of variables
    dj.construct();
    dj.populate(1, true);
    dj.plot(100);
    redraw();
  }

  PImage plot(int f) {
    int factor = f;

    //Plot image from density array
    pi = createImage(N, N, ARGB);
    pi.loadPixels();
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (density[i][j] > 0) {
          float myhue = map(previousx[i][j], 0, N, 128, 255);
          float mysat = map(log(density[i][j]), 0, logmaxd, 128, 0);
          float mybright = map(log(density[i][j]), 0, logmaxd, 0, 255) + factor;
          color c = color(myhue, mysat, mybright);
          pi.pixels[i * N + j] = c;
        }
      }
    }
    pi.updatePixels();
    return pi;
  }

}

Re: Breaking a 'for' loop
Reply #6 - May 14th, 2009, 8:36am
 
Nice sketch, and I can understand why one might want to stop rendering... (quite slow on my computer).

I can illustrate my advice:
Code:
deJongAttractor dj;
boolean stop;
int stepCounter;

void setup() {
size(600, 600);
dj = new deJongAttractor();
stop = false;
noFill();
// Let it loop
smooth();
colorMode(HSB, 255);
dj.reparam();
frameRate(30);
}

void draw() {
if (!stop) {
//Loops the non-clearing update and plotting to produce low-noise render
stepCounter++;
if (stepCounter > 127) {
stop = true;
return;
}
print(stepCounter + " ");
dj.incrementalupdate();
}
background(0);
image(dj.pi, 0, 0, width, height);
}

void keyPressed() {
stop = true;
}

// Unchanged
void mouseDragged() {
dj.reparam();
}

void mouseReleased() {
dj.updateloop();
}

class deJongAttractor {
// Below, same code
// ...

// Changed code
void updateloop() {
stop = false;
stepCounter = 0;
}

void incrementalupdate() {
populate(16, false);
plot(0);
pi.blend(0, 0, dj.pi.width, dj.pi.height, 0, 0, dj.pi.width, dj.pi.height, SOFT_LIGHT);
}

void reparam() {
//Fast reparametrization of variables
dj.construct();
dj.populate(1, true);
dj.plot(100);
}

// Below, same code
// ...
}
Re: Breaking a 'for' loop
Reply #7 - May 14th, 2009, 12:47pm
 
It worked brilliantly, PhiLho. Thank you so much for your effort, I really appreciate it. Also thanks for the little cleanups. Above and beyond the call of duty.

I've uploaded the sketch here:
thor.abekat.net/wp-content/uploads/dejongattractorv2/index.html
Page Index Toggle Pages: 1