使用缩放图块最大化矩形区域覆盖范围的算法
我有 N 个可扩展的方形图块(按钮),需要放置在固定大小的矩形表面(工具箱)内。我想以相同的尺寸呈现所有按钮。
我怎样才能找到瓷砖的最佳尺寸,以提供瓷砖覆盖的矩形表面的最大面积。
I have N
scalable square tiles (buttons) that need to be placed inside of fixed sized rectangular surface (toolbox). I would like to present the buttons all at the same size.
How could I solve for the optimal size of the tiles that would provide the largest area of the rectangular surface being covered by tiles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
令
W
和H
为矩形的宽度和高度。令
s
为正方形的边长。那么可以放入矩形的方格数
n(s)
就是floor(W/s)*floor(H/s)
。您想要找到s
的最大值,其中n(s) >= N
如果您针对
s
绘制方格数,您可以将得到分段常数函数。不连续点位于值W/i
和H/j
处,其中i
和j
穿过正值整数。您想要找到满足
n(W/i) >= N
的最小i
,以及类似地,满足n(W/i) >= N
的最小j
>n(H/j)>=N。将这些最小值称为i_min
和j_min
。那么W/i_min
和H/j_min
中最大的就是您想要的s
。即
s_max = max(W/i_min,H/j_min)
要找到
i_min
和j_min
,只需进行强力搜索:对于每个,从 1 开始,测试并递增。如果 N 很大,从 1 开始搜索 i 和 j 可能会很令人厌恶(尽管很难想象会出现这样的情况)性能上是否有任何明显的差异)。在这种情况下,我们可以如下估计起始值。首先,图块面积的大致估计值为
W*H/N
,对应于sqrt(W*H/N)
的边。如果W/i <= sqrt(W*H/N)
,则i >= ceil(W*sqrt(N/(W*H)))
,类似地j >= ceil(H*sqrt(N/(W*H)))
因此,不要在
i=1
和处开始循环j=1
,我们可以从i = ceil(sqrt(N*W/H))
和j = ceil(sqrt(N*H/W)) 开始它们)
。 OP 表明,round 比 ceil 效果更好——最坏的情况是额外的迭代。以下是用 C++ 阐明的算法:
上面的内容是为了清楚起见而编写的。代码可以大大收紧,如下所示:
Let
W
andH
be the width and height of the rectangle.Let
s
be the length of the side of a square.Then the number of squares
n(s)
that you can fit into the rectangle isfloor(W/s)*floor(H/s)
. You want to find the maximum value ofs
for whichn(s) >= N
If you plot the number of squares against
s
you will get a piecewise constant function. The discontinuities are at the valuesW/i
andH/j
, wherei
andj
run through the positive integers.You want to find the smallest
i
for whichn(W/i) >= N
, and similarly the smallestj
for whichn(H/j) >= N
. Call these smallest valuesi_min
andj_min
. Then the largest ofW/i_min
andH/j_min
is thes
that you want.I.e.
s_max = max(W/i_min,H/j_min)
To find
i_min
andj_min
, just do a brute force search: for each, start from 1, test, and increment.In the event that N is very large, it may be distasteful to search the
i
's andj
's starting from 1 (although it is hard to imagine that there will be any noticeable difference in performance). In this case, we can estimate the starting values as follows. First, a ballpark estimate of the area of a tile isW*H/N
, corresponding to a side ofsqrt(W*H/N)
. IfW/i <= sqrt(W*H/N)
, theni >= ceil(W*sqrt(N/(W*H)))
, similarlyj >= ceil(H*sqrt(N/(W*H)))
So, rather than start the loops at
i=1
andj=1
, we can start them ati = ceil(sqrt(N*W/H))
andj = ceil(sqrt(N*H/W)))
. And OP suggests thatround
works better thanceil
-- at worst an extra iteration.Here's the algorithm spelled out in C++:
The above is written for clarity. The code can be tightened up considerably as follows:
我相信这可以作为约束最小化问题来解决,这需要一些基本的微积分。 。
定义:
您必须最小化该函数:
受约束:
我编写了一个 Mathematica 函数来实现这一点
易于阅读,因为方程与上面相同。
使用此函数,我制作了一个表格,用于
分配 6 方块
尽可能 看看,结果是正确的。
正如我所说,您可以使用适合您环境的标准微积分包,或者您也可以开发自己的最小化算法和程序。如果您决定选择最后一个选项,请按铃,我会提供一些好的建议。
哈!
编辑
只是为了好玩,我用结果绘制了一个图。
对于 31 个图块:
编辑 2:特征参数
该问题具有三个特征参数:
也许最后一个可能是结果有些令人惊讶,但很容易理解:如果您对放置 7x5 矩形和 6 个图块有疑问,请查看上表,正方形的大小将为 2.33。现在,如果您有一个 70x50 的矩形,显然生成的图块将是 23.33,根据问题进行等距缩放。
因此,我们可以采用这三个参数并构建它们关系的 3D 图,并最终将曲线与一些更容易计算的函数相匹配(例如使用最小二乘法或计算等值区域)。
无论如何,生成的缩放图是:
I believe this can be solved as a constrained minimisation problem, which requires some basic calculus. .
Definitions:
You have to minimise the function:
subject to the constraints:
I programed a little Mathematica function to do the trick
Easy to read since the equations are the same as above.
Using this function I made up a table for allocating 6 squares
as far as I can see, the results are correct.
As I said, you may use a standard calculus package for your environment, or you may also develop your own minimisation algorithm and programs. Ring the bell if you decide for the last option and I'll provide a few good pointers.
HTH!
Edit
Just for fun I made a plot with the results.
And for 31 tiles:
Edit 2: Characteristic Parameters
The problem has three characteristic parameters:
Perhaps the last one may result somewhat surprising, but it is easy to understand: if you have a problem with a 7x5 rectangle and 6 tiles to place, looking in the above table, the size of the squares will be 2.33. Now, if you have a 70x50 rectangle, obviously the resulting tiles will be 23.33, scaling isometrically with the problem.
So, we can take those three parameters and construct a 3D plot of their relationship, and eventually match the curve with some function easier to calculate (using least squares for example or computing iso-value regions).
Anyway, the resulting scaled plot is:
我意识到这是一个旧线程,但我最近以一种我认为有效并且总是给出正确答案的方式解决了这个问题。它旨在保持给定的纵横比。如果您希望子项(本例中为按钮)为正方形,只需使用宽高比 1。我目前在一些地方使用此算法,效果很好。
在算法结束时,“newSize”保持适当的大小。这是用 C# 编写的,但移植到其他语言相当容易。
I realize this is an old thread but I recently solved this problem in a way that I think is efficient and always gives the correct answer. It is designed to maintain a given aspect ratio. If you wish for the children(buttons in this case) to be square just use an aspect ratio of 1. I am currently using this algorithm in a few places and it works great.
At the end of the algorithm 'newSize' holds the appropriate size. This is written in C# but it would be fairly easy to port to other languages.
第一个非常粗略的启发式是采用
其中
s
是按钮边长,X
和Y
是按钮的宽度和高度。工具箱,N
是按钮的数量。在这种情况下,
s
将是最大可能的边长。然而,不一定可以将这组按钮映射到工具栏上。想象一个 20 个单位 x 1 个单位、带有 5 个按钮的工具栏。启发式将为您提供边长为 2(面积为 4)的总覆盖面积为 20。但是,每个按钮的一半将位于工具栏之外。
The first, very rough heuristic is to take
where
s
is the button-side-length,X
andY
are the width and height of the toolbox, andN
is the number of buttons.In this case,
s
will be the MAXIMUM possible side-length. It is not necessarily possible to map this set of buttons onto the toolbar, however.Imagine a toolbar that is 20 units by 1 unit with 5 buttons. The heuristic will give you a side length of 2 (area of 4), with a total covering area of 20. However, half of each button will be outside of the toolbar.
我会在这里采取迭代方法。
我会检查是否可以将所有按钮放在一行中。
如果不是,请检查是否可以放入两行,依此类推。
假设 W 是工具箱较小的一侧。
H是另一边。
对于每次迭代,我都会按顺序检查最好和最坏的可能情况。最好的情况意味着,假设这是第 n 次迭代,将尝试 W/n XW/n 大小的按钮。如果 h 值足够,那么我们就完成了。如果不是,最坏的情况是尝试 (W/(n+1))+1 X (W/(n+1))+1 大小的按钮。如果可以容纳所有按钮,那么我会尝试 W/n 和 (W/(n+1))+1 之间的二分法。如果不是,则迭代在 n+1 处继续。
I would take an iterative approach here.
I would check if it is possible to fit all button in a single row.
If not, check if it is possible to fit in two rows, and so on.
Say W is the smaller side of the toolbox.
H is the other side.
For each iteration, I would check for the best and worst possible cases, in that order. Best case means, say it is the nth iteration, would try a size of W/n X W/n sized buttons. If h value is enough then we are done. If not, the worst case is to try (W/(n+1))+1 X (W/(n+1))+1 sized buttons. If it is possible to fit all buttons, then i would try a bisection method between W/n and (W/(n+1))+1. If not iteration continues at n+1.
设 n(s) 为可容纳的正方形数量,s 为其边长。令 W、H 为要填充的矩形的边。那么n(s) = 楼层(W/s)* 楼层(H/s)。这是 s 中的单调递减函数,也是分段常数,因此您可以对二分搜索进行轻微修改以找到最小的 s,使得 n(s) >= N 但 n(s+eps) < N. 从 su = min(W, H) 和 l = Floor(min(W,H)/N) 的上限和下限开始,然后计算 t = (u + l) / 2。如果 n(t) >=N
那么 l = min(W/floor(W/t), H/floor(H/t)) 否则 u = max(W/floor(W/t), H/floor(H/t))。当 u 和 l 在连续迭代中保持相同时停止。
所以它就像二分搜索,但你利用了这样一个事实:函数是分段常数,并且变化点是当 W 或 H 是 s 的精确倍数时。很好的小问题,谢谢你提出来。
Let n(s) be the number of squares that can fit and s their side. Let W, H be the sides of the rectangle to fill. Then n(s) = floor(W/s)* floor(H/s). This is a monotonically decreasing function in s and also piecewise constant, so you can perform a slight modification of binary search to find the smallest s such that n(s) >= N but n(s+eps) < N. You start with an upper and lower bound on s u = min(W, H) and l = floor(min(W,H)/N) then compute t = (u + l) / 2. If n(t) >= N
then l = min(W/floor(W/t), H/floor(H/t)) otherwise u = max(W/floor(W/t), H/floor(H/t)). Stop when u and l stay the same in consecutive iterations.
So it's like binary search, but you exploit the fact that the function is piecewise constant and the change points are when W or H are an exact multiple of s. Nice little problem, thanks for proposing it.
我们知道任何最佳解决方案(可能有两个)都会水平或垂直填充矩形。如果您找到了未在一维上填充矩形的最佳解决方案,则始终可以增加图块的比例以填充一维。
现在,任何最大化覆盖表面的解决方案都将具有接近矩形的长宽比。该解决方案的长宽比为
垂直瓦片数/水平瓦片数
(矩形的长宽比为Y/X
)。您可以通过强制
Y>=X
来简化问题;换句话说,如果X>Y
,则转置矩形。这允许您只考虑宽高比 >= 1,只要您记得将解决方案转回即可。计算出纵横比后,您需要找到
V/H ~= Y/X
问题的解决方案,其中V
是垂直平铺数,< code>H 是水平图块计数。您将找到最多三个解决方案:最接近Y/X
的V/H
和V+1
、V-1< /代码>。此时,只需使用
V
和H
根据比例计算覆盖范围并取最大值(可能有多个)。We know that any optimal solution (there may be two) will fill the rectangle either horizontally or vertically. If you found an optimal solution that did not fill the rectangle in one dimension, you could always increase the scale of the tiles to fill one dimension.
Now, any solution that maximizes the surface covered will have an aspect ratio close to the aspect ratio of the rectangle. The aspect ratio of the solution is
vertical tile count/horizontal tile count
(and the aspect ratio of the rectangle isY/X
).You can simplify the problem by forcing
Y>=X
; in other words, ifX>Y
, transpose the rectangle. This allows you to only think about aspect ratios >= 1, as long as you remember to transpose the solution back.Once you've calculated the aspect ratio, you want to find solutions to the problem of
V/H ~= Y/X
, whereV
is the vertical tile count andH
is the horizontal tile count. You will find up to three solutions: the closestV/H
toY/X
andV+1
,V-1
. At that point, just calculate the coverage based on the scale usingV
andH
and take the maximum (there could be more than one).