107



int xspacing = 8;
int w;
int maxwaves = 4;

float theta = 0.0;
float[]amplitude = new float[maxwaves];
float[]dx = new float[maxwaves];
float[]yvalues;

void setup(){
size(600,200);
smooth();
w = width+16;
noStroke();
reset();
yvalues = new float[w/xspacing];
}

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

void calcWave(){
theta += 0.04;
for(int i = 0; i < yvalues.length; i++){
yvalues[i] = 0.0f;
}

for(int j = 0; j < maxwaves; j++){
float x = theta;
for(int i = 0; i < yvalues.length; i++){
if(j%2 ==0) yvalues[i]+= sin(x)*amplitude[j];
else yvalues[i] += cos(x)*amplitude[j];
x += dx[j];
}
}
}

void renderWave(){
for(int x = 0; x < yvalues.length; x++){
fill(255,0,0,50);
ellipseMode(CENTER);
ellipse(x*xspacing,width/6+yvalues[x],16,16);
}
}

void mousePressed(){
reset();
}

void reset(){
for(int i = 0; i < maxwaves; i++){
amplitude[i] = random(10,30);
float period = random(100,800);
dx[i] =(TWO_PI/period)*xspacing;
}
}

コメント