Pack items neatly in a rectangular box

I cannot 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

if the aspect ratio of the container 1.5

and i have elements the 6

columns should be3

x x x
x x x

      

if the aspect ratio of the container 2.0

and i have elements the 32

columns should be8

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 a relation 2.0

and i have 30

elements, the columns should probably be8

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

      

etc. I feel like it should be simple, but I can't think of an easy way to do it. the input will be items

, width

and height

, and the output will be columns

(the lines will be filled in by themselves)

thank

+3


source to share


1 answer


The formula I got is sqrt (items / aspect_ratio) * aspect_ratio. You can round this result to get the desired result in scenario 3.

As I understand:



'aspect ratio just seems to be scale of the number of rows; as such 
columns = rows * aspect ratio
also 
items = rows * columns 
now subbing in / algebra
rows^2 * aspect ratio = items
rows = sqrt(items/aspect ration)
columns = sqrt(items/ aspect ratio) * aspect ratio'

      

+1


source







All Articles