开发者

Extract a grid from a one-dimentional array

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜