Multiple Sine Waves

Hi,

Im working a variation of the Sine Wave example for a class, and im curious as to how I would be able to make parallel sine waves (add more) AND/OR make these waves diagonal (from top left to right bottom for example). Thank you!

int xspacing = 2; 
int w;             

float theta = 0.45;  
float amplitude = 150; 
float period = 900.0; 
float dx;  
float[] yvalues; 

void setup() {
  fullScreen();
  w = width+16;
  dx = (TWO_PI / period) * xspacing;
  yvalues = new float[w/xspacing];
}

void draw() {
  background(252, 177, 3);
  calcWave();
  renderWave();
}

void calcWave() {
  theta += 0.1;

  float x = theta;
  for (int i = 0; i < yvalues.length; i++) {
    yvalues[i] = sin(x)*amplitude;
    x+=dx;
  }
}

void renderWave() {
  noStroke();
  fill(255);
  for (int x = 0; x < yvalues.length; x++) {
    ellipse(x*xspacing, height/2+yvalues[x], 16, 16);
  }
}
2 Likes

Hi. I think i found something that might work
I basically just added some while loops then switched the X and Y. Your code was very advanced it would take me at least a week to make that! Anyway heres my code that added more waves and vertical waves

int xspacing = 2;
int w;

float theta = 0.45;
float amplitude = 150;
float period = 900.0;
float dx;
float[] yvalues;


int waves = 3;
int repeat = 0;

void setup() {
fullScreen();
w = width+16;
dx = (TWO_PI / period) * xspacing;
yvalues = new float[w/xspacing];
}

void draw() {
background(252, 177, 3);
calcWave();
renderWave();
}

void calcWave() {
theta += 0.1;

float x = theta;
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x)*amplitude;
x+=dx;
}
}

void renderWave() {
noStroke();
fill(255);

repeat = 0;
while(repeat < waves){
for (int x = 0; x < yvalues.length; x++) {
ellipse(x*xspacing, height/2+yvalues[x] + repeat*100, 16, 16);
}
repeat = repeat +1;
}

repeat = 0;
while(repeat < waves){
for (int x = 0; x < yvalues.length; x++) {
ellipse(height/2+yvalues[x] - 200 - repeat*100, x*xspacing + 500, 16, 16);
}
repeat = repeat +1;
}

}

please post code in the
</> Preformatted text button of the editor menu
also it is possibly to edit that manually by using 3 times ā€™ at beginning and end of code

in your code some * missing so the code can not run!

it should look like

int xspacing = 20;
float theta = 0, dtheta = 0.05; // moving speed
float amplitude = 150;
float period = 900.0;
float dx = (TWO_PI / period);
int waves = 3, dy = 100;

void setup() {
  size(800,800); //,FX2D
  //fullScreen();
  noStroke();
  fill(255);
  frameRate(120);
}

void draw() {
  surface.setTitle(nf(frameRate,0,1)+" FPS");            // show FPS
  background(252, 177, 3);
  theta += dtheta;                                       // keep it moving
    for ( int x = 0; x <= width; x += xspacing )          // make many circles
      for ( int repeat=-1; repeat < waves-1; repeat++ )  // here 3 sinus waves
         ellipse(x, height/2 - sin( theta + x * dx ) * amplitude + repeat * dy, 16, 16);
}

Thank you @PhoenixStormJr

@kll, will make sure to post properly, thank you!

1 Like

I would use preffered format but Iā€™m on android and it doesnt copy right. It copies in 1 straight line and i have to add millions of lines just to get it to work. 1 comment messes it all up

thanks for try,
sometimes the code source text is ok,
( like still includes the indentation blanks )
but without the proper </> formatting
shown damaged ā€œā€ "" or not show n100 instead n*100
and NO indentation.

so it might suffice to add manually the 3 ā€œ```ā€
at begin and end of the code ( your phone keyboard have that key? )