如何从给定的宽度和高度的有序一维列表-0,1,2,3,4,5,6 ....中获取二维坐标(x,y)?

时间:2019-05-04 07:32:42

标签: java python arrays

图像索引用0,1,2,3,4,5 ....(w*h)存储在1d列表中,而不是(0,0), (0,1),(0,2)...(w,h)(根据图像的高度和宽度)存储,如何从图像索引中获取索引(x,y)索引例如:57?

此代码仅适用于平方尺寸

//width & height given
//p is the index, for eg = 57, from the ordered list - 0,1,2...57...w*h

int remainder = p %height;
int quotient = p/height;

int x = quotient;
int y = remainder;

bufferedImage.setRGB(y,x,myWhite.getRGB());

reference image below for more clarity of the question above

1 个答案:

答案 0 :(得分:1)

您的尝试非常接近!

而不是除以矩阵的高度,而是除以 width ,因为随着您从图像的左到右,然后从上到下,一维数组的索引增加,而不是从上到下,然后从左到右。

int remainder = p %width;
int quotient = p/width;

int x = quotient;
int y = remainder;