Reputation: 337
I want to take a bounding rectangle and a list of other rectangles (x, y, width, height) that may overlap each other or the bounds and move/resize them to fit perfectly inside of the bounds, leaving no spaces or overlaps.
I've searched Google, Stack Exchange and other resources and can't find a name for this algorithm let alone an implementation. Is there a standard method of implementing this?
Here's what I hope the algorithm would do:
Additional thoughts:
Upvotes: 4
Views: 2698
Reputation: 5458
This seems like problem of finding layout structure of given rectangles.
General layout of rectangles can be of any kind, like in his problem, which is probably hard to solve. I would attack this problem by defining simple layout model which can be used to transform input data into it, and after that use that layout data to fill input boundary rectangle.
The simplest layout is with dividing a box into two boxes by stacking them horizontally or vertically. In your example boundary rectangle is divided into two horizontally stacked boxes. Right one is divided into vertically stacked boxes, and top one of them is divided horizontally.
Main step for creation of layout for given set of rectangles is to find horizontal or vertical line that divides the best set of rectangles. After that same method is applied on two sets of rectangles produces by splitting.
After layout structure is created, it is easy to find size and position of rectangles by recursively merging boxes bottom to top to calculate relative size. Than propagate position and size by going top to bottom.
Improvement to this layout is layout that allows stacking more boxes horizontally or vertically. Result is the same, but this approach is probably faster since less iterations of finding of splitting line is needed.
Upvotes: 0
Reputation: 18838
A renown problem which is related to your problem is "Pallet Loading" problem. In general, the complexity class of this problem is not known and is an open problem. You can read more about the problem and its variation in this article.
Upvotes: 1