Skip to content

Instantly share code, notes, and snippets.

@drewlustro
Last active August 29, 2015 14:02
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 drewlustro/2edaabb00ceb25331db5 to your computer and use it in GitHub Desktop.
Save drewlustro/2edaabb00ceb25331db5 to your computer and use it in GitHub Desktop.
Waves MR Demo
int xspacing = 3; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
int maxwaves = 4; // total # of waves to add together
int numwaves = 15; // total # of separate waves
float theta = 0.0;
float[] amplitude = new float[maxwaves]; // Height of wave
float[] dx = new float[maxwaves]; // Value for incrementing X, to be calculated as a function of period and xspacing
float[] yvalues; // Using an array to store height values for the wave (not entirely necessary)
float[] xpow = new float[numwaves];
float[] ypow = new float[numwaves];
float[] radius = new float[numwaves];
PImage img;
void setup() {
size(600, 800);
frameRate(15);
colorMode(RGB, 255, 255, 255, 100);
w = width+1000;
for (int i = 0; i < maxwaves; i++) {
amplitude[i] = random(10,30);
float period = random(100,300); // How many pixels before the wave repeats
dx[i] = (TWO_PI / period) * xspacing;
}
for(int j = 0; j < numwaves; j++){
xpow[j] = random(.99, 1.01);
ypow[j] = random(.95, .99);
radius[j] = random(3, 15);
}
yvalues = new float[w/xspacing];
img = loadImage("mr-icon-white.png");
}
void draw() {
background(0);
calcWave();
renderWave();
image(img, 490, 720);
}
void mousePressed(){
saveFrame("waves.jpg");
}
void calcWave() {
// Increment theta (try different values for 'angular velocity' here
theta += .04;
// Set all height values to zero
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = 0;
}
// Accumulate wave height values
for (int j = 0; j < maxwaves; j++) {
float x = theta;
for (int i = 0; i < yvalues.length; i++) {
// Every other wave is cosine instead of sine
if (j % 2 == 0) yvalues[i] += sin(x)*amplitude[j];
else yvalues[i] += cos(x)*amplitude[j];
x+=dx[j];
}
}
}
void renderWave() {
// A simple way to draw the wave with an ellipse at each location
noStroke();
fill(255,50);
ellipseMode(CENTER);
float vertX; // = x * xspacing;
float vertY; // = height + yvalues[x];
for (int x = 0; x < yvalues.length; x++) {
vertX = x * xspacing;
vertY = height + yvalues[x];
for (int z = 0; z < xpow.length; z++) {
ellipse(vertX,vertY,radius[z],radius[z]);
vertX = pow(vertX, xpow[z]);
vertY = pow(vertY, ypow[z]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment