Code Monkey home page Code Monkey logo

boxpack's Introduction

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

boxpack's People

Contributors

munro avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.