Hi,
I’m attempting to reorder a 2D array to follow a zigzag path. This image illustrates the path I wish to follow:
https://en.wikipedia.org/wiki/JPEG#/media/File:JPEG_ZigZag.svg
I’m having a bit of a problem moving backwards and forwards across the array.
The code below is what I have at the moment, generating a 2D array of colours in an image:
import java.util.Arrays;
int sizeX = 1200;
int sizeY = 800;
int detail = 30;
String[][] colourList = new String[ceil(sizeX/detail)][ceil(sizeY/detail)];
void setup() {
size(1200, 800);
PImage img = loadImage("image.jpg");
for (int i=0; i<ceil(sizeX/detail); i++) {
for (int j=0; j<ceil(sizeY/detail); j++) {
PImage newImg = img.get(i*detail, j*detail, detail, detail);
int colourExtracted = extractColorFromImage(newImg);
String thisCol = hex(colourExtracted, 6);
colourList[i][j] = thisCol;
fill(colourExtracted);
noStroke();
rect(i*detail, j*detail, detail, detail);
}
}
// print 2d array
println(Arrays.deepToString(colourList));
println();
println(Arrays.deepToString(colourList)
.replace("[[", "")
.replace("], [", "\n")
.replace("]]", "")
.replace(" ", " "));
}
color extractColorFromImage(final PImage img) {
img.loadPixels();
color r = 0, g = 0, b = 0;
for (final color c : img.pixels) {
r += c >> 020 & 0xFF;
g += c >> 010 & 0xFF;
b += c & 0xFF;
}
r /= img.pixels.length;
g /= img.pixels.length;
b /= img.pixels.length;
return color(r, g, b);
}
If there’s any pointers that anyone could share, I’d be most grateful.
Thanks in advance.