Evenly distribute items on the screen

Posted by abolotnov on Stack Overflow See other posts from Stack Overflow or by abolotnov
Published on 2012-06-02T22:35:44Z Indexed on 2012/06/02 22:41 UTC
Read the original article Hit count: 264

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about layout