Skip to content

Instantly share code, notes, and snippets.

@wonderburg7
Last active January 24, 2019 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wonderburg7/213e513e99b235d01646a9df9bfc2da8 to your computer and use it in GitHub Desktop.
Save wonderburg7/213e513e99b235d01646a9df9bfc2da8 to your computer and use it in GitHub Desktop.
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment