# 获取整数中的二维数组位置

go评论62阅读模式

Get twodimensional array positions out of integer

# 问题

``````int MAPWIDTH = 64; // 像素
int MAPHEIGHT = 16; // 像素
PImage[][] forGroundMap;

int size = file.width * file.height;

for (int i = 0; i < size; i++) {
int x = i % MAPWIDTH;
int y = i / MAPWIDTH;

// forGroundMap[y][x] == 在这里写入相应的内容
}
``````

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

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

return count;
}
``````

for every pixel I check the color and create an image, I now want to add this image inside of an two dimensional array.

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

int size = file.width * file.height;

for (int i = 0; i &lt; size; i++) {

int y = divideBy(i, MAPWIDTH);

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

For reference: The images are always 64 in height and 32 in width.

To get the position I have tried to divide the current iteration by the width to know what row I am currently at.

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

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

return count;
``````

}

This however doesn't give me back the column inside my row And I am not sure if this will work at all and how to continue forward.

# 答案1

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

``````int x = 1 / 64;
int y = 1 % 64;
System.out.printf("你在位置 [%d,%d]%n", x, y);
x = 65 / 64;
y = 65 % 64;
System.out.printf("你在位置 [%d,%d]%n", x, y);
``````

What you appears to be wanting is simple division math.

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

edit

To get the x value as well use modulo

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

• 本文由 发表于 2020年10月2日 08:35:20
• 转载请务必保留本文链接：https://go.coder-hub.com/64164843.html
• java
• math
• multidimensional-array
• pixel
• processing

go 72

go 84

go 65

go 58