lightweight rectangle packing algorithm
pack is a box packing algorithm which determines the most compact configuration for a set of rectangles. To this end, it calculates all possible permutations and rates them by area of whitespace remaining.
Unlike traditional bin packing algorithms, the boxes here are placed in a single container that grows appropriately as they are packed inside. This property makes it ideal for assembling spritesheets given a list of images.
A rough example has been designed for testing and demonstration purposes.
Pto pack existing boxes.
Rto generate a random configuration.
Nto reset the layout to its initial state.
In this demo, boxes are chunked into groups of 4 as they are packed together to reduce calculation times. Unfortunately, this approach tends to yield suboptimal results. This issue will be addressed as library performance is improved upon.
pack(sizes[, layout]) -> layout
Arranges a list of
[ width, height ] pairs into the most compact
layout possible. The result will be reminiscent of the following data structure:
var layout =size: 13 8boxes:size: 5 3 position: 0 0size: 6 5 position: 0 3size: 5 4 position: 6 0size: 2 4 position: 11 0size: 7 4 position: 6 4size: 1 3 position: 5 0
The boxes will be placed around the provided
layout if given (this is used internally for recursion), otherwise an empty one will be created.