I am looking for some pointers to algorithms that should allow to tile with no overlap different size rectangles.
Given a set of different sized rectangles, tile them on an area of size H x W with no overlap. The objective would be to maximize the space used or conversely - minimize area of gaps. If there is not enough space, proceed on the second area of the same size and so on.
It is assumed that each rectangle's width and height are smaller than respective dimensions of the tiling area. Rectangles are not rotated or otherwise transformed - i.e. their sides are either horizontal or vertical.
I am not looking for finished code, just curious what approaches/algorithms are the best to use to solve this problem.
Most simple is to use a kd-tree to subdivide the tree into vertical and horizontal euklidian 2d space. Then you can pack a rectangle into one of the created space and recursively subdivide the tree. There is a Jquery treemap plugin example available online. The jquery plugin masonry can do the same but it's more like a 1d bin-packing solver. 2d bin-packing is much more complicated and can also mean to rotate rectangles. Here is an example for packing lightmaps: http://www.blackpawn.com/texts/lightmaps/default.html.
I have one idea that could go in the right direction. The idea is to track ratio of tile area vs white area in a bounding box.
input: unordered set of input rectangles output: filled area
- Define empty bounding box
- Select such two rectangles A_i and B_j from the input set whose bounding box B contains minimal blank area ratio
- Update the bounding box with the two optimal boxes
- Put the bounding box in a corner, say (1,1)
- Repeat until no boxes
- Take a new box from the set such as the updated bounding box has minimum blank
- Constrain growing in horizontal or vertical direction if the bounding box width or height exceeds that of output area
- If it is impossible to add a new box, proceed with a new area H x W and restart the algorithm, else update the bounding box
There are still some points to define - how to best decide the place of bounding box? How to impose the bounding box growing restrictions? How to efficiently find the best bounding box?
来源:https://stackoverflow.com/questions/11724066/tiling-different-size-rectangles