V

Avatar



1d to 2d

At some point you might end up needing to convert a 1d index value to access a 2d array. Here’s how you can do it.


int x = index % xres;
int y = index / xres;

As noted by Diogo Teixeira, one way to get rid of the mod/div operations for POT buffers would be:


int precalc_a = ( xresPOT - 1 );
int precalc_b = (int)log2( xresPOT );
int x = index & precalc_a;
int y = index >> precalc_b;

.





,