Fitting rectangles together in optimal fashion
Asked Answered
Z

4

10

I was wondering if anyone knows of any algorithms suited to fitting together N number of rectangles of unknown size into the smallest possible containing rectangle.

By optimal I mean with reducing the amount of white space left over in the resulting containing rectangle.

I would like to use this to generate css sprites from a series of images.

Many Thanks,

Ian

Zelda answered 8/6, 2010 at 15:13 Comment(3)
Why does the containing shape also have to be square? Why can't it be rectangular but not square?Chardin
Good point. I meant rectangle. I'll edit.Zelda
google.com/search?q=rectangle+packing+algorithmAnzac
Z
1

Through packing images into square texture and Simon's answer I got to this link http://code.activestate.com/recipes/442299/

I did not check the recipe, but it seems to allow using non-square containers.

Zen answered 8/6, 2010 at 15:36 Comment(0)
R
2

I think what you describe is a variant of the "two dimensional bin packing" problem. The only difference is that you have the items and are trying to find the smallest rectangle.

This survey article is a good start.

Ruvolo answered 8/6, 2010 at 15:42 Comment(0)
Z
1

Through packing images into square texture and Simon's answer I got to this link http://code.activestate.com/recipes/442299/

I did not check the recipe, but it seems to allow using non-square containers.

Zen answered 8/6, 2010 at 15:36 Comment(0)
P
1

The only way to guarantee and optimal solution is to brute force the answer. This quickly becomes unmanagable for personal computers when you have several rectangles, and allow for the possibility of rotation.

Wikipedia has a good article on packing problem http://en.wikipedia.org/wiki/Packing_problem

Polypeptide answered 8/6, 2010 at 16:18 Comment(0)
K
0

Here is a good description of a fast packing algorithm - http://www.codeproject.com/KB/web-image/rectanglepacker.aspx

Kenley answered 3/1, 2012 at 1:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.