# Multiple Sine Waves

**URL:** <https://discourse.processing.org/t/multiple-sine-waves/14332>\
**Category:** Coding Questions\
**Created:** [October 1, 2019, 1:32am UTC](https://discourse.processing.org/t/multiple-sine-waves/14332 "2019-10-01T01:32:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paz](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@paz](https://discourse.processing.org/u/paz)\
**Post date:** [October 1, 2019, 1:32am UTC](https://discourse.processing.org/t/multiple-sine-waves/14332/1 "2019-10-01T01:32:04Z")

</div>

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!

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 1, 2019, 3:03am UTC](https://discourse.processing.org/t/multiple-sine-waves/14332/2 "2019-10-01T03:03:41Z")

</div>

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

```auto
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;
}

}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 1, 2019, 4:16am UTC](https://discourse.processing.org/t/multiple-sine-waves/14332/3 "2019-10-01T04:16:53Z")

</div>

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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![paz](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@paz](https://discourse.processing.org/u/paz)\
**Post date:** [October 1, 2019, 10:40am UTC](https://discourse.processing.org/t/multiple-sine-waves/14332/4 "2019-10-01T10:40:39Z")

</div>

Thank you @PhoenixStormJr

---

<div class="post-metadata">

**Author:** ![paz](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@paz](https://discourse.processing.org/u/paz)\
**Post date:** [October 1, 2019, 10:41am UTC](https://discourse.processing.org/t/multiple-sine-waves/14332/5 "2019-10-01T10:41:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 5, 2019, 4:46pm UTC](https://discourse.processing.org/t/multiple-sine-waves/14332/6 "2019-10-05T16:46:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 5, 2019, 9:14pm UTC](https://discourse.processing.org/t/multiple-sine-waves/14332/7 "2019-10-05T21:14:13Z")

</div>

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? )
