Skip to content

Commit c33c8a6

Browse files
committed
fixing the sorting sektch
1 parent ff229d9 commit c33c8a6

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

sketches/pixel_sort/pixel_sort.pde

+27-25
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,63 @@ import java.util.Random;
22

33
PImage img;
44
int y;
5-
int p;
5+
int randomFactor;
66

77
void setup() {
88
size(640, 360);
99
frameRate(120);
10+
1011
img = loadImage("moonwalk.jpg");
12+
randomFactor = 10; // vary this to get different results
1113
y = 0;
12-
p = 1;
1314
}
1415

1516
void draw() {
1617
background(255);
1718
PImage buf = createImage(img.width, img.height, RGB);
18-
buf = img.copy();
19-
19+
// randomize sampling window
20+
int p = int(max(random(1, randomFactor), 1));
21+
2022
for (int x=0; x < img.width; x += p) {
2123
for (int y=0; y < img.height; y++) {
2224
color[] colors = new color[p];
2325
for (int i=0; i < p; i++) {
2426
int index = x+i;
25-
colors[i] = img.get(index, y);
26-
if (index >= img.width - 1) {
27-
break;
27+
// wrap over if we reached the boundaries
28+
if (index >= img.width-1) {
29+
colors[i] = img.get(i, y);
30+
} else {
31+
colors[i] = img.get(index, y);
2832
}
2933
}
30-
34+
3135
shuffleColors(colors);
32-
36+
3337
for (int i=0; i < p; i++) {
3438
int index = x+i;
3539
color c = colors[i];
36-
buf.set(index, y, c);
37-
if (index >= img.width - 1) {
38-
break;
40+
if (index >= img.width-1) {
41+
buf.set(i, y, c);
42+
} else {
43+
buf.set(index, y, c);
3944
}
4045
}
4146
}
4247
}
43-
48+
4449
img = buf;
4550
image(img, 0, 0);
46-
p++;
4751
}
4852

4953
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-
}
54+
int index;
55+
Random random = new Random();
56+
for (int i = array.length - 1; i > 0; i--) {
57+
index = random.nextInt(i + 1);
58+
if (index != i) {
59+
array[index] ^= array[i];
60+
array[i] ^= array[index];
61+
array[index] ^= array[i];
6162
}
63+
}
6264
}

0 commit comments

Comments
 (0)