Exposé的布局算法算法、布局、Expos

2023-09-11 04:01:20 作者:你若安好便是晴天

我在做一些东西,勾画出项目类似于Mac OS X上确实与Exposé中的窗口。它可以适应的物品的纵横比和可用面积的高宽比。

I'm making something which lays items out similar to what Mac OS X does with windows in Exposé. It adapts to the aspect ratio of the items and the aspect ratio of the available area.

基本上,可用面积被分成行和列。一个项目是把每个小区(一个行和列的交叉点)在。这些项目必须保持其纵横比(这里宽/高),尽​​管单元格的纵横比。细胞的数量的必须的大于或等于的项目数。在细胞的数目大于项目的数量的情况下,最后一行将不会被充分利用。 我们的目标是让尽可能多的通过项目尽可能利用现有的区域。我是pretty的确保每个单元的长宽比是更接近项目的宽高比,就更好了。

Basically, the available area is divided up into rows and columns. An item is put in each cell (the intersection of a row and column). The items must maintain their aspect ratio (here width / height) despite the aspect ratio of the cell. The number of cells must be greater than or equal to the number of items. In the case where the number of cells is greater than the number of items, the last row will not be fully utilized. The goal is to have as much of the available area utilized by items as possible. I'm pretty sure the closer each cell's aspect ratio is to the item's aspect ratio, the better.

以下的作业以及当可用面积的高宽比等于项'长宽比:

The following works well when the available area's aspect ratio is equal to the items' aspect ratios:

rows    := round(sqrt(count));
columns := ceiling(sqrt(count));

其中:计数是项目的数量; 圆(X) X 来最接近的整数值,四舍五入中途情况下,远离零;和上限(X)返回最小的积分值不大于 X

Where: count is the number of items; round(x) rounds x to nearest integral value, rounding halfway cases away from zero; and ceiling(x) returns the smallest integral value not less than x.

我知道Compiz的使用下列类似的算法,但它并没有考虑到这两个项目,可利用区域的纵横比:

I know Compiz uses the following similar algorithm, but it doesn't take into account the aspect ratios of the items and available area:

rows    := floor(sqrt(count + 1));
columns := ceiling(count / rows);

其中:楼(X)返回比 X 最大积分值不大于

Where: floor(x) returns the largest integral value not greater than x.

我放在一起以下O(n)的算法,该算法测试的行和列的每个组合,并寻找最适合的,但肯定有一个O(1)算法,因为这会产生完全相同的结果作为第一个(O( 1))的算法时这两个项目,可利用区域的纵横比是相同的:

I put together the following O(n) algorithm which tests every combination of rows and columns and looks for the best fit, but surely there's a O(1) algorithm since this produces exactly the same results as the first (O(1)) algorithm when the aspect ratios of the items and available area are the same:

fit (itemCount, itemRatio, availableRatio)
{
    bestRows := infinity;
    bestColumns := infinity;
    bestDiff := infinity;

    for (rows := 1; rows <= count; rows += 1)
    {
        columns := ceiling(count / rows);

        cellWidth  := availableRatio / columns;
        cellHeight := 1.0 / rows;
        cellRatio := cellWidth / cellHeight;

        diff := abs(cellRatio - itemRatio);

        if (diff < bestDiff)
        {
            bestRows := rows;
            bestColumns := columns;
            bestDiff := diff;

            if (diff = 0)
                break;
        }
    }

    return (bestRows, bestColumns);
}

其中: ABS(X)返回 X

注:您可能会注意到,这是不优化的的所有的

NOTE: You may notice this isn't optimized at all

那么,什么是必须通过的项目尽可能使用最多的可用区域的最佳方法是什么? (换句话说,如何找到最适合的?)

So, what's the best way to have the most available area utilized by the items as possible? (In other words, how do I find the best fit?)

推荐答案

您可能会寻找'2D包装算法。

You might be looking for '2d packing algorithms'.