Skip to content

Instantly share code, notes, and snippets.

@davidblitz
Created October 8, 2017 20:39
Show Gist options
  • Save davidblitz/7e74288789f859b6ce3ebc4d14f402d0 to your computer and use it in GitHub Desktop.
Save davidblitz/7e74288789f859b6ce3ebc4d14f402d0 to your computer and use it in GitHub Desktop.
Sorting animation for processing (Java)
#insert filename of any image
String fname = "../sample-images/earth.jpg";
#insert width and height of image
int imgWidth = 256;
int imgHeight = 256;
PImage img;
void settings() {
size(imgWidth, imgHeight);
}
void setup() {
colorMode(HSB, 255);
img = loadImage(fname);
img.loadPixels();
frameRate(100);
}
int cnt = 1;
int steps = 1;
void draw() {
//if (frameCount % 1000 == 0) {
background(img);
//}
if (frameCount < imgWidth) {
for (int y = 0; y < imgHeight; ++y) {
for (int pos = y*imgWidth + frameCount;
pos % imgWidth != 0 && saturation(img.pixels[pos - 1]) > saturation(img.pixels[pos]);
--pos) {
//swap pos - 1 and pos
int tmp = img.pixels[pos - 1];
img.pixels[pos - 1] = img.pixels[pos];
img.pixels[pos] = tmp;
}
}
if(frameCount % 8 == 1) {
String name2 = (new Integer(512 - frameCount )).toString();
saveFrame("output/###.jpg");
saveFrame("output/" + name2 + ".jpg");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment