如何优化矩形布局
我有动态数量的等比例和大小的矩形对象,我想在屏幕上以最佳方式显示它们。我可以调整对象的大小,但需要保持比例。
我知道屏幕尺寸是多少。
如何计算将屏幕划分为的最佳行数和列数以及将对象缩放到什么尺寸?
谢谢,
杰米。
I have a dynamic number of equally proportioned and sized rectangular objects that I want to optimally display on the screen. I can resize the objects but need to maintain proportion.
I know what the screen dimensions are.
How can I calculate the optimal number of rows and columns that I will need to divide the screen in to and what size I will need to scale the objects to?
Thanks,
Jamie.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设所有矩形具有相同的尺寸和方向,并且不应更改。
让我们玩!
我希望我的数学是正确的,但这离你想要的不远;)
编辑:
比率与宽度和高度之间没有太大区别...
然后您返回使用“r”来找出行和列使用了多少。
Assuming that all rectangles have the same dimensions and orientation and that such should not be changed.
Let's play!
I hope I got my maths right, but that cannot be far from what you are looking for ;)
EDIT:
there is not much difference between having a ratio and the width and height...
And then you're back using 'r' to find out how much rows and columns use.
我喜欢的一种方法是使用面积的平方根:
让
r = 矩形数量
w = 显示宽度
h = 显示高度
那么,
A = (w * h) / r
是每个矩形的面积,
L = sqrt(A)
是每个矩形的底长。如果它们不是正方形,则只需相应相乘即可保持相同的比率。
做类似事情的另一种方法是只取矩形数量的平方根。这将为您提供网格的一维(即列数):
C = sqrt(n)
是网格中的列数,
R = n / C
code> 是行数。请注意,其中一个必须
ceiling
,另一个floor
,否则您将截断数字并可能会错过一行。One way I like to do that is to use the square root of the area:
Let
r = number of rectangles
w = width of display
h = height of display
Then,
A = (w * h) / r
is the area per rectangleand
L = sqrt(A)
is the base length of each rectangle.If they are not square, then just multiply accordingly to keep the same ratio.
Another way to do a similar thing is to just take the square root of the number of rectangles. That'll give you one dimension of your grid (i.e. the number of columns):
C = sqrt(n)
is the number of columns in your gridand
R = n / C
is the number of rows.Note that one of these will have to
ceiling
and the otherfloor
otherwise you will truncate numbers and might miss a row.杰米,我将“最佳行数和列数”解释为“有多少行和列将提供最大的矩形,与所需的比例和屏幕尺寸一致”。这是一种解释该解释的简单方法。
每个可能的选择(矩形的行数和列数)都会产生指定比例的矩形的最大可能尺寸。循环可能的选择并计算结果大小,在可能的解决方案空间上实现简单的线性搜索。下面是执行此操作的一些代码,使用 480 x 640 的示例屏幕和 3 x 5 比例的矩形。
运行该代码会提供以下跟踪:
由此可以清楚地看出,4x4 或 5x3 布局将生成最大的矩形。同样清楚的是,矩形大小(作为行数的函数)在极端处最差(最小),在中间点处最佳(最大)。假设矩形的数量不多,您可以简单地用您选择的语言编写上面的计算代码,但一旦结果面积在上升到最大值后开始减少,就立即退出。
这是一个快速而肮脏的(但我希望,相当明显的)解决方案。如果矩形的数量变得足够大,您可以通过多种方式调整性能:
Jamie, I interpreted "optimal number of rows and columns" to mean "how many rows and columns will provide the largest rectangles, consistent with the required proportions and screen size". Here's a simple approach for that interpretation.
Each possible choice (number of rows and columns of rectangles) results in a maximum possible size of rectangle for the specified proportions. Looping over the possible choices and computing the resulting size implements a simple linear search over the space of possible solutions. Here's a bit of code that does that, using an example screen of 480 x 640 and rectangles in a 3 x 5 proportion.
Running that code provides the following trace:
From this, it's clear that either a 4x4 or 5x3 layout will produce the largest rectangles. It's also clear that the rectangle size (as a function of row count) is worst (smallest) at the extremes and best (largest) at an intermediate point. Assuming that the number of rectangles is modest, you could simply code the calculation above in your language of choice, but bail out as soon as the resulting area starts to decrease after rising to a maximum.
That's a quick and dirty (but, I hope, fairly obvious) solution. If the number of rectangles became large enough to bother, you could tweak for performance in a variety of ways:
这几乎与肯尼思的问题完全一样。他还将其写在他的博客上。
如果在一维上缩放比例以便包装正方形,则会出现同样的问题。
This is almost exactly like kenneth's question here on SO. He also wrote it up on his blog.
If you scale the proportions in one dimension so that you are packing squares, it becomes the same problem.
您提到的行和列表明您设想将矩形排列在网格中,可能有一些空格(例如底行的一些)未填充。假设情况是这样:
假设您缩放对象,使得(数量未知)
n
的对象适合整个屏幕。现在假设有 N 个对象,因此将有
几行对象(其中 ceil 是 Ceiling 函数),它将占据
垂直高度。我们需要找到
n
,并选择最小的n
,使得该距离小于screenHeight
。由于存在上限函数,
n
的简单数学表达式变得更加棘手。如果列数相当小,找到n
的最简单方法可能就是循环增加n
直到满足不等式。编辑:我们可以从 for n 的上限开始循环
,然后向下计算:然后在 O(sqrt(N)) 中找到解决方案。 O(1) 解决方案是假设
或 采用
(Matthieu M. 的解决方案),但这些解决方案的缺点是
n
的值可能不是最优的。当
N=0
和N=1
且对象的长宽比满足objectHeight/objectWidth > 时,会出现边框情况。 screenHeight/screenWidth
- 这两个都很容易处理。Your mention of rows and columns suggests that you envisaged arranging the rectangles in a grid, possibly with a few spaces (e.g. some of the bottom row) unfilled. Assuming this is the case:
Suppose you scale the objects such that (an as-yet unknown number)
n
of them fit across the screen. ThenNow suppose there are N objects, so there will be
rows of objects (where ceil is the Ceiling function), which will take up
of vertical height. We need to find
n
, and want to choose the smallestn
such that this distance is smaller thanscreenHeight
.A simple mathematical expression for
n
is made trickier by the presence of the ceiling function. If the number of columns is going to be fairly small, probably the easiest way to findn
is just to loop through increasingn
until the inequality is satisfied.Edit: We can start the loop with the upper bound of
for n, and work down: the solution is then found in O(sqrt(N)). An O(1) solution is to assume that
or to take
(the solution of Matthieu M.) but these have the disadvantage that the value of
n
may not be optimal.Border cases occur when
N=0
, and whenN=1
and the aspect ratio of the objects is such thatobjectHeight/objectWidth > screenHeight/screenWidth
- both of these are easy to deal with.