Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GrowingPacker = function(maxW, maxH) { this.maxW = maxW; this.maxH = maxH;};
- GrowingPacker.prototype = {
- fit: function(blocks) {
- var n, node, block, len = blocks.length;
- var w = len > 0 ? blocks[0].w : 0;
- var h = len > 0 ? blocks[0].h : 0;
- this.root = { x: 0, y: 0, w: w, h: h };
- for (n = 0; n < len ; n++) {
- block = blocks[n];
- if (node = this.findNode(this.root, block.w, block.h)) {
- block.fit = this.splitNode(node, block.w, block.h);
- }
- else {
- block.fit = this.growNode(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;
- },
- growNode: function(w, h) {
- var canGrowRight = (w <= this.root.w && this.root.w + w <= this.maxW);
- var canGrowDown = (h <= this.root.h && this.root.h + h <= this.maxH);
- if (canGrowRight) {
- return this.growRight(w, h);
- }
- else if (canGrowDown) {
- return this.growDown(w, h);
- }
- else
- return null; // need to ensure sensible root starting size to avoid this happening
- },
- growRight: function(w, h) {
- this.root = {
- used: true,
- x: 0,
- y: 0,
- w: this.root.w + w,
- h: this.root.h,
- down: this.root,
- right: { x: this.root.w, y: 0, w: w, h: this.root.h }
- };
- if (node = this.findNode(this.root, w, h))
- return this.splitNode(node, w, h);
- else
- return null;
- },
- growDown: function(w, h) {
- this.root = {
- used: true,
- x: 0,
- y: 0,
- w: this.root.w,
- h: this.root.h + h,
- down: { x: 0, y: this.root.h, w: this.root.w, h: h },
- right: this.root
- };
- if (node = this.findNode(this.root, w, h))
- return this.splitNode(node, w, h);
- else
- return null;
- }
- }
- /*****************************************************************************************************/
- var blocks = [
- { w: 500, h: 700 },
- { w: 500, h: 700 },
- { w: 500, h: 350 },
- { w: 500, h: 350 },
- { w: 500, h: 350 },
- { w: 500, h: 350 },
- { w: 500, h: 350 },
- { w: 500, h: 350 },
- { w: 500, h: 350 },
- { w: 500, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 },
- { w: 250, h: 350 }
- ];
- var sheets = [];
- while(blocks.length) {
- var packer = new GrowingPacker(1000,800);
- packer.fit(blocks);
- sheet = [];
- for (var i=blocks.length-1; i>=0; i--) {
- if (blocks[i].fit !== undefined && blocks[i].fit !== null) {
- //console.log(blocks[i].fit);
- sheet.unshift(blocks[i]);
- blocks.splice(i,1);
- }
- }
- //console.log(sheet[sheet.length-1].fit.y + sheet[sheet.length-1].h);
- //console.log(sheet);
- sheets.push(sheet);
- }
- for(var i=0; i<sheets.length; i++) {
- var sheet = sheets[i];
- var sheetWidth = sheet[sheet.length-1].w + sheet[sheet.length-1].fit.x;
- var sheetHeight = sheet[sheet.length-1].h + sheet[sheet.length-1].fit.y;
- for(var j=0; j<sheet.length; j++) {
- console.log("SHEET #" + i + " - W: " + sheetWidth + " H: " + sheetHeight + " BLOCK #" + j + " - W: " + sheet[j].w + " H: " + sheet[j].h + " X: " + sheet[j].fit.x + " Y: " + sheet[j].fit.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement