Hi, I have a small program here with which you can load a picture.
(I have one from the Internet: https: //i.ytimg.com/vi/uFf1j4c70s8/maxresdefault.jpg)
You can set how many columns and how many rows it should have. It is only important that the image can also be divided with this number, otherwise a black border will result because the PImage.get () function only accepts Integers.
Here is the code:
PImage image;
PImage[][] cuts;
int cols = 5;
int rows = 5;
int w, h;
int selI = 0, selJ = 0;
boolean selected = false;
void setup() {
size(800, 800);
image = loadImage("https://i.ytimg.com/vi/uFf1j4c70s8/maxresdefault.jpg");
surface.setSize(image.width, image.height);
println(float(width) / cols);
w = round(float(width) / cols);
h = round(float(height) / rows);
setCuts();
shuffel(cuts);
}
void draw() {
background(0);
displayCuts();
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
PVector l = new PVector(i*w, j*h);
if (mouseX < l.x+w && mouseX > l.x && mouseY < l.y+h && mouseY > l.y) {
fill(255, 0, 0, 100);
rect(i*w, j*h, w, h);
}
}
}
if (selected) {
fill(0, 255, 0, 122);
rect(selI*w, selJ*h, w, h);
}
}
void mousePressed() {
if (selected == false) {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
PVector l = new PVector(i*w, j*h);
if (mouseX < l.x+w && mouseX > l.x && mouseY < l.y+h && mouseY > l.y) {
selI = i;
selJ = j;
selected = true;
}
}
}
} else {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
PVector l = new PVector(i*w, j*h);
if (mouseX < l.x+w && mouseX > l.x && mouseY < l.y+h && mouseY > l.y) {
swap(cuts, i, j, selI, selJ);
selected = false;
}
}
}
}
}
void displayCuts() {
noFill();
stroke(255, 0, 0);
strokeWeight(2);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
image(cuts[i][j], i*w, j*h, w, h);
rect(i*w, j*h, w, h);
}
}
}
void shuffel(PImage[][] imgArr) {
for (int i = 0; i < imgArr.length; i++) {
for (int j = 0; j < imgArr[0].length; j++) {
swap(imgArr, i, j, floor(random(imgArr.length)), floor(random(imgArr[0].length)));
}
}
}
void swap(PImage[][] arr, int aI, int aJ, int bI, int bJ) {
PImage curr = arr[aI][aJ];
arr[aI][aJ] = arr[bI][bJ];
arr[bI][bJ] = curr;
}
void setCuts() {
cuts = new PImage[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
cuts[i][j] = image.get(int(i*w), int(j*h), (w), (h));
}
}
}
I hope I could help you.
Florian