我有 N 个方格。
我有一个长方形的盒子。
我希望所有的方块都能放进盒子里。
我希望正方形尽可能大。
如何计算正方形的最大尺寸以使它们都适合盒子?
这适用于缩略图库中的缩略图。
int function thumbnailSize(
iItems, // The number of items to fit.
iWidth, // The width of the container.
iHeight, // The height of the container.
iMin // The smallest an item can be.
)
{
// if there are no items we don't care how big they are!
if (iItems = 0) return 0;
// Max size is whichever dimension is smaller, height or width.
iDimension = (iWidth min iHeight);
// Add .49 so that we always round up, even if the square root
// is something like 1.2. If the square root is whole (1, 4, etc..)
// then it won't round up.
iSquare = (round(sqrt(iItems) + 0.49));
// If we arrange our items in a square pattern we have the same
// number of rows and columns, so we can just divide by the number
// iSquare, because iSquare = iRows = iColumns.
iSize = (iDimension / iSquare);
// Don't use a size smaller than the minimum.
iSize = (iSize max iMin);
return iSize;
}
这段代码目前工作正常。其背后的想法是采用矩形容器的最小尺寸,假设该容器是该尺寸的正方形,然后假设我们有相同数量的行和列,刚好足以容纳 iItems 正方形。
如果容器大部分是方形的,则此功能非常有用。但是,如果您有一个长矩形,则缩略图会比应有的小。例如,如果我的矩形为 100 x 300,并且有三个缩略图,则它应该返回 100,但实际上返回 33。
可能不是最佳的(如果它有效,我还没有尝试过),但我认为比你当前的方法更好:
w:矩形的宽度
h:矩形的高度
n:图像数量
a = w*h :矩形的面积。
is = a/n 理想情况下图像的最大面积。
il = sqrt(ia) 理想情况下图像的最大长度。
nw = round_up(w/il):需要堆叠的图像数量。
nh = round_up(h/il):需要相邻堆叠的图像数量。
l = min(w/nw, w/nh) :要使用的图像的长度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)