6px / bin-packer
为PHP提供的2D bin packing
v0.2.0
2023-03-27 12:39 UTC
Requires
- php: ^7.2|^8.1
Requires (Dev)
- ext-imagick: *
- phpunit/phpunit: ^9.1
Suggests
- ext-imagick: To use the visualizer and GIF maker
This package is auto-updated.
Last update: 2024-09-27 17:13:02 UTC
README
为PHP提供的2D bin packing,包含旋转和增长功能
从 Padam87/bin-packer 分支而来
用法
基本
$bin = new Bin(1000, 1000); $blocks = [ new Block(100, 100), new Block(300, 100), new Block(175, 125), new Block(200, 75), new Block(200, 75), ]; $packer = new BinPacker(); $blocks = $packer->pack($bin, $blocks);
确定结果(是否成功打包了块?)
foreach ($blocks as $block) { if ($block->getNode() && $block->getNode()->isUsed()) { // packed } }
旋转
默认情况下,所有块都允许旋转。只有在初始方向找不到合适的位置时才会发生旋转。
您可以通过将块构造函数的第三个参数设置为false
来禁用旋转。
new Block(100, 100, false);
识别块
有时为块设置一个标识符可能会有用。构造函数的第四个参数是块ID。
new Block(100, 100, false, 'My id, can be anything.');
bin增长
允许bin增长,您可以每次都放得下所有块。
您可以通过将bin构造函数的第三个参数设置为true
来启用宽度增长,通过设置第四个参数来启用高度增长。
$bin = new Bin(1000, 1000, true, false);
此bin仅允许在宽度上增长。
可视化器
您可以使用可视化器创建已打包bin的图片。
$bin = new Bin(1000, 1000); $blocks = [ new Block(100, 100), new Block(300, 100), new Block(175, 125), new Block(200, 75), new Block(200, 75), ]; $packer = new BinPacker(); $blocks = $packer->pack($bin, $blocks); $image = $visualizer->visualize($bin, $blocks);
此功能使用Imagick扩展,并返回一个\Imagick类。您可以使用结果来保存或显示图像。
$image->setFormat('jpg'); $image->writeImage('bin.jpg');
GIF创建器
警告 GIF创建器的性能非常慢。我建议仅将其用于调试目的或非实时场景。
$packer = new BinPacker(); $gifMaker = new GifMaker(new Visualizer()); $blocks = $packer->pack($bin, $blocks, $gifMaker); $gif = $gifMaker->create(); $gif->writeImages('bin.gif', true);