Can`t animate data

edited January 2016 in Questions about Code

Hello. I got a prinsipal mistake of making code. Please tell me, why i cant make animation with this code? I`ve got on the screen only the last loop realization.

    float[] x = new float[36000];
    float[] y = new float[36000];
    float rad = 450;

    void setup()
    {
     size(1000,1000); 
     translate (500,500);
     for (int i = 0 ; i < 36000 ; i ++)
     {
      x[i] =  cos(radians(i)) * rad;
      println(x[i]);
      y[i] =  sin(radians(i)) * rad;
      point (x[i] , y[i]);
      //line (x[i] , y[i] , x[i+1] , y[i+1] );
     }
    }

    void draw()
    {
     background(0);
      translate (500,500);
      //rotate(0.95);
      for (int n = 0; n <= 2; n = n + 1)
      {
     cardio (n);
     //delay(100); 
      }
    }

    void cardio (int n)
    {
      background(0);  
      for (int i = 0; i < 360; i++)
      {
         //point (y[i]+10 , x[i] + 10);
       // x[i] = c;
      //line (0 , 0 , c , c );
      stroke(204, 102, 0);
      line (x[i] , y[i] , x[i * n] , y[i * n ] );
      }
    }

rheyu

Tagged:

Answers

  • edited January 2016 Answer ✓

    The screen only updates at the end of draw(). You don't need a for loop in draw() to do the 100 different images - draw() itself is the loop that you want.

    Here's a working version with a few other fixes, like adding a delay:

    float[] x = new float[36000]; 
    float[] y = new float[36000]; 
    float rad = 450;
    
    int n = 0;
    int time = 0;
    
    void setup() {
      size(1000, 1000); 
      translate (500, 500);
      for (int i = 0; i < x.length; i++) {
        x[i] =  cos(radians(i)) * rad;
        y[i] =  sin(radians(i)) * rad;
      }
    }
    
    void draw() {
      if ( millis() > time ) {
        time = millis() + 100;
        background(0);
        translate (500, 500);
        cardio (n);
        n++;
        n%=100;
      }
    }
    
    void cardio (int n) {
      stroke(204, 102, 0);
      for (int i = 0; i < 360; i++)
      {
        line (x[i], y[i], x[i * n], y[i * n ] );
      }
    }
    
  • Yet one question please: what does 24 line (n%=100;) mean? I commented it, and code working =).

    Thanks!

  • Answer ✓

    n%=100 means "subtract 100 from n until n is less than 100". This makes sure that the value of n stays between 0 and 100 (without equaling 100).

  • edited January 2016

    Hmm... Isn't that explanation too abstract? Operator modulo % is the remainder of a division: ~O)
    https://Processing.org/reference/modulo.html

  • Meh. Same thing.

Sign In or Register to comment.