Limited number of iterations in for loop/draw?!

I have a code that draws a line inside a for loop in void draw() and it works fine, until the number of iterations of the loop times the number of frames equals 25,000,000. Then the line displays incorrectly. For example:

Using for (int i=0; i<=5000; i++) it stops displaying correctly at FrameCount = 5000 (5,000 * 5,000 = 25,000,000).

Using for (int i=0; i<=10000; i++) it stops displaying correctly at FrameCount = 2500 (10,000 * 2500 = 25,000,000).

Using for (int i=0; i<=50000; i++) it stops displaying correctly at FrameCount = 500 (50,000 * 500 = 25,000,000).

Is there a way to just keep it running properly? Thanks.

Answers

  • can you post a runnable example? something minimal that shows the problem, something we can run

  • edited May 2016

    Sure @koogs, here it is:

    float x;
    float y;
    float z=0.0001;
    float m =200;
    float xOld = x;
    float yOld ;
    float r;
    float theta;
    
    void setup() {
      size(400, 400, P2D);
    }
    
    void draw() {
      background(0);
    
      translate(width/2, height/2);
    
      for (int i=0; i<=50000; i++) {  //LOOK HERE
        r = cos(theta*z);
    
        x = r*cos(theta);
        y = r*sin(theta);
    
        stroke(255);
        line(x*m, y*m, xOld*m, yOld*m);
    
        xOld = x;
        yOld = y;
    
        theta+=PI*0.01;
      }
      println("theta = "+theta);
      println("frameCount = "+frameCount); //LOOK HERE
      println("x = "+ x);
      println("y = "+ y);
    }
    
  • beware printing too much to the console - it'll lock up your computer (as it has just done to mine)

    i put a couple of lines of debug (enabled by keypress) that showed x, y, oldX and oldY and pressed the button after it stopped displaying and it showed that x and oldX and y and oldY were the same. i think it stops drawing because it's drawing a 0 length line. but i could be wrong - because my computer locked up and i'm wary of running it again 8)

    i wonder if it's a precision problem, that adding and multiplying by such small values is causing problems.

  • yeah, look, the last three values it printed before i stopped it:

    theta = 1048576.0
    frameCount = 568
    x = -0.35511753
    y = -0.12435142
    
    theta = 1048576.0
    frameCount = 569
    x = -0.35511753
    y = -0.12435142
    
    theta = 1048576.0
    frameCount = 570
    x = -0.35511753
    y = -0.12435142
    

    theta isn't increasing, x and y aren't changing...

  • I think the problem could be in the size of theta, because the last value is 1048576, and a MB = 1048576 B.
    What do you think? Is there a way to fix this?

    @ koogs Thanks for taking the time

Sign In or Register to comment.