Pause in for loop doesn't function
in
Programming Questions
•
1 year ago
Hello,
I want to use a for loop to move a drawing between two points. However the loop is too fast to see all the positions. So I am trying to insert a pause in the loop but it doesn't work. Is there any solution compatible with a for loop ?
Thank you in advance.
I want to use a for loop to move a drawing between two points. However the loop is too fast to see all the positions. So I am trying to insert a pause in the loop but it doesn't work. Is there any solution compatible with a for loop ?
Thank you in advance.
- /*
- Circle moving between two points chosen among a set of random ones
- */
- PImage monImage;
- final int imageWidth = 100;
- final int imageHeight = 100;
- final int displayWidth = 500;
- final int displayHeight = 500;
- color myColor;
- PVector startPoint = new PVector() ;
- PVector endPoint = new PVector() ;
- PVector currentPoint = new PVector();
- final int numSteps = 100;
- ArrayList possiblePoints = new ArrayList();
- int currentTime;
- void setup() {
- size(displayWidth, displayHeight);
- monImage = createImage(imageWidth, imageHeight, RGB);
- // random filling of image
- for (int i = 0; i < imageWidth; i++) {
- for (int j = 0; j < imageHeight; j++) {
- // creates random points and stores them
- int test = int(int(random(1000)) >= 999);
- if (test == 1) {
- myColor = color(255, 0, 0);
- possiblePoints.add(new PVector(i,j));
- } else {
- myColor = color(255, 255, 255) ;
- }
- monImage.pixels[(j*imageWidth)+i] = myColor;
- }
- }
- shapeMode(CENTER);
- fill(255, 255, 255, 127);
- stroke(0, 0, 0, 255);
- }
- void draw() {
- monImage.updatePixels();
- // displacement
- // choice of two random points within the ones generated in setup
- startPoint = (PVector) possiblePoints.get((int(random(possiblePoints.size()))));
- endPoint = (PVector) possiblePoints.get((int(random(possiblePoints.size()))));
- /*
- println ("Start point : "+startPoint.x+" , "+startPoint.y);
- println ("End point : "+endPoint.x+" , "+endPoint.y);
- */
- // moving circle between start and end point
- for (int i = 0; i < numSteps; i++) {
- noSmooth();
- image(monImage,0,0, displayWidth, displayHeight);
- smooth();
- currentPoint.x = (startPoint.x * (1 - (i/numSteps))) + (endPoint.x * (i/numSteps)) ;
- currentPoint.y = (startPoint.y * (1 - (i/numSteps))) + (endPoint.y * (i/numSteps)) ;
- ellipse(currentPoint.x, currentPoint.y, 10, 10);
- // Short pause between iterations - doesn't work
- currentTime = millis();
- while (millis() < currentTime+10) {
- println(millis());
- }
- }
- }
1