Skip to content

Instantly share code, notes, and snippets.

@eloone
Last active December 17, 2015 11:19
Show Gist options
  • Save eloone/5601671 to your computer and use it in GitHub Desktop.
Save eloone/5601671 to your computer and use it in GitHub Desktop.
Recursive function to calculate a grid
function calculate(cols, nb){
_this_.cols = cols;
_this_.rows = Math.ceil(nb/cols);
_this_.rest = nb % cols;
_this_.boxDim = _this_.width/cols - 2*_this_.border;
if(_this_.boxDim <= 1){
debug.exec('warn', 'Dimensions of the boxes are too small to be seen');
}
//Recurses if the height of the resulting grid is greater than the settings height
if((_this_.boxDim+2*_this_.border)*_this_.rows > _this_.height){
calculate(cols + 1, nb);
}
}
//We start by computing the rendered grid recursively, we want at minimum 2 columns, since it's a grid
//We call the recursive function to calculate the grid at the beginning of the script
//Once it's done, the grid is calculated
calculate(2, _this_.nb_items);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment