SHARE
TWEET

One dimensional array as grid

ulfben Jan 30th, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //one dimensional array as grid
  2. var grid = [];
  3. var numCols = 5;
  4.  
  5. function get(row, col){
  6.     return grid[row*numCols + col];
  7. }
  8.  
  9. function set(row, col, value){
  10.     return grid[row*numCols + col] = value;
  11. }
  12.  
  13. //one dimensional array as grid with stride
  14. //example;  rgba pixel data
  15. function getPixel(x, y){
  16.     var index = (y*imageData.width + x) * 4; //4 integers for RGBA
  17.     var red = imageData[index];
  18.     var green = imageData[index+1];
  19.     var blue = imageData[index+2];
  20.     var alpha = imageData[index+3];
  21.     var alphaPrcnt = alpha/255; //0-1  
  22.    
  23.     var rgba = (red << 24) + (green << 16) + (blue << 8) + (alpha);
  24.     return rgba;
  25. }
RAW Paste Data
Top