boxpack
Bin packing algorithm for rectangles
Last updated 12 years ago .
BSD · Repository · Original npm · Tarball · package.json
$ npm install boxpack 
SYNC missed versions from official npm registry.

Boxpack—Bin packing algorithm! Build Status

Bin packing algorithm!

Example

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('');

Data structures

{Rect} {width: {Number}, height: {Number}}

Defines rectangles that we want to pack in a bin.

{Box} {x: {Number}, y: {Number}, width: {Number}, height: {Number}}

Defines where a rectangle was packed in a bin.

Bin methods

var bin = boxpack({Object} options)

Creates a new bin for packing rects into.

var result = bin.pack({width: 50, height: 100})

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

Weighting algorithms

boxpack.algo.dist

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

boxpack.algo.top

Sorts boxes based on their distance from the X-axis

boxpack.algo.left

Sorts boxes based on their distance from the Y-axis

Helper functions

boxpack.rectFit({Rect}, {Box}) -> {Boolean}

boxpack.boxFit({Box}, {Box}) -> {Boolean}

boxpack.intersect({Box}, {Box}) -> {Boolean}

boxpack.divideX({Box}, {Number}) -> {Array {Box}}

boxpack.divideY({Box}, {Number}) -> {Array {Box}}

boxpack.subtract({Box}, {Box}) -> {Array {Box}}

License

MIT

Current Tags

  • 0.1.0                                ...           latest (12 years ago)

1 Versions

  • 0.1.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE