gpt4 book ai didi

algorithm - 一种实现矩形装箱的方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:21:36 43 4
gpt4 key购买 nike

我正在尝试使用最大矩形算法实现 2D 装箱,如下文所示。

http://clb.demon.fi/files/RectangleBinPack.pdf

为了实现这一点,哪种类型的数据结构最合适?谷歌搜索后我发现有不同的 Guillotine 打包算法使用树实现。同样的方法也可以应用于此吗?算法本身对我来说不是很清楚。我也可以对此进行更多说明吗?

最佳答案

我在研究自动生成 CSS Sprite 时偶然发现了一些东西。我认为在源代码中,矩形由元组表示,实际算法使用二叉树。也许它对你有用。

http://codeincomplete.com/posts/2011/5/7/bin_packing/

编辑这是来自 https://github.com/jakesgordon/bin-packing/blob/master/js/packer.js 的代码示例

/******************************************************************************

This is a very simple binary tree based bin packing algorithm that is initialized
with a fixed width and height and will fit each block into the first node where
it fits and then split that node into 2 parts (down and right) to track the
remaining whitespace.

Best results occur when the input blocks are sorted by height, or even better
when sorted by max(width,height).

Inputs:
------

w: width of target rectangle
h: height of target rectangle
blocks: array of any objects that have .w and .h attributes

Outputs:
-------

marks each block that fits with a .fit attribute pointing to a
node with .x and .y coordinates

Example:
-------

var blocks = [
{ w: 100, h: 100 },
{ w: 100, h: 100 },
{ w: 80, h: 80 },
{ w: 80, h: 80 },
etc
etc
];

var packer = new Packer(500, 500);
packer.fit(blocks);

for(var n = 0 ; n < blocks.length ; n++) {
var block = blocks[n];
if (block.fit) {
Draw(block.fit.x, block.fit.y, block.w, block.h);
}
}


******************************************************************************/

Packer = function(w, h) {
this.init(w, h);
};

Packer.prototype = {

init: function(w, h) {
this.root = { x: 0, y: 0, w: w, h: h };
},

fit: function(blocks) {
var n, node, block;
for (n = 0; n < blocks.length; n++) {
block = blocks[n];
if (node = this.findNode(this.root, block.w, block.h))
block.fit = this.splitNode(node, block.w, block.h);
}
},

findNode: function(root, w, h) {
if (root.used)
return this.findNode(root.right, w, h) || this.findNode(root.down, w, h);
else if ((w <= root.w) && (h <= root.h))
return root;
else
return null;
},

splitNode: function(node, w, h) {
node.used = true;
node.down = { x: node.x, y: node.y + h, w: node.w, h: node.h - h };
node.right = { x: node.x + w, y: node.y, w: node.w - w, h: h };
return node;
}

}

关于algorithm - 一种实现矩形装箱的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21078959/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com