Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two dimension array to single dimension and back again

I was trying to find information about transforming an index that has been converted from a 2 dimensional index , to a single one. Then turn the single index back to a 2 dimensional one. I don't even know what this method is called.

formula to single index

  int index = x + y * width;
   MyArray[index] ;

So my question is how do I turn it back into two dimensions?

int x = index ??? width;
int y = index ??? width;

Can't wrap my head around it for some reason.

Thanks

like image 894
TimD Avatar asked Apr 01 '11 01:04

TimD


1 Answers

int x = index % width;
int y = index / width;
like image 87
BrokenGlass Avatar answered Nov 15 '22 07:11

BrokenGlass