boxpack

Bin packing algorithm for rectangles

Boxpack—Bin packing algorithm!

Bin packing algorithm!

var bin = boxpack();
 
var boxes = bin.pack([
    {width: 50, height: 100},
    {width: 75, height: 75},
    {width: 25, height: 25},
    {width: 100, height: 100}
]);
 
document.getElementById('result').innerHTML = boxes.map(function (box) {
    return '<div style="' +
        'position:absolute;' +
        'border:1px solid black;' +
        'left:' + box.x + 'px;' +
        'top:' + box.y + 'px;' +
        /* subtract border from width & height */
        'width:' + (box.width - 2) + 'px;' +
        'height:' + (box.height - 2) + 'px"></div>';
}).join('');

Defines rectangles that we want to pack in a bin.

Defines where a rectangle was packed in a bin.

Creates a new bin for packing rects into.

result will return false if there was no room left to pack the Rect. Otherwise it will return Box

Sorts boxes based on their distance from (0, 0)

Sorts boxes based on their distance from the X-axis

Sorts boxes based on their distance from the Y-axis

MIT