Skip to content

Instantly share code, notes, and snippets.

@JoshuaSullivan
Created February 19, 2016 20:49
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 JoshuaSullivan/3fd25d3092d2ee93d815 to your computer and use it in GitHub Desktop.
Save JoshuaSullivan/3fd25d3092d2ee93d815 to your computer and use it in GitHub Desktop.
A Processing 3.0 sketch animating some waves!
class SingleWave {
float SEGMENTS_PER_CYCLE = 48.0;
float wavelength, r, dr;
color drawColor;
SingleWave(float wavelength, color drawColor) {
this.wavelength = wavelength;
this.drawColor = drawColor;
this.r = random(TWO_PI);
this.dr = random(-PI / 48.0, PI / 48.0);
}
void changeFrequency(float dr) {
this.dr = dr;
}
void update() {
this.r += this.dr;
}
void drawWithSize(float w, float h) {
float r0 = this.r;
float rChange = TWO_PI / SEGMENTS_PER_CYCLE;
float segWidth = this.wavelength / SEGMENTS_PER_CYCLE;
float x0 = 0.0;
float amplitude = h / 2.0;
float yCenter = amplitude;
stroke(this.drawColor);
strokeWeight(2);
while (x0 < w) {
float x1 = x0 + segWidth;
float r1 = r0 + rChange;
float y0 = yCenter + amplitude * sin(r0);
float y1 = yCenter + amplitude * sin(r1);
line(x0, y0, x1, y1);
x0 = x1;
r0 = r1;
}
}
}
SingleWave[] waves;
int WAVE_COUNT = 6;
void setup() {
size(1000, 400);
colorMode(HSB, 360, 100, 100);
waves = new SingleWave[WAVE_COUNT];
float deltaC = 360.0 / WAVE_COUNT;
for(int i = 0; i < WAVE_COUNT; i++) {
color c = color(deltaC * i, 100, 100);
SingleWave wave = new SingleWave(random(200.0, 800.0), c);
waves[i] = wave;
}
background(0);
}
void draw() {
clear();
pushMatrix();
translate(0.0, 50.0);
for(int i = 0; i < WAVE_COUNT; i++) {
waves[i].update();
waves[i].drawWithSize(1000, 300);
}
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment