Created
August 10, 2014 18:57
-
-
Save binarymax/301ba025bdaa4e838186 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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