矩形项目的优化网格 [英] optimized grid for rectangular items
问题描述
我有N个长宽比为Aitem(X:Y)的矩形项目.
我有一个长宽比为Aview的矩形显示区域
I have N rectangular items with an aspect ratio Aitem (X:Y).
I have a rectangular display area with an aspect ratio Aview
项目应以表格形式排列(即r行,c列).
The items should be arranged in a table-like layout (i.e. r rows, c columns).
什么是理想的网格行x列,以便单个项目最大? (当然,行*列> = N-即可能有未使用的"网格位置).
what is the ideal grid rows x columns, so that individual items are largest? (rows * colums >= N, of course - i.e. there may be "unused" grid places).
一种简单的算法可以遍历行= 1..N,计算所需的列数,并使行/列对具有最大的项.
A simple algorithm could iterate over rows = 1..N, calculate the required number of columns, and keep the row/column pair with the largest items.
我想知道是否有一个非迭代算法(例如,对于Aitem = Aview = 1,行/列可以由sqrt(N)近似).
I wonder if there's a non-iterative algorithm, though (e.g. for Aitem = Aview = 1, rows / cols can be approximated by sqrt(N)).
推荐答案
注意:我不太理解Frédéric的回答,所以我自己解决了这个问题,并提出了似乎相同的解决方案.我想我也最好解释一下我做了些什么,以防它有所帮助.
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)
现在将宽度/高度比a
的矩形与正方形打包在一起,等效于将视图与项目打包在一起.理想的情况是让我们的网格(现在为正方形)完全填充矩形,这将给我们
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
其中r
和c
是行数和列数:
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)
如果网格是完美的,则r
和c
将是整数,但如果不是,则必须尝试Frédéric提到的三个选项,并保持r*c
最小但仍大于N
的选项. :
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)
floor(r), ceil(c)
ceil(r), floor(c)
ceil(r), ceil(c)
这篇关于矩形项目的优化网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!