Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @func ds_grid_get_bounds
- /// @arg {grid} grid
- /// @desc Returns the smallest rectangular bounds of non-zero data in a grid
- /// @desc e.g. a bounding box trimmed of all empty space
- /// @desc Format is LEFT/TOP/RIGHT/BOTTOM (X1/Y1/X2/Y2)
- /// Could be optimized further to use half-intervals
- var _grid = argument[0];
- var _gridW = ds_grid_width(_grid);
- var _gridH = ds_grid_height(_grid);
- var _left = undefined;
- var _top = undefined;
- var _right = undefined;
- var _bottom = undefined;
- // Check rows
- // Determine top row
- var _maxRow = undefined;
- for (var _yy = 0; _yy < _gridH; _yy++){
- _maxRow = ds_grid_get_max(_grid,0,_yy,_gridW - 1,_yy);
- if (_maxRow > 0){
- _top = _yy;
- break;
- }
- }
- // Leave if the grid was entirely empty
- if (is_undefined(_top)){
- return [undefined, undefined, undefined, undefined];
- }
- // Determine bottom row. Bottom can be the same as top but will never be smaller.
- for (var _yy = _gridH - 1; _yy >= _top; _yy--){
- _maxRow = ds_grid_get_max(_grid,0,_yy,_gridW - 1,_yy);
- if (_maxRow > 0){
- _bottom = _yy;
- break;
- }
- }
- // Check columns
- var _maxCol = undefined;
- // Determine left col. Skip empty rows
- for (var _xx = 0; _xx < _gridW; _xx++){
- _maxCol = ds_grid_get_max(_grid,_xx,_top,_xx,_bottom);
- if (_maxCol > 0){
- _left = _xx;
- break;
- }
- }
- // Determine right col. Right can be the same as left but will never be smaller.
- for (var _xx = _gridW - 1; _xx >= _left; _xx--){
- _maxCol = ds_grid_get_max(_grid,_xx,_top,_xx,_bottom);
- if (_maxCol > 0){
- _right = _xx;
- break;
- }
- }
- return [_left,_top,_right,_bottom];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement