Optimized grid for rectangular items?

Note: I couldn't quite understand Frédéric's answer, so I worked the problem out myself and came up with what appears to be the same solution. I figured I might as well explain what I did in case it is helpful First I normalized the aspect ratio of the view to that of the items. (I'm assuming you don't want to rotate the items.) a = (view_width/view_height) / (item_width/item_height) Now packing a rectangle of width/height ratio a with squares is equivalent to packing the view with items.

The ideal case would be for our grid (of squares now) to fill the rectangle completely, which would give us a = c/r where r and c are the numbers of rows and columns: N = r*c Multiplying/dividing these two equations gives us N*a = c^2 N/a = r^2 c = sqrt(N*a) r = sqrt(N/a) If the grid is perfect r and c will be integers, but if not, you have to try the three options Frédéric mentioned and keep the one where r*c is smallest but still more than N : floor(r), ceil(c) ceil(r), floor(c) ceil(r), ceil(c).

Note: I couldn't quite understand Frédéric's answer, so I worked the problem out myself and came up with what appears to be the same solution. I figured I might as well explain what I did in case it is helpful. First I normalized the aspect ratio of the view to that of the items.(I'm assuming you don't want to rotate the items.) a = (view_width/view_height) / (item_width/item_height) Now packing a rectangle of width/height ratio a with squares is equivalent to packing the view with items.

The ideal case would be for our grid (of squares now) to fill the rectangle completely, which would give us a = c/r where r and c are the numbers of rows and columns: N = r*c Multiplying/dividing these two equations gives us N*a = c^2 N/a = r^2 c = sqrt(N*a) r = sqrt(N/a) If the grid is perfect, r and c will be integers, but if not, you have to try the three options Frédéric mentioned and keep the one where r*c is smallest but still more than N: floor(r), ceil(c) ceil(r), floor(c) ceil(r), ceil(c).

Thanks for your detailed comments. I still have some cases where the brute force search "looks better" than these results of yours / Frederics suggestion. Maybe I'm still doign somethign wrogn with rounding, though... I 'm playing around with this only part - time.

– peterchen Mar 22 '10 at 9:52.

Your solution can be easily improved to handle the generic case : If we (temporary) forget the need to have an integer number of rows and columns, we have rows * columns = N x = aitem * y aview = rows * x = rows * aitem * y 1 = columns * y = (N/rows) * ( aview / aitem*rows) = N * aview /(aitem * rows²) hence rows=sqrt(N *aview/aitem) and columns = N/rows = sqrt(N * aitem / aview) Then ceil(rows) and ceil(columns) is a solution while floor(rows) and floor(columns) are too small to be a solution together (if rows and columns are not integers). This leaves 3 possible solutions : floor(rows) ceil(columns) ceil(rows) floor(columns) ceil(rows) ceil (columns) edited to correct the equations. The first result was wrong (see comments).

1 I think aitem*aview in your final equations is a typo. What about the case where N=2, aview=6, aitem=3. You would end up with too many columns.

– mckeed Mar 19 '10 at 22:31 It's not a typo. It's a mistake in the first equations, where I've taken opposite definitions of aview and aitem. It's embarrassing as my initial formula was not homogeneous (yes, I'm a physicist) and the error appears on a simple test case.

Thanks for pointing it. Now, we have 2 rows and 1 column for your testcase, as expected – Frédéric Grosshans Mar 20 '10 at 19:13.

Good question. If your view has dimensions A x B (fixed) and your items have dimensions a x be (variable, to be maximized), then you need: trunc(A / a) * trunc(B / b) >= N I have no idea how to solve this though - the trunc is the tricky part, as it's non-linear.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions