kemonologic

ds_grid_get_bounds

Oct 15th, 2020
343
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @func ds_grid_get_bounds
  2. /// @arg {grid} grid
  3. /// @desc Returns the smallest rectangular bounds of non-zero data in a grid
  4. /// @desc e.g. a bounding box trimmed of all empty space
  5. /// @desc Format is LEFT/TOP/RIGHT/BOTTOM (X1/Y1/X2/Y2)
  6. /// Could be optimized further to use half-intervals
  7.  
  8. var _grid = argument[0];
  9. var _gridW = ds_grid_width(_grid);
  10. var _gridH = ds_grid_height(_grid);
  11.  
  12. var _left = undefined;
  13. var _top = undefined;
  14. var _right = undefined;
  15. var _bottom = undefined;
  16.  
  17. // Check rows
  18. // Determine top row
  19. var _maxRow = undefined;
  20. for (var _yy = 0; _yy < _gridH; _yy++){
  21.     _maxRow = ds_grid_get_max(_grid,0,_yy,_gridW - 1,_yy);
  22.     if (_maxRow > 0){
  23.         _top = _yy;
  24.         break;
  25.     }
  26. }
  27. // Leave if the grid was entirely empty
  28. if (is_undefined(_top)){
  29.     return [undefined, undefined, undefined, undefined];
  30. }
  31.  
  32. // Determine bottom row. Bottom can be the same as top but will never be smaller.
  33. for (var _yy = _gridH - 1; _yy >= _top; _yy--){
  34.     _maxRow = ds_grid_get_max(_grid,0,_yy,_gridW - 1,_yy);
  35.     if (_maxRow > 0){
  36.         _bottom = _yy;
  37.         break;
  38.     }
  39. }
  40.  
  41. // Check columns
  42. var _maxCol = undefined;
  43. // Determine left col. Skip empty rows
  44. for (var _xx = 0; _xx < _gridW; _xx++){
  45.     _maxCol = ds_grid_get_max(_grid,_xx,_top,_xx,_bottom);
  46.     if (_maxCol > 0){
  47.         _left = _xx;
  48.         break;
  49.     }
  50. }
  51.  
  52. // Determine right col. Right can be the same as left but will never be smaller.
  53. for (var _xx = _gridW - 1; _xx >= _left; _xx--){
  54.     _maxCol = ds_grid_get_max(_grid,_xx,_top,_xx,_bottom);
  55.     if (_maxCol > 0){
  56.         _right = _xx;
  57.         break;
  58.     }
  59. }
  60.  
  61. return [_left,_top,_right,_bottom];
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×