int xspacing = 32; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
float theta = 0.0; // Start angle at 0
float amplitude = 150.0; // Height of wave
float period = 500.0; // How many pixels before the wave repeats
float dx; // Value for incrementing X, a function of period and xspacing
float[] yvalues; // Using an array to store height values for the wave
void setup() {
size(1000, 1000);
w = width+250;
dx = (TWO_PI / period) * xspacing;
yvalues = new float[w/xspacing];
strokeWeight(10);
}
void draw() {
background(0);
calcWave();
renderWave();
}
void calcWave() {
// Increment theta (try different values for 'angular velocity' here
theta += 0.02;
// For every x value, calculate a y value with sine function
float x = theta;
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x)*amplitude;
x+=dx;
}
}
void renderWave() {
noStroke();
fill(255);
stroke(255);
strokeCap(SQUARE);
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++) {
// ellipse(x*xspacing-250, height/2+yvalues[x], 16, 16);
// ellipse(x*xspacing, (height/2+yvalues[x]), 16, 16);
ellipse(height/2+yvalues[x], x*xspacing-256, 16, 16);
ellipse(height/2+yvalues[x], x*xspacing, 16, 16);
// line(height/2+yvalues[x], x*xspacing, height/2+1, x*xspacing);
// line(height/2+yvalues[x], x*xspacing-256, height/2-1, x*xspacing-256);
// line(height/2+yvalues[x], x*xspacing-256, height/2+sin(dx)*amplitude, (x+1)*xspacing-256);
}
}