A simple package is carried out on your basis, so that four arrangements can be quickly formed.
Thank you for the algorithm provided!
int total;
float itemSize;
void setup() {
size(800, 800);
pixelDensity(2);
smooth();
rectMode(CENTER);
total = 4;
itemSize = width/total;
}
void draw() {
clear();
drawGrid("lt");
}
// lt -> left top
// rt -> right top
// ld -> left down
// rd -> right down
void drawGrid(String str) {
int r1 = 0;
int c1 = 0;
for (int r = 0; r < total; r++) {
if (str == "lt" || str == "rt") {
r1 = r;
} else if (str == "ld" || str == "rd") {
r1 = total - r - 1;
}
for (int c = 0; c < total; c++) {
if (str == "lt" || str == "ld") {
c1 = c;
} else if (str == "rt" || str == "rd") {
c1 = total-c-1;
}
int v = 1 + c1 + r1 * total;
float x = (c + 0.5) * itemSize;
float y = (r + 0.5) * itemSize;
fill(-1);
square(x, y, itemSize * 0.8);
fill(0);
textSize(40);
text(v, x, y);
}
}
}
algorithm by: @quark For loop matrix index - #3 by quark
