我有N
可缩放的方形瓷砖(按钮)需要放在固定尺寸的矩形表面(工具箱)内。我想以相同的尺寸呈现按钮。
我如何解决瓷砖的最佳尺寸,以提供瓷砖覆盖的矩形表面的最大区域。
答案 0 :(得分:11)
让W
和H
为矩形的宽度和高度。
让s
为正方形边长。
然后,您可以放入矩形的方格n(s)
为floor(W/s)*floor(H/s)
。您想要找到s
n(s) >= N
的最大值
如果您对s
绘制平方数,您将获得分段常数函数。不连续性的值为W/i
和H/j
,其中i
和j
贯穿正整数。
您希望找到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 ++编写的算法:
#include <math.h>
#include <algorithm>
// find optimal (largest) tile size for which
// at least N tiles fit in WxH rectangle
double optimal_size (double W, double H, int N)
{
int i_min, j_min ; // minimum values for which you get at least N tiles
for (int i=round(sqrt(N*W/H)) ; ; i++) {
if (i*floor(H*i/W) >= N) {
i_min = i ;
break ;
}
}
for (int j=round(sqrt(N*H/W)) ; ; j++) {
if (floor(W*j/H)*j >= N) {
j_min = j ;
break ;
}
}
return std::max (W/i_min, H/j_min) ;
}
以上是为了清楚起见而写的。代码可以大大收紧如下:
double optimal_size (double W, double H, int N)
{
int i,j ;
for (i = round(sqrt(N*W/H)) ; i*floor(H*i/W) < N ; i++){}
for (j = round(sqrt(N*H/W)) ; floor(W*j/H)*j < N ; j++){}
return std::max (W/i, H/j) ;
}
答案 1 :(得分:9)
我相信这可以作为约束最小化问题来解决,这需要一些基本的微积分。 。
定义:
a, l -> rectangle sides
k -> number of squares
s -> side of the squares
你必须尽量减少这个功能:
f[s]:= a * l/s^2 - k
受限制:
IntegerPart[a/s] IntegerPart[l/s] - k >= 0
s > 0
我编写了一个Mathematica函数来编写技巧
f[a_, l_, k_] := NMinimize[{a l/s^2 - k ,
IntegerPart[a/s] IntegerPart[l/s] - k >= 0,
s > 0},
{s}]
易于阅读,因为方程与上述相同。
使用此功能,我编制了一个用于分配 6 正方形
的表格
据我所见,结果是正确的。
正如我所说,您可以为您的环境使用标准的微积分包,或者您也可以开发自己的最小化算法和程序。如果您决定使用最后一个选项,请按铃,我会提供一些好的指示。
HTH!
修改
为了好玩,我用结果制作了一个情节。
对于31块瓷砖:
编辑2:特征参数
问题有三个特征参数:
也许最后一个可能会有些令人惊讶,但很容易理解:如果您遇到7x5矩形和6个瓷砖的问题,请查看上表,方块的大小为2.33。现在,如果你有一个70x50的矩形,显然得到的图块将是23.33,与问题等距缩放。
因此,我们可以采用这三个参数并构建它们之间关系的三维图,并最终将曲线与一些更容易计算的函数匹配(例如使用最小二乘法或计算等值区域)。
无论如何,得到的缩放图是:
答案 2 :(得分:4)
我意识到这是一个老线程,但我最近以一种我认为有效并始终给出正确答案的方式解决了这个问题。它旨在维持给定的宽高比。如果您希望孩子(在这种情况下按钮)是正方形,只需使用1的宽高比。我目前在一些地方使用此算法并且效果很好。
double VerticalScale; // for the vertical scalar: uses the lowbound number of columns
double HorizontalScale;// horizontal scalar: uses the highbound number of columns
double numColumns; // the exact number of columns that would maximize area
double highNumRows; // number of rows calculated using the upper bound columns
double lowNumRows; // number of rows calculated using the lower bound columns
double lowBoundColumns; // floor value of the estimated number of columns found
double highBoundColumns; // ceiling value of the the estimated number of columns found
Size rectangleSize = new Size(); // rectangle size will be used as a default value that is the exact aspect ratio desired.
//
// Aspect Ratio = h / w
// where h is the height of the child and w is the width
//
// the numerator will be the aspect ratio and the denominator will always be one
// if you want it to be square just use an aspect ratio of 1
rectangleSize.Width = desiredAspectRatio;
rectangleSize.Height = 1;
// estimate of the number of columns useing the formula:
// n * W * h
// columns = SquareRoot( ------------- )
// H * w
//
// Where n is the number of items, W is the width of the parent, H is the height of the parent,
// h is the height of the child, and w is the width of the child
numColumns = Math.Sqrt( (numRectangles * rectangleSize.Height * parentSize.Width) / (parentSize.Height * rectangleSize.Width) );
lowBoundColumns = Math.Floor(numColumns);
highBoundColumns = Math.Ceiling(numColumns);
// The number of rows is determined by finding the floor of the number of children divided by the columns
lowNumRows = Math.Ceiling(numRectangles / lowBoundColumns);
highNumRows = Math.Ceiling(numRectangles / highBoundColumns);
// Vertical Scale is what you multiply the vertical size of the child to find the expected area if you were to find
// the size of the rectangle by maximizing by rows
//
// H
// Vertical Scale = ----------
// R * h
//
// Where H is the height of the parent, R is the number of rows, and h is the height of the child
//
VerticalScale = parentSize.Height / lowNumRows * rectangleSize.Height;
//Horizontal Scale is what you multiply the horizintale size of the child to find the expected area if you were to find
// the size of the rectangle by maximizing by columns
//
// W
// Vertical Scale = ----------
// c * w
//
//Where W is the width of the parent, c is the number of columns, and w is the width of the child
HorizontalScale = parentSize.Width / (highBoundColumns * rectangleSize.Width);
// The Max areas are what is used to determine if we should maximize over rows or columns
// The areas are found by multiplying the scale by the appropriate height or width and finding the area after the scale
//
// Horizontal Area = Sh * w * ( (Sh * w) / A )
//
// where Sh is the horizontal scale, w is the width of the child, and A is the aspect ratio of the child
//
double MaxHorizontalArea = (HorizontalScale * rectangleSize.Width) * ((HorizontalScale * rectangleSize.Width) / desiredAspectRatio);
//
//
// Vertical Area = Sv * h * (Sv * h) * A
// Where Sv isthe vertical scale, h is the height of the child, and A is the aspect ratio of the child
//
double MaxVerticalArea = (VerticalScale * rectangleSize.Height) * ((VerticalScale * rectangleSize.Height) * desiredAspectRatio);
if (MaxHorizontalArea >= MaxVerticalArea ) // the horizontal are is greater than the max area then we maximize by columns
{
// the width is determined by dividing the parent's width by the estimated number of columns
// this calculation will work for NEARLY all of the horizontal cases with only a few exceptions
newSize.Width = parentSize.Width / highBoundColumns; // we use highBoundColumns because that's what is used for the Horizontal
newSize.Height = newSize.Width / desiredAspectRatio; // A = w/h or h= w/A
// In the cases that is doesnt work it is because the height of the new items is greater than the
// height of the parents. this only happens when transitioning to putting all the objects into
// only one row
if (newSize.Height * Math.Ceiling(numRectangles / highBoundColumns) > parentSize.Height)
{
//in this case the best solution is usually to maximize by rows instead
double newHeight = parentSize.Height / highNumRows;
double newWidth = newHeight * desiredAspectRatio;
// However this doesn't always work because in one specific case the number of rows is more than actually needed
// and the width of the objects end up being smaller than the size of the parent because we don't have enough
// columns
if (newWidth * numRectangles < parentSize.Width)
{
//When this is the case the best idea is to maximize over columns again but increment the columns by one
//This takes care of it for most cases for when this happens.
newWidth = parentSize.Width / Math.Ceiling(numColumns++);
newHeight = newWidth / desiredAspectRatio;
// in order to make sure the rectangles don't go over bounds we
// increment the number of columns until it is under bounds again.
while (newWidth * numRectangles > parentSize.Width)
{
newWidth = parentSize.Width / Math.Ceiling(numColumns++);
newHeight = newWidth / desiredAspectRatio;
}
// however after doing this it is possible to have the height too small.
// this will only happen if there is one row of objects. so the solution is to make the objects'
// height equal to the height of their parent
if (newHeight > parentSize.Height)
{
newHeight = parentSize.Height;
newWidth = newHeight * desiredAspectRatio;
}
}
// if we have a lot of added items occaisionally the previous checks will come very close to maximizing both columns and rows
// what happens in this case is that neither end up maximized
// because we don't know what set of rows and columns were used to get us to where we are
// we must recalculate them with the current measurements
double currentCols = Math.Floor(parentSize.Width / newWidth);
double currentRows = Math.Ceiling(numRectangles/currentCols);
// now we check and see if neither the rows or columns are maximized
if ( (newWidth * currentCols ) < parentSize.Width && ( newHeight * Math.Ceiling(numRectangles/currentCols) ) < parentSize.Height)
{
// maximize by columns first
newWidth = parentSize.Width / currentCols;
newHeight = newSize.Width / desiredAspectRatio;
// if the columns are over their bounds, then maximized by the columns instead
if (newHeight * Math.Ceiling(numRectangles / currentCols) > parentSize.Height)
{
newHeight = parentSize.Height / currentRows;
newWidth = newHeight * desiredAspectRatio;
}
}
// finally we have the height of the objects as maximized using columns
newSize.Height = newHeight;
newSize.Width = newWidth;
}
}
else
{
//Here we use the vertical scale. We determine the height of the objects based upong
// the estimated number of rows.
// This work for all known cases
newSize.Height = parentSize.Height / lowNumRows;
newSize.Width = newSize.Height * desiredAspectRatio;
}
在算法结束时,'newSize'保持适当的大小。这是用C#编写的,但是移植到其他语言会相当容易。
答案 3 :(得分:0)
第一个非常粗略的启发式是
s = floor( sqrt( (X x Y) / N) )
其中s
是按钮长度,X
和Y
是工具箱的宽度和高度,N
是按钮的数量。< / p>
在这种情况下,s
将是最大可能的边长。但是,不一定可以将这组按钮映射到工具栏上。
想象一个工具栏,它是20个单位乘以1个单位,带有5个按钮。启发式将使您的边长为2(面积为4),总覆盖面积为20.但是,每个按钮的一半将位于工具栏之外。
答案 4 :(得分:0)
我会在这里采取迭代方法。 我会检查是否可以将所有按钮放在一行中。 如果没有,检查是否可以放入两行,依此类推。
假设W是工具箱的较小一面。 H是另一方。
对于每次迭代,我会按顺序检查最佳和最差的情况。最好的情况意味着,比如它是第n次迭代,会尝试W / n X W / n大小按钮的大小。如果h值足够,那么我们就完成了。如果不是,最坏的情况是尝试(W /(n + 1))+ 1 X(W /(n + 1))+ 1大小的按钮。如果可以适合所有按钮,那么我会尝试在W / n和(W /(n + 1))+ 1之间的二分法。如果不是迭代继续在n + 1。
答案 5 :(得分:0)
设n(s)是可以适合的方格数和它们的边。设W,H为要填充的矩形的边。然后n(s)= floor(W / s)* floor(H / s)。这是s中的单调递减函数并且也是分段常数,因此您可以执行二进制搜索的略微修改以找到最小s,使得n(s)> = N但n(s + eps)<1。 N.你从su = min(W,H)和l = floor(min(W,H)/ N)的上限和下限开始,然后计算t =(u + l)/ 2.如果n(t) &gt; = N. 然后l = min(W / floor(W / t),H / floor(H / t))否则u = max(W / floor(W / t),H / floor(H / t))。当你和我在连续迭代中保持不变时停止。 所以它就像二进制搜索,但你利用了函数是分段常数的事实,而变化点是当W或H是s的精确倍数时。很好的小问题,谢谢你提出来。
答案 6 :(得分:0)
我们知道任何最佳解决方案(可能有两个)都会水平或垂直填充矩形。如果您找到了一个未在一个维度上填充矩形的最佳解决方案,则可以始终增加切片的比例以填充一个维度。
现在,任何最大化覆盖表面的解决方案都会使宽高比接近矩形的宽高比。解决方案的宽高比为vertical tile count/horizontal tile count
(矩形的宽高比为Y/X
)。
您可以通过强制Y>=X
来简化问题。换句话说,如果X>Y
,则转置矩形。这使您只需考虑纵横比&gt; = 1,只要您记得转换回来。
一旦计算了宽高比,您就希望找到V/H ~= Y/X
问题的解决方案,其中V
是垂直图块计数,H
是水平图块计数。您最多可以找到三种解决方案:最近的V/H
到Y/X
和V+1
,V-1
。此时,只需使用V
和H
根据比例计算覆盖率,并取最大值(可能不止一个)。