Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active October 26, 2023 11:01
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/d7f2c6c938e1acd2320062a3f842d3f7 to your computer and use it in GitHub Desktop.
Save KrabCode/d7f2c6c938e1acd2320062a3f842d3f7 to your computer and use it in GitHub Desktop.
import java.util.UUID;
ArrayList<P> ps = new ArrayList<P>();
int pointCount = 1000;
PImage source;
private float t;
private int framesToCapture = 300;
private int captureStart = -1;
String id = "";
public void settings() {
size(600, 600);
}
public void setup() {
reset();
voronoiFilter();
}
void reset() {
source = loadImage("https://picsum.photos/600/600.jpg");
ps.clear();
background(0);
while (ps.size() < pointCount) {
ps.add(new P());
}
voronoiFilter();
}
public void draw() {
t = map(frameCount, 0, framesToCapture, 0, TWO_PI);
for (P p : ps) {
p.myPixels.clear();
p.update();
}
voronoiFilter();
if (captureStart > 0 && frameCount > captureStart && frameCount <= captureStart + framesToCapture) {
println((frameCount - captureStart) + " / " + framesToCapture);
saveFrame("capture/"+id+"/####.jpg");
}
}
private void voronoiFilter() {
/*
background(0);
int i = 0;
for (P p : ps) {
if(i++ == 0){
stroke(255);
}else{
stroke(50);
}
strokeWeight(3);
point(p.pos.x, p.pos.y);
}
*/
image(source, 0, 0, width, height);
loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
P nearest = findNearestPoint(x, y);
nearest.myPixels.add(new PVector(x, y));
}
}
for (P p : ps) {
int count = p.myPixels.size();
int r = 0;
int g = 0;
int b = 0;
for (PVector px : p.myPixels) {
int c = pixels[floor(px.x) + floor(px.y) * width];
r += c >> 16 & 0xFF;
g += c >> 8 & 0xFF;
b += c & 0xFF;
}
int averageColor = 0;
if (count >0) {
averageColor = color(r / count, g / count, b / count);
}
for (PVector px : p.myPixels) {
pixels[floor(px.x) + floor(px.y) * width] = color(averageColor);
}
}
updatePixels();
filter(BLUR, .5f);
}
public void keyPressed() {
if (key == 'r') {
reset();
}
if (key == 'k') {
// saveFrame("capture/####.jpg");
id = UUID.randomUUID().toString();
captureStart = frameCount+1;
}
}
P findNearestPoint(int x, int y) {
P result = null;
float smallestDistanceFound = width;
for (P p : ps) {
float d = dist(p.pos.x, p.pos.y, x, y);
if (d < smallestDistanceFound) {
smallestDistanceFound = d;
result = p;
}
}
return result;
}
class P {
PVector origPos = new PVector(random(width), random(height));
PVector pos = new PVector(origPos.x, origPos.y);
ArrayList<PVector> myPixels = new ArrayList<PVector>();
public void update() {
//simplex noise implementation from here: https://github.com/SRombauts/SimplexNoise/blob/master/references/SimplexNoise.java
pos.x = (float) (origPos.x + 5 * (1-2*SimplexNoise.noise(origPos.x, cos(t), sin(t))));
pos.y = (float) (origPos.y + 5 * (1-2*SimplexNoise.noise(origPos.y, cos(t), sin(t))));
}
}
@KrabCode
Copy link
Author

0218
0602
0865
5130
5295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment