Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

2つの波で囲まれた区間

/**
* Region Shaped By Two Waves
*
* @author aa_debdeb
* @date 2016/02/07
*/

Wave wave1;
Wave wave2;

void setup(){
  size(500, 500);
  frameRate(30); 
  
  wave1 = new Wave();
  wave2 = new Wave();
}

void draw(){
  background(0);
  translate(0, height / 2);
  noFill();
  stroke(0, 250, 154);
  strokeWeight(4);
  
  beginShape();
  for(int w = 0; w <= width; w++){
    float a = w * 0.01 + frameCount * 0.1;
    float h = wave1.getValue(a);
    vertex(w, h);
  }  
  endShape();
  
  beginShape();
  for(int w = 0; w <= width; w++){
    float a = w * 0.01 + frameCount * 0.1;
    float h = wave2.getValue(a);
    vertex(w, h);
  }  
  endShape();
  
  strokeWeight(2);
  for(int w = 0; w <= width; w += 5){
    float a = w * 0.01 + frameCount * 0.1;
    float h1 = wave1.getValue(a);
    float h2 = wave2.getValue(a);
    line(w, h1, w, h2);
  }
}

class Wave{
  ArrayList<Float> amps;
  ArrayList<Float> freqs;
  ArrayList<Float> phases;
  Wave(){
    amps = new ArrayList<Float>();
    freqs = new ArrayList<Float>();
    phases = new ArrayList<Float>();
    for(int i = 0; i < 20; i++){
      amps.add(random(50));
      freqs.add(random(10));
      phases.add(random(TWO_PI));
    }
  }
  
  float getValue(float t){
    float value = 0;
    for(int i = 0; i < 10; i++){
      value += amps.get(i) * sin(freqs.get(i) * t + phases.get(i));
    }
    return value;
  }
}