-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalletizing.js
55 lines (46 loc) · 1.25 KB
/
palletizing.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
"use strict";
var maxRect = require("./lib/solvers/maxRect");
var Pallet = require("./lib/Pallet");
var getArea = require("./lib/utils/getArea");
var getTotalArea = require("./lib/utils/getTotalArea");
module.exports = function(rectangles, options){
options = options || {};
if(!options.silent){
console.log("Packing " + rectangles.length +
" rectangles using the maximal rectangles algorithm.");
}
var pallet = new Pallet(rectangles),
totalArea = options.silent ? 0 : getTotalArea(rectangles),
bestArea = Infinity, bestLayout = null;
for(;;){
var layout = maxRect(rectangles, pallet.w, pallet.h);
if(layout){
var area = getArea(rectangles, layout);
if(area < bestArea){
bestArea = area;
bestLayout = layout;
if(!options.silent){
console.log("Found rectangle " + pallet.w +
" by " + pallet.h + " wasting " +
(area - totalArea) + " pixels.");
}
}
if(!pallet.next(area)){
break;
}
}else{
if(!pallet.next()){
break;
}
}
}
if(!options.silent){
var waste = bestArea - totalArea;
if(waste){
console.log("The best solution wasted " + waste + " pixels.");
}else{
console.log("Found ideal solution.");
}
}
return {area: bestArea, layout: bestLayout, rectangles: rectangles};
};