假設我有一個二維像素網格(4 x 4 像素)——并且我有一個我的草圖大小的影像,已被切割成 16 個部分。現在我將所有 16 個部分加載到一個陣列中。我想將這個陣列依次映射到 2D 網格上,以便我的整體影像再次正確組合在一起。即,左上影像 0.png 和右下影像 16.png。
我只是找不到允許我這樣做的公式。例如,我知道x y*width你可以通過所有像素——從左上角到右下角——所以我嘗試了。沒有*width它就不能正確地坐在一起 - 與x y*width- ArrayIndexOutOfBoundsException (當然)。
所以我想我需要一個二維陣列——但 images[x][y]我得到了一個 NullPointerException。我附上了我正在嘗試創建的影像:

到目前為止,這是我的代碼——沒有二維陣列……
float pixelamount = 4;
float pixelsize;
PImage[] images = new PImage [16];
void setup() {
size(1080, 1080);
pixelsize = width/pixelamount;
for (int i = 0; i < images.length; i ) {
images[i] = loadImage(i ".png");
}
imageMode(CENTER);
}
void draw() {
background(0);
pushMatrix();
translate(pixelsize/2, pixelsize/2);
for (int x = 0; x < pixelamount; x ) {
for (int y = 0; y < pixelamount; y ) {
pushMatrix();
translate(pixelsize*x, pixelsize*y);
image(images[x y], 0, 0, pixelsize, pixelsize);
popMatrix();
}
}
popMatrix();
}
正如我所說——在這條線上,image(images[x y], 0, 0, pixelsize, pixelsize);我只是沒有得到正確的數學。我需要一個二維陣列來解決這個問題嗎?還是完全不同的東西?
uj5u.com熱心網友回復:
這應該在沒有二維陣列的情況下解決。
如果欄位的尺寸已知為 4x4,那么回圈可能應該從 0 運行到 4,如下所示:
void draw() {
background(0);
pushMatrix();
translate(pixelsize/2, pixelsize/2);
for (int x = 0; x < 4; x ) {
for (int y = 0; y < 4; y ) {
pushMatrix();
translate(pixelsize * x, pixelsize * y);
image(images[4 * x y], 0, 0, pixelsize, pixelsize);
popMatrix();
}
}
popMatrix();
}
uj5u.com熱心網友回復:
亞歷克斯是正確的。Cyrill,您走在正確的軌道上,但似乎對查看資料的 3 種方式感到困惑:
- 影像陣列是一維陣列(索引 0 到 15)
- for 回圈是嵌套的,因此您需要將 2D 索引轉換為 1D。你是對的:
x y*width會給你正確的陣列索引,但在這種情況下width不是你的草圖的全寬(以像素為單位)而是網格的寬度(即 4x4 網格中的列數:4) - 你得到一個空指標指標,因為你試圖訪問一維陣列中的元素,就好像它是二維的一樣。
像這樣的東西應該作業:
float pixelamount = 4;
float pixelsize;
PImage[] images = new PImage [16];
void setup() {
size(1080, 1080);
pixelsize = width/pixelamount;
for (int i = 0; i < images.length; i ) {
images[i] = loadImage(i ".png");
}
//imageMode(CENTER);
}
void draw() {
background(0);
pushMatrix();
translate(pixelsize/2, pixelsize/2);
for (int x = 0; x < pixelamount; x ) {
for (int y = 0; y < pixelamount; y ) {
pushMatrix();
translate(pixelsize*x, pixelsize*y);
image(images[x y * pixelamount], 0, 0, pixelsize, pixelsize);
popMatrix();
}
}
popMatrix();
}
如果您想使用與您存盤資料的方式相匹配的單個 for 回圈(而不是嵌套的 for 回圈)進行回圈,您可以使用此公式從 1D 索引轉到 2D 索引:
x = index % gridColumns
y = index / gridColumns
(請記住這些是整數(所以在其他語言(如 Python/JS/等)中,你會注意除法運算))
這是一個基本示例來說明這一點:
size(1080, 1080);
textAlign(CENTER, CENTER);
textFont(createFont("Courier New Bold", 12));
int pixelAmount = 4;
int pixelSize = width/pixelAmount;
int gridColumns = 4;
// iterate once
for(int i = 0; i < 16; i ){
// calculate 2D grid indices
int xIndex = i % gridColumns;
int yIndex = i / gridColumns;
// convert from index to pixel size
int x = xIndex * pixelSize;
int y = yIndex * pixelSize;
// render debug data
String debugText = "1D index:" i
"\n2D indices:[" xIndex "][" yIndex "]"
"\nx, y pixels::" x "," y;
fill(255);
rect(x, y, pixelSize, pixelSize);
fill(0);
text(debugText, x pixelSize / 2, y pixelSize / 2);
}
這是與上面使用 2D 陣列和嵌套回圈的示例相同的示例:
size(1080, 1080);
textAlign(CENTER, CENTER);
textFont(createFont("Courier New Bold", 12));
int pixelAmount = 4;
int pixelSize = width/pixelAmount;
int[][] grid = new int[pixelAmount][pixelAmount];
// mimick image loading (storing 1D index)
int index = 0;
for(int y = 0; y < pixelAmount; y )
for(int x = 0; x < pixelAmount; x )
grid[x][y] = index ;
// mimick reading 2D array data
for(int y = 0; y < pixelAmount; y ){
for(int x = 0; x < pixelAmount; x ){
int xPixels = x * pixelSize;
int yPixels = y * pixelSize;
// manually copute index
// index = x y * pixelAmount;
// or retrieve stored index
index = grid[x][y];
String debugText = "1D index:" index ".png"
"\n2D indices:[" x "][" y "]"
"\nx, y pixels::" xPixels "," yPixels;
fill(255);
rect(xPixels, yPixels, pixelSize, pixelSize);
fill(0);
text(debugText, xPixels pixelSize / 2, yPixels pixelSize / 2);
}
}
我的回答更多是為了完整起見:顯示查看資料的 1D/2D 方式。
uj5u.com熱心網友回復:
基于最新的答案——這是我的代碼——完美運行!
float pixelamount = 4;
float pixelsize;
PImage[] images = new PImage [16];
void setup() {
size(1080, 1080);
pixelsize = width/pixelamount;
for (int i = 0; i < images.length; i ) {
images[i] = loadImage(i ".png");
}
imageMode(CENTER);
}
void draw() {
background(0);
pushMatrix();
translate(pixelsize/2, pixelsize/2);
for (int x = 0; x < pixelamount; x ) {
for (int y = 0; y < pixelamount; y ) {
pushMatrix();
translate(pixelsize * x, pixelsize * y);
image(images[x y * int(pixelamount)], 0, 0, pixelsize, pixelsize);
popMatrix();
}
}
popMatrix();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/437152.html
