繁体   English   中英

从整数中获取二维数组位置

[英]Get twodimensional array positions out of integer

我正在从图像中加载像素。 对于我检查颜色并创建图像的每个像素,我现在想将此图像添加到二维数组中。

int MAPWIDTH = 64; // in pixels
int MAPHEIGHT = 16; // in pixel
PImage[][] forGroundMap;

PImage file = loadImage(path);


file.loadPixels();
   
   int size = file.width * file.height;
   
   for (int i = 0; i < size; i++) {

    int y = divideBy(i, MAPWIDTH);

    //forGroundMap[y][x] == something what is x and y here
   }

供参考:图像始终为 64 高和 32 宽。

为了获得位置,我尝试将当前迭代除以宽度以了解我当前所在的行。

int divideBy(int number, int timesIn) {
int count = 0;

while (number >= timesIn) {
    number = number - timesIn;
    count++;
    println(number);
}

return count;

}

然而,这并没有让我回到我行内的列而且我不确定这是否会起作用以及如何继续前进。

您似乎想要的是简单的除法数学。

int y = 1 / 64;
System.out.println (y);
y = 65 / 64;
System.out.println (y);

编辑

要获得 x 值以及使用模

int x = 1 / 64;
int y = 1 % 64;
System.out.printf ("you are at [%d,%d]%n", x,y);
x = 65 / 64;
y = 65 % 64;
System.out.printf ("you are at [%d,%d]%n", x,y);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM