# How do I get the final size of the tray with Java bin packing algorithm&quest;

Using this 2d bin-packing algorithm
(EDIT: Fixed demo) which is a variation of this
how can I get the final bin width and height of each bin?

My demo code follows:

```var blocks = [
{w: 1000, h: 800},
{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);
}
}```

The original algorithm only deals with a single, ever-expanding bin so I modified it to take a max width and height. Then I run through the array of blocks, call the packer, push fit blocks to a new array and unset them from ‘blocks’ until ‘blocks’ is empty. Whether that was the best approach is the subject of another question.

Anyway, I’ve tried modifying growNode like so:

```growNode: function(w, h) {
var canGrowRight  = (w <= this.root.w && this.root.w + w <= maxW);
var canGrowDown = (h <= this.root.h && this.root.h + h <= maxH);

if (canGrowRight) {
this.sheetW = this.root.w + w; //<--------------added
return this.growRight(w, h);
}
else if (canGrowDown) {
this.sheetH = this.root.h + h; //<--------------added
return this.growDown(w, h);
}
else

return null; // need to ensure sensible root starting size to avoid this happening
},```

which works for every sheet but the first one. I tried to add these lines in a few other methods as well with no success. I also tried to get the sheet size from the last block in the sheet’s width + x but that only works if the sheet is full.

My question again is how can I get the final sheet size for each sheet?

|