Skip to content

Commit ff229d9

Browse files
committed
added a rudimentary pixel sort example
1 parent de708ee commit ff229d9

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

sketches/pixel_sort/data/moonwalk.jpg

117 KB
Loading

sketches/pixel_sort/pixel_sort.pde

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.Random;
2+
3+
PImage img;
4+
int y;
5+
int p;
6+
7+
void setup() {
8+
size(640, 360);
9+
frameRate(120);
10+
img = loadImage("moonwalk.jpg");
11+
y = 0;
12+
p = 1;
13+
}
14+
15+
void draw() {
16+
background(255);
17+
PImage buf = createImage(img.width, img.height, RGB);
18+
buf = img.copy();
19+
20+
for (int x=0; x < img.width; x += p) {
21+
for (int y=0; y < img.height; y++) {
22+
color[] colors = new color[p];
23+
for (int i=0; i < p; i++) {
24+
int index = x+i;
25+
colors[i] = img.get(index, y);
26+
if (index >= img.width - 1) {
27+
break;
28+
}
29+
}
30+
31+
shuffleColors(colors);
32+
33+
for (int i=0; i < p; i++) {
34+
int index = x+i;
35+
color c = colors[i];
36+
buf.set(index, y, c);
37+
if (index >= img.width - 1) {
38+
break;
39+
}
40+
}
41+
}
42+
}
43+
44+
img = buf;
45+
image(img, 0, 0);
46+
p++;
47+
}
48+
49+
void shuffleColors(color[] array) {
50+
int index;
51+
Random random = new Random();
52+
for (int i = array.length - 1; i > 0; i--)
53+
{
54+
index = random.nextInt(i + 1);
55+
if (index != i)
56+
{
57+
array[index] ^= array[i];
58+
array[i] ^= array[index];
59+
array[index] ^= array[i];
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)