如何使用nodejs打包2D盒子

How to pack 2D boxes with nodejs?

本文关键字:2D 盒子 打包 nodejs 何使用      更新时间:2023-09-26

我正在使用节点js开发一个Web应用程序,我需要使用盒子打包算法来找到最佳解决方案。我可以尝试自己做一个算法(http://en.wikipedia.org/wiki/Packing_problems),但我想知道是否已经存在这样的东西?知道吗?

目前我有一个看起来像这样的对象数组。

var box = [
    {info: 'some info', width:200, height: 50},
    {info: 'some info', width:200, height: 50}
];

我想要(x,y)坐标,知道将每个盒子打包到2D空间的哪个位置。

你有背包节点js模块(我是开发人员),可以做你需要的。

您可以传递任何对象列表,只要它们定义了heightwidth属性。下面是一个简短的示例:

var BackPack = require("backpacking");
var boxes = [];
for(var i = 0; i<20; i++){
    var width = Math.floor(Math.random() * (20 - 5 + 1)) + 5;
    var height = Math.floor(Math.random() * (20 - 5 + 1)) + 5;
    boxes.push({info: 'box_'+i, 'width': width, 'height': height});
}
// Define the width and the height of the container where you want to pack your boxes.
backPack = new BackPack(40, 10000);
// Here you have the packedBoxes with de x and y coordinates.
packedBoxes = backPack.pack(boxes);

免責聲明

解决方案不是最佳的。但这是一个快速的算法。包装质量应在下个月提高。

更多信息

查看 github.com 自述文件以获取更多详细信息 https://github.com/paulfournel/backpacking/