2

我正在开发一个门户/仪表板类型的界面,它具有可以在仪表板空间中自由拖动的面板/小部件,只要它们不覆盖任何其他面板。可以通过包含所有可用面板的菜单将新面板添加到仪表板,当单击菜单项时,面板将被放置到仪表板中。当前占据仪表板空间的面板都表示在一个对象中,如下所示:

{
    'panel_1': { top: 0, left: 0, width: 300, height: 350 },
    'panel_2': { top: 0, left: 370, width: 200, height: 275 },
    'panel_3': { top: 370, left: 0, width: 275, height: 400 },
    ...
}

我的问题是,什么是有效的算法,当用户单击菜单中的一个时,它会正确地将一个新面板(具有给定宽度和高度)放置在最靠近左侧和顶部(x 和 y)的未占用空间中值 0、0,不重叠任何现有面板?

4

2 回答 2

2

我认为,简单的蛮力算法会适合你。我记得,适合矩形解决了另一个问题

遍历仪表板轴以找出是否可以放置矩形,直到X < rectangle.widh + dashboard.width,对于 Y。

仪表板上的 Foreach X,Y 遍历每个面板以查找它们是否重叠。您可以应用一些优化,以减少迭代量。如果面板重叠矩形,您可以增加 X 或 Y(在嵌套循环中)不是 1,而是面板的宽度或高度。

在大多数情况下,您不会进行dashboard.width*dashboard.height*panel.count迭代。通过一些优化,它会很快找到最合适的

于 2012-05-01T14:34:57.730 回答
0

我知道这是一个老问题,但如果有人想要概念证明,那么它看起来像这样:

function findSpace(width, height) {
    var $ul = $('.snap-layout>ul');
    var widthOfContainer = $ul.width();
    var heightOfContainer = $ul.height();
    var $lis = $ul.children('.setup-widget'); // The li is on the page and we dont want it to collide with itself

    for (var y = 0; y < heightOfContainer - height + 1; y++) {
        var heightOfShortestInRow = 1;
        for (var x = 0; x < widthOfContainer - width + 1; x++) {
            //console.log(x + '/' + y);
            var pos = { 'left': x, 'top': y };
            var $collider = $(isOverlapping($lis, pos, width, height));
            if ($collider.length == 0) {
                // Found a space
                return pos;
            }

            var colliderPos = $collider.position();
            // We have collided with something, there is no point testing the points within this widget so lets skip them
            var newX = colliderPos.left + $collider.width() - 1; // -1 to account for the ++ in the for loop
            x = newX > x ? newX : x; // Make sure that we are not some how going backwards and looping forever

            var colliderBottom = colliderPos.top + $collider.height();
            if (heightOfShortestInRow == 1 || colliderBottom - y < heightOfShortestInRow) {
                heightOfShortestInRow = colliderBottom - y; // This isn't actually the height its just the distance from y to the bottom of the widget, y is normally at the top of the widget tho
            }
        }
        y += heightOfShortestInRow - 1;
    }

    //TODO: Add the widget to the bottom
}


function isOverlapping($obsticles, tAxis, width, height) {
    var t_x, t_y;
    if (typeof (width) == 'undefined') {
        // Existing element passed in
        var $target = $(tAxis);
        tAxis = $target.position();
        t_x = [tAxis.left, tAxis.left + $target.outerWidth()];
        t_y = [tAxis.top, tAxis.top + $target.outerHeight()];
    } else {
        // Coordinates and dimensions passed in
        t_x = [tAxis.left, tAxis.left + width];
        t_y = [tAxis.top, tAxis.top + height];
    }

    var overlap = false;

    $obsticles.each(function () {
        var $this = $(this);
        var thisPos = $this.position();
        var i_x = [thisPos.left, thisPos.left + $this.outerWidth()]
        var i_y = [thisPos.top, thisPos.top + $this.outerHeight()];

        if (t_x[0] < i_x[1] && t_x[1] > i_x[0] &&
             t_y[0] < i_y[1] && t_y[1] > i_y[0]) {
            overlap = this;
            return false;
        }
    });
    return overlap;
}
于 2014-06-27T00:31:58.953 回答