public
Created

Small processing script that creates an animation of swapping adjacent pixels.

  • Download Gist
pixelSwap.pde
Processing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
import java.util.Random;
 
color red, blue, green;
color[][] pix;
static int size = 5;
Random r;
 
void setup() {
frameRate(1000);
size(500, 500);
smooth();
noStroke();
r = new Random();
red = color(255, 0, 0);
green = color(0, 255, 0);
blue = color(0, 0, 255);
pix = new color[width / size][];
for(int i = 0; i < pix.length; i++) {
pix[i] = new color[height / size];
for(int j = 0; j < pix[i].length; j++) {
if(i * size < width / 3) {
pix[i][j] = red;
} else if(i * size < width / 3 * 2) {
pix[i][j] = green;
} else {
pix[i][j] = blue;
}
}
}
}
 
void draw() {
background(255);
for(int x = 0; x < pix.length; x++) {
for(int y = 0; y < pix[x].length; y++) {
fill(pix[x][y]);
rect(x * size, y * size, size, size);
}
}
while(true) {
int x = int(r.nextFloat() * pix.length);
int y = int(r.nextFloat() * pix[x].length);
int dx = x + int(r.nextFloat() * 4 - 2);
int dy = y + int(r.nextFloat() * 4 - 2);
color temp = pix[x][y];
try {
if((x == dx && y == dy) || pix[x][y] == pix[dx][dy]) {
continue;
} else {
pix[x][y] = pix[dx][dy];
pix[dx][dy] = temp;
break;
}
}
catch(ArrayIndexOutOfBoundsException e) {
continue;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.