- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我这里用到了装箱js实现https://github.com/jakesgordon/bin-packing
当我将帧大小指定为 800x600 时
Blocks size as 150x700,150x700 它会说,它不能容纳 但是,有足够的空间。 700x150的时候也一样,做成700x150的就可以了。
我如何调整代码,以便它可以动态旋转 block 大小并适合框架。
这里使用的js打包器是,
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;
}
}
最佳答案
我正在添加第二个答案,因为这与第一个答案完全不同,并试图通过问题中提出的 2D Bin Packing 算法解决更核心的问题。使用该特定算法,splitNode
例程生成可用于拟合 block 的 down
和 right
节点,但没有考虑到作为可用节点积累,相邻节点的联合可以容纳更大的 block ......
例如,在下面提出的算法中,给定一个 800x600 的初始堆,放置一个 500x300 的 block 将导致该堆包含 (0,300)-(800,600) 和 (500,0)-(800,600) 两个堆 block 。这两个 heapBlock 将在 (500,300)-(800,600) 的区域重叠,以确保在搜索适合 block 的位置时表示最大的 heapBlock 区域。而在 2D Bin Packing 算法中,down
或 right
没有相交区域,而是倾向于一个或另一个节点的潜在重叠空间...
下面的算法试图通过实现代表最大可用 block 的可用堆 block 数组来弥补这个缺点,即使这些堆 block 彼此重叠。缺点是这引入了一个 O(n^2) 算法 (unionAll
) 来管理堆,在 O(n) 循环 (fit
) 之上通过 block 来适应。因此,该算法的性能可能接近 O(n^3),尽管这可能是更糟糕的情况……
Packer = function(w, h) {
this.init(w, h);
};
Packer.prototype = {
init: function(w, h) {
this._root = { x: 0, y: 0, w: w, h: h }
},
intersect: function(block0, block1) {
//
// Returns the intersecting block of
// block0 and block1.
//
let ix0 = Math.max(block0.x0, block1.x0);
let ix1 = Math.min(block0.x1, block1.x1);
let iy0 = Math.max(block0.y0, block1.y0);
let iy1 = Math.min(block0.y1, block1.y1);
if (ix0 <= ix1 && iy0 <= iy1) {
return {x0: ix0, y0: iy0, x1: ix1, y1: iy1};
} else {
return null;
}
},
chunkContains: function(heapBlock0, heapBlock1) {
//
// Determine whether heapBlock0 totally encompasses (ie, contains) heapBlock1.
//
return heapBlock0.x0 <= heapBlock1.x0 && heapBlock0.y0 <= heapBlock1.y0 && heapBlock1.x1 <= heapBlock0.x1 && heapBlock1.y1 <= heapBlock0.y1;
},
expand: function(heapBlock0, heapBlock1) {
//
// Extend heapBlock0 and heapBlock1 if they are
// adjoining or overlapping.
//
if (heapBlock0.x0 <= heapBlock1.x0 && heapBlock1.x1 <= heapBlock0.x1 && heapBlock1.y0 <= heapBlock0.y1) {
heapBlock1.y0 = Math.min(heapBlock0.y0, heapBlock1.y0);
heapBlock1.y1 = Math.max(heapBlock0.y1, heapBlock1.y1);
}
if (heapBlock0.y0 <= heapBlock1.y0 && heapBlock1.y1 <= heapBlock0.y1 && heapBlock1.x0 <= heapBlock0.x1) {
heapBlock1.x0 = Math.min(heapBlock0.x0, heapBlock1.x0);
heapBlock1.x1 = Math.max(heapBlock0.x1, heapBlock1.x1);
}
},
unionMax: function(heapBlock0, heapBlock1) {
//
// Given two heap blocks, determine whether...
//
if (heapBlock0 && heapBlock1) {
// ...heapBlock0 and heapBlock1 intersect, and if so...
let i = this.intersect(heapBlock0, heapBlock1);
if (i) {
if (this.chunkContains(heapBlock0, heapBlock1)) {
// ...if heapBlock1 is contained by heapBlock0...
heapBlock1 = null;
} else if (this.chunkContains(heapBlock1, heapBlock0)) {
// ...or if heapBlock0 is contained by heapBlock1...
heapBlock0 = null;
} else {
// ...otherwise, let's expand both heapBlock0 and
// heapBlock1 to encompass as much of the intersected
// space as possible. In this instance, both heapBlock0
// and heapBlock1 will overlap.
this.expand(heapBlock0, heapBlock1);
this.expand(heapBlock1, heapBlock0);
}
}
}
},
unionAll: function() {
//
// Loop through the entire heap, looking to eliminate duplicative
// heapBlocks, and to extend adjoining or intersecting heapBlocks,
// despite this introducing overlapping heapBlocks.
//
for (let i = 0; i < this.heap.length; i++) {
for (let j = 0; j < this.heap.length; j++) {
if (i !== j) {
this.unionMax(this.heap[i],this.heap[j]);
if (this.heap[i] && this.heap[j]) {
if (this.chunkContains(this.heap[j], this.heap[i])) {
this.heap[i] = null;
} else if (this.chunkContains(this.heap[i], this.heap[j])) {
this.heap[j] = null;
}
}
}
}
}
// Eliminate the duplicative (ie, nulled) heapBlocks.
let onlyBlocks = [];
for (let i = 0; i < this.heap.length; i++) {
if (this.heap[i]) {
onlyBlocks.push(this.heap[i]);
}
}
this.heap = onlyBlocks;
},
fit: function(blocks) {
//
// Loop through all the blocks, looking for a heapBlock
// that it can fit into.
//
this.heap = [{x0:0,y0:0,x1:this._root.w, y1: this._root.h}];
var n, node, block;
for (n = 0; n < blocks.length; n++) {
block = blocks[n];
block.rotate = false;
if (this.findInHeap(block)) {
this.adjustHeap(block);
} else {
// If the block didn't fit in its current orientation,
// rotate its dimensions and look again.
block.w = block.h + (block.h = block.w, 0);
block.rotate = true;
if (this.findInHeap(block)) {
this.adjustHeap(block);
}
}
}
},
findInHeap: function(block) {
//
// Find a heapBlock that can contain the block.
//
for (let i = 0; i < this.heap.length; i++) {
let heapBlock = this.heap[i];
if (heapBlock && block.w <= heapBlock.x1 - heapBlock.x0 && block.h <= heapBlock.y1 - heapBlock.y0) {
block.x0 = heapBlock.x0;
block.y0 = heapBlock.y0;
block.x1 = heapBlock.x0 + block.w;
block.y1 = heapBlock.y0 + block.h;
return true;
}
}
return false;
},
adjustHeap: function(block) {
//
// Find all heap entries that intersect with block,
// and adjust the heap by breaking up the heapBlock
// into the possible 4 blocks that remain after
// removing the intersecting portion.
//
let n = this.heap.length;
for (let i = 0; i < n; i++) {
let heapBlock = this.heap[i];
let overlap = this.intersect(heapBlock, block);
if (overlap) {
// Top
if (overlap.y1 !== heapBlock.y1) {
this.heap.push({
x0: heapBlock.x0,
y0: overlap.y1,
x1: heapBlock.x1,
y1: heapBlock.y1
});
}
// Right
if (overlap.x1 !== heapBlock.x1) {
this.heap.push({
x0: overlap.x1,
y0: heapBlock.y0,
x1: heapBlock.x1,
y1: heapBlock.y1
});
}
// Bottom
if (heapBlock.y0 !== overlap.y0) {
this.heap.push({
x0: heapBlock.x0,
y0: heapBlock.y0,
x1: heapBlock.x1,
y1: overlap.y0
});
}
// Left
if (heapBlock.x0 != overlap.x0) {
this.heap.push({
x0: heapBlock.x0,
y0: heapBlock.y0,
x1: overlap.x0,
y1: heapBlock.y1
});
}
this.heap[i] = null;
}
}
this.unionAll();
}
}
fit
算法会将结果留在heap
和传入的blocks
数组中。例如……
p = new Packer(2400,1200);
blocks = [{w:2100,h:600},{w:2100,h:600},{w:150,h:200},{w:740,h:200},{w:500,h:100}];
p.fit(blocks);
...将留下 p.heap
和 blocks
如下...
The final HEAP
[{"x0":2100,"y0":940,"x1":2400,"y1":1200},
{"x0":2300,"y0":500,"x1":2400,"y1":1200},
{"x0":2250,"y0":0,"x1":2300,"y1":200}]
The final BLOCKS
[{"w":2100,"h":600,"rotate":false,"x0":0,"y0":0,"x1":2100,"y1":600},
{"w":2100,"h":600,"rotate":false,"x0":0,"y0":600,"x1":2100,"y1":1200},
{"w":150,"h":200,"rotate":false,"x0":2100,"y0":0,"x1":2250,"y1":200},
{"w":200,"h":740,"rotate":true,"x0":2100,"y0":200,"x1":2300,"y1":940},
{"w":100,"h":500,"rotate":true,"x0":2300,"y0":0,"x1":2400,"y1":500}]
请注意,此算法未优化。即,它不会对传入的 block 进行排序(即,按宽度、高度或面积等),也不会在执行 unionAll
后对堆进行排序,这会将堆减少到最大大小的列表堆 block 。 (即,在每次调用 unionAll
后,都有机会按宽度、高度或面积等对堆进行排序,以便在堆中搜索下一个要放置的可用 block 时,如果堆是从大到小排序,算法会将 block 放置在最大的可用堆 block 中,或者如果从小到大排序, block 将放置在刚好足够大的堆 block 中...)无论如何,将保留这些类型的优化作为练习。
另外,请以怀疑的态度看待这个算法,并进行适当的测试。
关于javascript - Bin Packing Js 实现使用盒子旋转以获得最佳匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642111/
bin、bin/、bin/*和bin/**有什么区别我的 .gitignore 文件?我一直在使用 bin/,但正在查看 other .gitignore files (在 eclipse file
给定 n 个无限容量的箱子,我想将 m 件元素装入其中(每个元素都有特定的重量),同时最小化最重箱子的重量。 这不是传统的垃圾箱包装/背包问题,其中垃圾箱的容量有限,而您试图尽量减少使用的垃圾箱数量;
Closed. This question is off-topic。它当前不接受答案。 想改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 7年前关
我一直在寻找一个问题的解决方案,这个问题显然比我最初想象的更不寻常。 Optaplanner 看起来很有前途,但由于我对 Java 相对缺乏经验,所以我想在深入研究之前调查一下这是否完全不可能。 我正
我有一个琐碎的问题。我有一个非常大的 df 有很多列。我正在尝试找到最有效的方法来对具有不同 bin 大小的所有列进行 bin 并创建一个新的 df。这是一个仅对单个列进行分箱的示例: import
我想以这样的方式对数字列 (var) 进行分类,使每个分类中的行数大致相同。我的附加要求是该列中的一个(唯一)值不能分配给多个 bin。例如,如果 var 列中的值 1 分配给 bin 1,则不允许将
我在 centos (rhel 7)_ 上使用 make 命令和 gcc 7.1 通过代码进行编译,但遇到了以下错误: /bin/sh:/bin: 是一个目录 which sh 返回/usr/bin/
/usr/bin/ld: cannot find -ldlib /usr/bin/ld: cannot find -lcblas /usr/bin/ld: cannot find -llapack 在
我进行了一项眼动追踪实验,试图检测两个区域(感兴趣区域,名为“代理”和“患者”的 AOI)的注视分布。我将整个时间划分为时间段,例如得到以下列表: Stimulus Participant A
我正在尝试替换 whoami带有 ~/.local/bin/ 中的脚本的命令.有没有办法让我的 whoami 获得优先权,这样当我运行 whoami 时,我的脚本就会运行? 最佳答案 这就是我的 ~/
我的项目结构是这样的: . ├── Cargo.lock ├── Cargo.toml └── src ├── bin │ └── other.rs ├── main.rs
我正在开发一个小型图书馆应用程序,它以这种格式存储技术手册: 目前,我正在尝试将库的内容保存并根据用户的需要加载到 bin 文件中。 但是,当我尝试加载库文件时,唯一的变化是“-1”被打印到控制台。此
我试图忽略 Web 项目的 bin 文件夹,但包含该 bin 文件夹中的 .refresh 文件。 这是我的 .gitignore 中的内容: [Bb]in/ #Allow .refresh file
ipv6_hdr__f_next_header_cp: coverpoint this.ipv6_hdr.ipv6_f_next_header iff (this.has_ipv6_header){
今天有人给我发了一个以 #: 开头的脚本,谷歌搜索后我没有找到任何答案。 即使脚本有效,我想知道那是什么意思。 最佳答案 哇!这让我想起了很多记忆! 回到 1980 年代和 90 年代初期,有两种基本
我使用这个 ksh 函数将“1-Jan-2011”格式转换为“1.1.2011”。 #!/bin/ksh ##---- function to convert 3 char month into nu
我在 c# winform 项目中使用 sql-server compact 数据库。 如果我查看构成解决方案的文件,我可以在以下两个文件夹中看到 exe 和 sdf 文件的副本: /bin/Debu
这个问题与我上一个问题不同。我正在通过以下代码使用 facetgrid 打印直方图。 import numpy as np import pandas as pd import seaborn as
我有一个整数需要根据概率分布分成 bin。例如,如果我有 N=100 对象进入 [0.02, 0.08, 0.16, 0.29, 0.45] 那么你可能会得到 [1, 10, 20 , 25, 44]
我在寻找使用 Jenkins 运行 Appium 的脚本时阅读了以下脚本 #!/bin/bash --login killall -9 "iPhone Simulator" &> /dev/null
我是一名优秀的程序员,十分优秀!