Skip to content

Instantly share code, notes, and snippets.

@wonderburg7
Created January 26, 2019 14:54
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/0440f25292c4a985c9568835eb3fd33b to your computer and use it in GitHub Desktop.
Save wonderburg7/0440f25292c4a985c9568835eb3fd33b to your computer and use it in GitHub Desktop.
PImage img;
int xspacing = 1; // How far apart should each horizontal location be spaced
int w = 100, w2 = 150; // Width of entire wave
float theta = 0.0; // Start angle at 0
float theta2 = 0.0;
float amplitude = 3.0, amplitude2 = 12.0; // Height of wave
float period = 35, period2 = 150.0;// How many pixels before the wave repeats
float dx, dx2; // Value for incrementing X, a function of period and xspacing
float[] yvalues, yvalues2; // Using an array to store height values for the wave
int ballSize = 9;
static final color BG = -4;
void setup() {
size(1000, 1000);
imageMode(CENTER);
dx = (TWO_PI / period) * xspacing;
dx2 = (TWO_PI / period2) * xspacing;
yvalues = new float[w/xspacing];
yvalues2 = new float[w2/xspacing];
img = loadImage("zen cup.png");
img.resize(width, height);
}
void draw() {
background(BG);
image(img,width/2.0, height/2.0);
calcWave();
renderWave();
saveTransparentCanvas(BG, "img");
}
void calcWave() {
// Increment theta (try different values for 'angular velocity' here
theta += 0.1;
theta2 += 0.05;
// 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;
}
x = theta2;
for (int i = 0; i < yvalues2.length; i++) {
yvalues2[i] = sin(x)*amplitude2;
x+=dx2;
}
}
void renderWave() {
noStroke();
fill(0);
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++) {
pushMatrix();
translate(0,35);
ellipse(height/2+yvalues[x], x*xspacing, ballSize, ballSize);
ellipse(height/2+yvalues[x]-70, x*xspacing+20, ballSize, ballSize);
ellipse(height/2+yvalues[x]+70, x*xspacing+20, ballSize, ballSize);
popMatrix();
if (yvalues[x]+70 == 70.2995){
print(frameCount);
}
}
for (int x = 0; x < yvalues2.length; x++) {
pushMatrix();
translate(0,35);
ellipse(height/2+yvalues2[x], x*xspacing+600, ballSize, ballSize);
popMatrix();
}
}
void saveTransparentCanvas(final color bg, final String name) {
final PImage canvas = get(0,0,height,width);
canvas.format = ARGB;
final color p[] = canvas.pixels, bgt = bg & ~#000000;
for (int i = 0; i != p.length; ++i) if (p[i] == bg) p[i] = bgt;
canvas.updatePixels();
canvas.save("Frame####"+frameCount+".png");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment