GithubHelp home page GithubHelp logo

georgetsu / rectangle-packer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from penlect/rectangle-packer

0.0 0.0 0.0 301 KB

Rectangle packing program

License: MIT License

C 44.42% Python 41.00% Makefile 1.63% Cython 12.94%

rectangle-packer's Introduction

Welcome to rectangle-packer

PyPI-Versions PyPI-Wheel Build-Status Read-the-Docs GitHub-License

PyPI-Downloads

Primary use: Given a set of rectangles with fixed orientations, find a bounding box of minimum area that contains them all with no overlap.

This project is inspired by Matt Perdeck's blog post Fast Optimizing Rectangle Packing Algorithm for Building CSS Sprites.

  • The latest documentation is available on Read the Docs.
  • The source code is available on GitHub.

Installation

Install latest version from PyPI:

$ python3 -m pip install rectangle-packer

Or clone the repository and install with:

$ python3 setup.py install

Basic usage

# Import the module
>>> import rpack

# Create a bunch of rectangles (width, height)
>>> sizes = [(58, 206), (231, 176), (35, 113), (46, 109)]

# Pack
>>> positions = rpack.pack(sizes)

# The result will be a list of (x, y) positions:
>>> positions
[(0, 0), (58, 0), (289, 0), (289, 113)]

The output positions are the lower left corner coordinates of each rectangle in the input.

These positions will yield a packing with no overlaps and enclosing area as small as possible (best effort).

Note

  • You must use positive integers as rectangle width and height.
  • The module name is rpack which is an abbreviation of the package name at PyPI (rectangle-packer).
  • The computational time required by :py:func:`rpack.pack` increases by the number and size of input rectangles. If this becomes a problem, you might need to implement your own divide-and-conquer algorithm.

Examples

Example A:

pack10

Example B:

packphi

Example C: Sometimes the input rectangles simply cannot be packed in a good way. Here is an example of low packing density:

pack10bad

Example D: The image below is contributed by Paul Brodersen, and illustrates a solution to a problem discussed at stackoverflow.

PaulBrodersenExampleImage

rectangle-packer's People

Contributors

penlect 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.