Bin Packing Js实施,使用盒子旋转以获得最佳配合 [英] Bin Packing Js implementation using box rotation for best fit

查看:114
本文介绍了Bin Packing Js实施,使用盒子旋转以获得最佳配合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里 https://github.com/jakesgordon/bin-包装

当我将帧尺寸指定为800x600

When I specify the frame size as 800x600

并将块尺寸指定为150x700,150x700它会说,它不能容纳,但是,有足够的空间。制作700x150和700x150时,效果相同。

and Blocks size as 150x700,150x700 it would say that, it cant accommodate However, there is ample space. The same when 700x150, 700x150 is made, it would fit it.

我如何修改代码,以便它可以动态旋转块大小并适合

How Do I adapt the code, so that it can dynamically rotate the block size and fits in to the frame.

此处使用的js包装器是

The js packer used here is,

    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;
  }

}


推荐答案

我认为以下代码可以解决问题...?! (即,我进行了有限的测试,但是对于我测试的东西,它似乎可以工作。)

I think the code below might do the trick...?! (Ie, I did limited testing, but for what I tested, it appears to work.)

我基本上在 findnode 中添加了另一个选项例行程序,如果块不适合其预定义的方向,则可以旋转块(即,切换宽度和高度尺寸)。这涉及在 block 中添加另一个称为 rotate 的属性,以指示尺寸已被交换。 (当然,引入交换和 rotate 属性是必要的,必须将 block 传递给 findnode 而不是 w h ,如前面的代码。)

I essentially added another option in the findnode routine to rotate the block (ie, switch the width and height dimensions) as an option if it doesn't fit in it's predefined orientation. This involved adding another property to block dubbed rotate as an indicator that the dimensions were swapped. (And the introduction of swapping and the rotate property necessitated, of course, passing block to findnode rather than w and h as in the previous code.)

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];
      block.rotate = false;
      if (node = this.findNode(this.root, block))
        block.fit = this.splitNode(node, block);
    }  
  },

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

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

希望这能帮到您。

这篇关于Bin Packing Js实施,使用盒子旋转以获得最佳配合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆