开发者

Extract a grid from a one-dimentional array

开发者 https://www.devze.com 2023-01-27 02:14 出处:网络
I have an array with 12 values and I need to display these in a grid like this (always 4 columns, 3 rows):

I have an array with 12 values and I need to display these in a grid like this (always 4 columns, 3 rows):

  1  |  2  |  3  |  4
 ----------------------
  5  |  6  |  7  |  8
 ----------------------
  9  |  10 |  11 |  12

I am looping through the grid and I have two coordinates: column and row.

How do I know which index belongs to which row and column? I tried several things, but they are not working:

开发者_JAVA百科objectAtIndex: (row + 1) * (column + 1) - 1
objectAtIndex: row + column

etc...


Row and column indexes start with 0.


forward conversion: objectAtPosition(x,y) = array[columns*y + x]

provided x<columns && y<rows

backward conversion: positionAtIndex(i) = (row=(i div columns), col=(i mod columns))

note that div and mod correspond to integer operators / and % in C languages.

0

精彩评论

暂无评论...
验证码 换一张
取 消