Skip to content

Instantly share code, notes, and snippets.

@binarymax
Created August 10, 2014 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binarymax/301ba025bdaa4e838186 to your computer and use it in GitHub Desktop.
Save binarymax/301ba025bdaa4e838186 to your computer and use it in GitHub Desktop.
/*
2D ARRAY
0 1 2 3 4 (x)
0 [ ][ ][ ][ ][ ]
1 [ ][ ][#][ ][ ]
2 [ ][ ][ ][ ][ ]
3 [ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ]
(y)
1D BUFFER
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 (offset)
[ ][ ][ ][ ][ ][ ][ ][#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
ARRAY(2,1) == OFFSET(7) ... x+y*width
OFFSET(7) == ARRAY(2,1) ... y:floor(off/width) x:off%width
*/
var width = 100;
var height = 100;
var offset = function(x,y) {
return x+y*width;
};
var points = function(off) {
var y = Math.floor(off/width);
var x = off%width;
return {x:x,y:y};
};
function test(){
for(var x=0;x<width;x++) {
for(var y=0;y<height;y++) {
var off = offset(x,y);
var crt = points(off);
if (crt.x!==x || crt.y!==y) {
console.log("FAIL!",x,y,crt.x,crt.y);
return false;
}
}
}
return true;
}
console.log(test());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment