Evenly distribute items on the screen
- by abolotnov
I am trying to solve this little puzzle (the algorithm): I have N image icons and I want to distribute them evenly on users screen. Say, I put them in a table. If there is one image, there will be one cell in a table. If two - one row with two columns, if three - one row and three columns, if four - two rows, two columns... and so on until row space is gone and since then the table should only grow in columns without adding extra rows.
I'm trying to figure an algorithm for this and perhaps this is something that has a solution already somewhere?
My attempt is so far something like this:
obtain_max_rows()
obtain_visible_columns()
if (number_of_pictures > max_rows*max_columns)
{
columns = roundup(number_of_pictures/max_rows)
for(max_rows){generate row;for columns{generate column}}
}
else
{
**here comes to trouble...**
}
This logic is bit silly though - it somehow needs to think cases where there are 12 pictures on first screen and 2 on the other trying to balance it say 8/6 or somehow like that.