Given a screen which I want to fill up with n
buttons of equal size, how would I calculate it such that the screen is optimally filled with the buttons? It is for a mobile app where the user just has to make a quick selection.
In pseudo code what I initially wrote:
ta = w*h; //total area
as = ta/n; //area of each square
ss = sqrt(as); //side square
sh = ceil(w/ss); //squares horizontally
sv = ceil(h/ss); //squares vertically
sw = w/sh; //square width
sh = h/sv; //square height
However there are a couple of problems with this, first of all due to the ceil
function I may end up with an entirely empty row at the bottom of the screen, however a round
will sometimes make the squares not fit. Additionally in cases where the bottom row contains only a few buttons the width could have been increased such that less space would be left over on the bottom row (though some space will of course be left).
All similar questions discuss solutions for different sizes of rectangles which I could work out using a recursive or similar approach, but this should be far simpler I believe.