繁体   English   中英

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

[英]How to get the 2d coordinates(x,y) from an ordered 1d list - 0,1,2,3,4,5,6… with given width & height?

图像的索引存储在一维列表中,其中包含0,1,2,3,4,5 ....(w*h)而不是(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());

下面的参考图片可以使上面的问题更加清晰

您的尝试非常接近!

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

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

int x = quotient;
int y = remainder;

暂无
暂无

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

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