neatly pack items into a rectangular box

Viewed 54

I can't think of a more elegant way to explain this. So I have a rectangular container and a variable number of items and I want to fill the container evenly like so

if the aspect ratio of the container is 1.5 and I have 6 items, columns should probably be 3

x x x
x x x

if the aspect ratio of the container is 2.0 and I have 32 items, columns should probably be 8

x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x

if by chance the ratio is 2.0 and I have 30 items, columns should probably still be 8

x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x

and so on. I feel like this should be simple but I can't think of a simple way to do it. the input would be items, width, and height, and the output would be columns (rows would fill out themselves)

thanks

1 Answers
Related