/
diagonal-packer.js
55 lines (44 loc) · 1.27 KB
/
diagonal-packer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var DialonalPacker = function () {
};
//自下而上的拼图
DialonalPacker.prototype = {
constructor: DialonalPacker,
//[w:10,h:10]
fit: function (blocks) {
var n, block, len = blocks.length, prevBlock;
// this.root = { x: 0, y: 0, w: w, h: h };
for (n = 0; n < len; n++) {
block = blocks[n];
if (!this.root) {
this.root = {
x: 0,
y: 0,
w: block.w,
h: block.h
};
block.fit = this.root;
} else {
this.root = {
x: 0,
y: 0,
w: block.w + this.root.w,
h: block.h + this.root.h,
rightTop: {
x: this.root.w,
y: 0,
w: block.w,
h: block.h
}
}
block.fit = this.root.rightTop;
}
for (var j = 0; j < n; j++) {
prevBlock = blocks[j];
if (prevBlock.fit) {
prevBlock.fit.y += block.h;
}
}
}
}
}
module.exports = DialonalPacker;