简体   繁体   中英

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

the indexes of an image are stored in a 1d list with 0,1,2,3,4,5 ....(w*h) ,instead of (0,0), (0,1),(0,2)...(w,h) (given height and width of the image), How to get the indexes(x,y) from an index for example : 57?

This code only worked with squared dimensions

//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());

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

Your attempt is very close!

Instead of dividing by the height of the matrix, divide by the width , because the indices of the 1D array increases as you go from the left to the right of the image, then top to bottom, as opposed to top to bottom, then left to right.

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

int x = quotient;
int y = remainder;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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