Symmetric overlapping sine wave code

int n = 5; // NO. OF STRANDS
float[] translate;//OFFSET OF INDIVIDUAL STRAND
float[] amp;// AMPLITUDE OF INDIVIDUAL STRAND
float[] period;// PERIOD OF INDIVIDUAL STRAND RELATIVE TO STANDARD PERIOD
float frequency;
float per;//STANDARD PERIOD
float angle;
float startWave;
float endWave;

void setup() {
  size(800, 800);
  smooth();
  stroke(255);
  strokeWeight(0.7);
  noFill();  
  frequency = 4;
  per = height/frequency;
  amp       = new float[n];
  translate = new float[n];
  period    = new float[n];
 void draw() {
  background(0);
  translate(width/2, 0);
  //line(0, 0, 0, height);
 
  calcamptransperiod();
  drawWave(1);
  drawWave(-1);
}

void calcamptransperiod() {
  for (int i=0; i<n; i++) {
    float maxamp = 105.0;
    amp[i]       = maxamp-(i*(maxamp/6.6));
    float offset = 15.0;
    translate[i] = i*offset;
  }
  for (int i=0; i<n; i++) {
    period[i] =(per-translate[i]) + translate[(n-1)-i];
    //println("amp"+i+"="+amp[i]);
    //println("translate"+i+"="+translate[i]);
    //println("period"+i+"="+period[i]);
    //println(" ");
  }
}

void drawWave(int direction) {
  for (int f=0; f<frequency; f++) {
    //line(-width/2, height/frequency*f, width, height/frequency*f);
    for (int i=0; i<n; i++) {
      startWave = per*f;
      endWave   = per*f+period[i];
      angle=0.0;
      for (float y=startWave; y<endWave; y++) {      
        pushMatrix();
        translate(0, translate[i]);
        float x = sin(angle)*amp[i]*direction;
        point(x, y);
        popMatrix();
        angle+=PI/period[i];
      }
    }
    direction = - direction;
  }
}

I am trying to create overlapping sine waves . I have achieved some result but its not symmetric.
It doesn’t start & end at same spot. I want to achieve symmetricity along x-axis & y-axis both.
I don’t know if I am missing something that is awfully simple.

If you reduce the number of strands to 1 your sketch is symmetric. It seems you haven’t taken the extra strands in account with your formula. A quick fix would be to move your sketch up a bit with translate, but I guess you want all strands to start at y = 0 instead?

i failed completely, but i love it !

1 Like
int n =5; // NO. OF STRANDS
float frequency=5;
float per;//STANDARD PERIOD
float peroffset;
float offset = 15.0;

float[] translate;//OFFSET OF INDIVIDUAL STRAND
float[] amp;// AMPLITUDE OF INDIVIDUAL STRAND
float[] period;// PERIOD OF INDIVIDUAL STRAND RELATIVE TO STANDARD PERIOD

void setup() {
  size(600, 800);
  smooth();
  stroke(255);
  strokeWeight(0.7);
  noFill();  
  //frameRate(15);
  //noLoop();
  peroffset = offset*(n-1);// OFFSET OF LAST STRAND   
  //SUBSTRACTING MAXIMUM OFFSET FROM STANDARD PERIOD TO ACHIEVE SYMMETRICITY ALONG X_AXIS (COMPENSATING FOR TRANSLATE FROM STANDARD PERIOD)
  per =(height-peroffset)/frequency;

  amp       = new float[n];
  translate = new float[n];
  period    = new float[n];
}
void draw() {
  background(0);
  translate(width/2, 0);
  //line(0, 0, 0, height);

  calcamptransperiod();
  drawWave(1);
  drawWave(-1);
}

void calcamptransperiod() {
  for (int i=0; i<n; i++) {
    float maxamp = offset*n;
    amp[i]       = maxamp-(i*(maxamp/5.6));
    translate[i] = i*offset;
  }
  for (int i=0; i<n; i++) {
    period[i] =(per-translate[i]) + translate[(n-1)-i];
    //println("amp"+i+"="+amp[i]);
    //println("translate"+i+"="+translate[i]);
    //println("period"+i+"="+period[i]);
    //println(" ");
  }
}

void drawWave(int direction) {
  for (int f=0; f<frequency; f++) {
    //line(-width/2, per*f, width, per*f);
    for (int i=0; i<n; i++) {
      float startWave = per*f;
      float endWave   = per*f+period[i];
      float angle=0.0;
      for (float y=startWave; y<endWave; y++) {            
        pushMatrix();
        beginShape(); 
        translate(0, translate[i]);
        float x = pow(sin(angle), 1)*amp[i]*direction;
        vertex(x, y);
        endShape();
        popMatrix();        
        angle+=PI/period[i];
      }
    }
    direction = - direction;
  }
}

found the solution. Compensating the maximum translate i.e offset of the last strand in standard
period.

3 Likes

I had some fun with your code and made it animated and 3D.

Snapshot as requested:

Just some minor tweaks to make it 3D.
Sharing with your permission:

//https://discourse.processing.org/t/symmetric-overlapping-sine-wave-code/16565

int n =5; // NO. OF STRANDS
float frequency=5;
float per;//STANDARD PERIOD
float peroffset;
float offset = 15.0;

float[] translate;//OFFSET OF INDIVIDUAL STRAND
float[] amp;// AMPLITUDE OF INDIVIDUAL STRAND
float[] period;// PERIOD OF INDIVIDUAL STRAND RELATIVE TO STANDARD PERIOD

float rotWorld;   //glv

void setup() {
  size(600, 800, P3D);
  //smooth();
  stroke(255, 255, 0);
  strokeWeight(3);
  noFill();  
  //frameRate(15);
  //noLoop();
  peroffset = offset*(n-1);// OFFSET OF LAST STRAND   
  //SUBSTRACTING MAXIMUM OFFSET FROM STANDARD PERIOD TO ACHIEVE SYMMETRICITY ALONG X_AXIS (COMPENSATING FOR TRANSLATE FROM STANDARD PERIOD)
  per =(height-peroffset)/frequency;

  amp       = new float[n];
  translate = new float[n];
  period    = new float[n];
}

void draw() {
  background(0);
  translate(width/2, 0);
  //line(0, 0, 0, height);

  calcamptransperiod();
  
  rotWorld += TAU/200; //glv
  rotateY(rotWorld);   //glv
  pushMatrix();        //glv
  drawWave(2);
  rotateY(TAU/4);      //glv
  drawWave(-2);
  popMatrix();         //glv 
  }

void calcamptransperiod() {
  for (int i=0; i<n; i++) {
    float maxamp = offset*n;
    amp[i]       = maxamp-(i*(maxamp/5.6));
    translate[i] = i*offset;
  }
  for (int i=0; i<n; i++) {
    period[i] =(per-translate[i]) + translate[(n-1)-i];
    //println("amp"+i+"="+amp[i]);
    //println("translate"+i+"="+translate[i]);
    //println("period"+i+"="+period[i]);
    //println(" ");
  }
}

void drawWave(int direction) {
  for (int f=0; f<frequency; f++) {
    //line(-width/2, per*f, width, per*f);
    for (int i=0; i<n; i++) {
      float startWave = per*f;
      float endWave   = per*f+period[i];
      float angle=0.0;
      //println(startWave, endWave);
      for (float y=startWave; y<endWave; y++) {            
        //pushMatrix();
        beginShape(POINTS);                        //glv 
        //translate(0, translate[i]);
        float x = pow(sin(angle), 1)*amp[i]*direction;
        vertex(x, y+translate[i]);                 //glv
        //vertex(x, y);               
        endShape();
        //popMatrix();        
        angle+=PI/period[i];
      }
    }
    direction = - direction;
  }
}

:slight_smile:

2 Likes