简体   繁体   中英

Perfectly fill a rectangle with a list of smaller rectangles

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:

  1. I'm using Javascript, but a reference implementation in any language would help
  2. Where overlaps occur, any method of resizing the rectangles is fine but I would probably choose the mid point of the overlap
  3. Final rectangles may be any size, including 0 in one dimension, as long as they all fit within the bounds
  4. My data has the bounds and rectangles normalized to a 0-1 range, where 0,0 is the top left corner of the bounds and 1,1 is the bottom right corner

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM