Problem with a variable. Don't know where to declare

Hello,
I made a program with works when two variables (here two pulsations) are declared before the setup with a constant value.

Like that:

float w1 = 0.1;
float w2 = 0.11;

But actually I want the second value is a fonction of the first value.

Like that:

// float w1 = 0.1;
// float w2 = 11/10*w1;

And in the future the pulsation w1 will depending of the mouse.

Thanks for helping me.

float w1  = 0.1;
float w2  = 0.11; //11/10*w1;

int x;

int Um=100;
int Vm=100;

int num=1000;

float phiU= 2*PI;
float phiV= 2*PI;

float X1t= Um/2* cos (w1*x+phiU)-Vm/2*cos (w2*x+phiV);
float X2t= Um/2* cos (w1*x+phiU)+Vm/2*cos (w2*x+phiV);

void setup() {
  size (1000, 800);
  frameRate(30);
  colorMode(RGB, 255, 255, 255, 100);
  
  // float w1  = 0.01;
  // float w2  = 11/10*w1;
  //noStroke();
  //fill(0, 50, 0);
  
 
}

void draw() {
  
  background(255);
   calcWave();
  renderWave();
}

void  calcWave (){
  
 
   
 
  
  //  stroke(val);
  
   X1t= Um/2* cos (w1*x+phiU)-Vm/2* cos (w2*x+phiV);
   X2t= Um/2* cos (w1*x+phiU)+Vm/2* cos (w2*x+phiV);
  
 
  
  
  }

void   renderWave() {
  
 // float   w1  = 0.1;
 // float   w2  = 11/10*w1;
  
   
 
  
     for(int i=1; i<num; i++) { 
   
    float x = float(i/1000)+i;
 
   X1t= Um/2* cos (w1*x+phiU)-Vm/2* cos (w2*x+phiV);
   X2t= Um/2* cos (w1*x+phiU)+Vm/2* cos (w2*x+phiV);
  
 
 
    noStroke();
    fill(0,num,50);
    ellipse (x, X1t+200, 5,5); 
    
    fill(0,50,num);
    ellipse (x, X2t+400, 5,5); 
     
     print(w1,X1t);
     
   }  
    
    }
    
    

Hey,

Just declare them like this :

float w1, w2;
int x;
int Um=100;
int Vm=100;
int num=1000;
float phiU= 2*PI;
float phiV= 2*PI;
float X1t, X2t;

void setup() {
  size (1000, 800);
  frameRate(30);
  colorMode(RGB, 255, 255, 255, 100);
}

void draw(){
  //For example
  w1 = mouseX;
  w2 = 11/10*w1;
  X1t= Um/2* cos (w1*x+phiU)-Vm/2*cos (w2*x+phiV);
  X2t= Um/2* cos (w1*x+phiU)+Vm/2*cos (w2*x+phiV);

  //Draw something...
}

So in the draw loop, the values of w1 and w2 are changing all the time (and X1t/X2t too).

PS : it’s a Processing coding question, not a p5.js one

Hi Josephhh,
Thanks for your quick answer but:
When I declare variables in void draw(); I don’t have good value of w1 and w2, unlike before setup() with w1=0.1 and w2=0.11.
I don’t understand why?

float w1, w2;
int x;
int Um=100;
int Vm=100;
int num=1000;
float phiU= 2*PI;
float phiV= 2*PI;
float X1t, X2t;

void setup() {
  size (1000, 800);
  frameRate(30);
  colorMode(RGB, 255, 255, 255, 100);
}

void draw(){
  //For example
  w1 = 0.1;
  w2 = 11/10*w1;
  x=x+1;
  X1t= Um/2* cos (w1*x+phiU)-Vm/2*cos (w2*x+phiV);
  X2t= Um/2* cos (w1*x+phiU)+Vm/2*cos (w2*x+phiV);

  //Draw something...
  noStroke();
    fill(0,num,50);
    ellipse (x, X1t+200, 5,5); 
    
    fill(0,50,num);
    ellipse (x, X2t+400, 5,5); 
     
     println(w1);
     println(w2);
}

that are constants,

but for info about constants see
final / Reference / Processing.org

and it does not matter where you declare them
at the beginning

float  w1 = 0.1;
float  w2 = 11/10*w1;

or setup or draw,
i see same picture.

but HA!

float  w1 = 0.1;
float  w2 = 11.0/10.0*w1;

that produces different results!

but as i not understood what you want it to look like
my version might be more funny as use mouse X and Y and mousewheel


float w1, w2;
float w12twist = 10.0;
//int x=1;
int Um=100;
int Vm=100;
int num=255;
float phiU=HALF_PI;
float phiV=HALF_PI;
float X1t, X2t;

void setup() {
  size (500, 500);
//  frameRate(30);
//  colorMode(RGB, 255, 255, 255, 100);
  println("use mouse X for freq, mouse Y for VM phase and mouseWheel for w1/w2");
}

void draw() {
  background(150,150,0);
  //For example
  for (int x=0;x<500;x++){
  w1 = mouseX/500.0/PI;
  w2 = 11.0/w12twist*w1;
  phiV=PI*mouseY/500.0;
  X1t= Um/2*cos(w1*x+phiU) - Vm/2*cos(w2*x+phiV);
  X2t= Um/2*cos(w1*x+phiU) + Vm/2*cos(w2*x+phiV);

  //Draw something...

  noStroke();
  fill(0, num,50);
  ellipse (x,X1t+150, 5, 5); 
  fill(0,50, num);
  ellipse (x,X2t+350, 5, 5); 
  
  //println(w1, X1t, X2t);
  }
}

void mouseWheel(MouseEvent event) {
  w12twist += event.getCount()/5.0;  
}


You are trying to have both constants and variables at the same time, which is not possible.
If you plan to have w1 depending of the mouse value, you need to affect w1 and w2 in the draw function.
Otherwise just declare w1 and w2 as constants outside the functions.

your integer calc was the killer

Yes you are right.
Just had to say :

w2 = 1.1 * w1;
//or
w2 = 11.0/10 * w1;

Yep Guys.
Thanks you all.
It works perfectly, now I have to find the formula to simulate, interaction between several pulsation, not only with two.
I’m looking for a model which simulate several N coupled oscillator and not just two.
If you have any idea, I will study it.
Thanks again.

lucky you like it,
one way would be just to create random noise and add it,
or more sin waves,
here you should study the harmonics…
like how to make a saw or rectangle wave form by adding harmonics,

now from the coding there could be also a interesting way to use
inverse FFT instead manually adding sinus ( but its actually the same. )

and possibly adding key strokes to start stop adding NOTES
like a piano.

and when your created wave is very beautiful,
you might want to hear it too.