for loop question
              in 
             Programming Questions 
              •  
              11 months ago    
            
 
           
             Hello All!
            
            
New to programming and Processing so thanks for your patience/help.
            
Trying to generate 15 squares oscillating with simple harmonic motion but with different periods, each is essentially a pendulum. Here is an example that works by entering the periods of each oscillator manually:
            
            
I am trying to do this with a for loop but am only getting the first oscillator:
            
            
            
Many thanks, processing is Great! 
            
            
 
           
 
            
           New to programming and Processing so thanks for your patience/help.
Trying to generate 15 squares oscillating with simple harmonic motion but with different periods, each is essentially a pendulum. Here is an example that works by entering the periods of each oscillator manually:
- void setup() {
 size(800,340);
 }
 color c = color(255);
 float x = 400;
 void draw()
 {
 background(0);
 noStroke();
 int l = millis();
 fill(c);
 rectMode(CENTER);
 rect( x +( sin((cos((2*(PI*(millis())))/4013))))*300,20,20,20);
- /* generates a rect that moves along x axis under simple harmonic motion with period 4013 etc*/
 
-   rect( x +( sin((cos((2*(PI*(millis())))/3822))))*300,40,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/3648))))*300,60,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/3489))))*300,80,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/3344))))*300,100,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/3210))))*300,120,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/3086))))*300,140,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/2973))))*300,160,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/2866))))*300,180,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/2768))))*300,200,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/2675))))*300,220,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/2589))))*300,240,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/2508))))*300,260,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/2432))))*300,280,20,20);
 rect( x +( sin((cos((2*(PI*(millis())))/2361))))*300,300,20,20);
 }
I am trying to do this with a for loop but am only getting the first oscillator:
- void setup() {
 size(800,340);
 }
 color c = color(255);
 void draw() {
 background (0);
 noStroke();
 fill(c);
 for( int i = 0 ; i <= 14 ; i = i + 1 ) {
 float l_1 = 4;
 float x = 350;
 int m = millis();
 float l = l_1 * ( pow ( ( 20 / ( 20 + i )) , 2 ) );
 float t = TWO_PI * sqrt( ( l / 9.80665));
 float tn = t * 1000; // period of ith oscillator in milliseconds
 float p = sin ( ( cos ( ( 2 * ( PI * m ) ) / tn ) ) ); // x position of ith oscillator at millis() with values between +-1
 float pn = x + (p * 300); // centers each oscillator on screen and moves according to p through +-300
 rectMode(CENTER);
 rect( pn , (i + 1) * 20 , 20 , 20 );
 }
 }
Many thanks, processing is Great!
 
             
              
              1  
            
 
            
 
 
           
 
            