簡體   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