Skip to content

Instantly share code, notes, and snippets.

@marcedwards
Last active May 6, 2020 17:45
Show Gist options
  • Save marcedwards/8671f66295d1cdfbb066fa72737332a9 to your computer and use it in GitHub Desktop.
Save marcedwards/8671f66295d1cdfbb066fa72737332a9 to your computer and use it in GitHub Desktop.
Some wiggly voronoi in Processing
//
// Some wiggly voronoi.
// Created using Processing 3.5.4.
//
// Code by @marcedwards from @bjango.
//
// A GIF of this code can be seen here:
// https://twitter.com/marcedwards/status/1258000317804732416
//
color[] vc = new color[100];
float[] vx = new float[100];
float[] vy = new float[100];
void setup() {
size(400, 400, P2D);
frameRate(30);
}
void draw() {
int frames = 220;
// Load in the position of the points
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
float xoff = valueNoise(i, j, 0) * frames;
float yoff = valueNoise(i, j, 8) * frames;
float xdis = sin(Ease.hermite5(timeBounce(frames, xoff)) * TAU) * 0.5;
float ydis = sin(Ease.hermite5(timeBounce(frames, yoff)) * TAU) * 0.5;
float altl = (j % 2) * 0.8;
float x = (i + xdis - altl) * 70;
float y = (j + ydis) * 70;
float v = Ease.hermite5(Ease.tri(gradientAngular(x, y, 1 - timeLoop(frames / 2))));
int item = i + j * 10;
vx[item] = x;
vy[item] = y;
vc[item] = lerpColor(#333333, #eeeeee, v);
}
}
// Draw the voronoi
loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int closest = 0;
float distance = width * height;
for (int i = 0; i < vx.length; i++) {
if (dist(x, y, vx[i], vy[i]) < distance) {
closest = i;
distance = dist(x, y, vx[i], vy[i]);
}
}
pixels[x + y * width] = vc[closest % vc.length];
}
}
updatePixels();
}
//
//
//
float timeLoop(float totalframes, float offset) {
return (frameCount + offset) % totalframes / totalframes;
}
float timeLoop(float totalframes) {
return timeLoop(totalframes, 0);
}
float timeBounce(float totalframes, float offset) {
return Ease.tri(timeLoop(totalframes, offset));
}
float timeBounce(float totalframes) {
return timeBounce(totalframes, 0);
}
float valueNoise(float x, float y, float z) {
float v = sin(x * 281 + y * 95317 + z * 703) * 6731;
return v - floor(v);
}
float gradientAngular(float x, float y, float offset) {
float xd = (width / 2) - x;
float yd = (height / 2) - y;
return wrap((atan2(yd, xd) / TAU) + PI, offset);
}
static class Ease {
static public float hermite5(float t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
static public float tri(float t) {
return t < 0.5 ? t * 2 : 2 - (t * 2);
}
}
float wrap(float value, float offset) {
return (value + offset) % 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment