4
\$\begingroup\$

I need to put as many rectangle of same size as possible inside a polygon. The algorithm can put rectangles in different orientation(angle). But they cannot overlap. This is image is an example of the end result: image of rectangles inside larger rectangle

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Any constraints on the shape of the polygon? It's also a rectangle in your illustration, which is a relatively simple case. \$\endgroup\$ Apr 9, 2012 at 12:11
  • \$\begingroup\$ If that's a rectangle, then perhaps google for 2D bin packing might help. If not, your problem is not NP complete for sure.. \$\endgroup\$
    – teodron
    Apr 9, 2012 at 13:34
  • \$\begingroup\$ This is asking for backtracking for sure. \$\endgroup\$
    – kaoD
    Apr 9, 2012 at 13:48
  • \$\begingroup\$ @eBusiness, The Polygon can be in any shape but the rectangle is in fixed size. \$\endgroup\$ Apr 9, 2012 at 18:04
  • \$\begingroup\$ @teodron, The Polygon can be in any shape but the rectangle is in a fixed size. \$\endgroup\$ Apr 9, 2012 at 18:05

2 Answers 2

5
\$\begingroup\$

This is called the Pallet Loading Problem. Solving it is actually pretty hard, and we don’t know of an exact solution that always works in reasonable time. And sometimes the solution is not intuitive at all, see for instance:

solution

Here is a comprehensive list of existing algorithms (Recursive Five-block Algorithm, L-Algorithm, Recursive Partitioning Algorithm) with full source code. The same authors have a web application that lets you enter your own data and see the result.

\$\endgroup\$
1
  • \$\begingroup\$ thank you for the website. those algorithms work for a rectangle within other rectangles, would you know of an algorithm which handles rectangles within polygons? \$\endgroup\$
    – BenKoshy
    Feb 3, 2017 at 23:46
4
\$\begingroup\$

In this sample you can find an algorithm to pack small textures into a big texture, that is quite similar to your matter... and maybe a good start to solve it.

http://create.msdn.com/en-US/education/catalog/sample/sprite_sheet

EDIT:

You can use a tree, where the leafs have empty regions, and the rest of nodes have the rectangles. You can iterate deeply to get a solution.

Algoritm

At first pass, the node contains the empty region. There are four combinations for adding the rectangle, by the orientation ( Vertical, Horizontal) and the way that region will be divided.

Add the four combinations.

Choose the first child,

Try to add next rectangle, adding every combination as before

If there are no room for the rectangle go back, and test next child of this parent.

Repeat that until there is no more rectangles to add.

enter image description here

\$\endgroup\$
8
  • \$\begingroup\$ Thanks. I will check that. Is this work for a random polygon where I need to place rectangles in any direction? \$\endgroup\$ Apr 9, 2012 at 18:12
  • \$\begingroup\$ This seems to be for unknown length packing. I have rectangles which lengths are fixed. \$\endgroup\$ Apr 11, 2012 at 3:15
  • \$\begingroup\$ I think the problem would be the opposite... ;) \$\endgroup\$
    – Blau
    Apr 11, 2012 at 6:58
  • \$\begingroup\$ I am wondering whether there is no per-existing algorithm available for fixed size. Thanks anyway. \$\endgroup\$ Apr 12, 2012 at 5:36
  • \$\begingroup\$ What is useful for variable length size is useful for fixed length size... I don't understand what is your matter... \$\endgroup\$
    – Blau
    Apr 12, 2012 at 9:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .