Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal Layout Algorithm [duplicate]

I'm implementing a CSS Sprites setup in my ASP.NET application based on this article..

http://weblogs.asp.net/zowens/archive/2008/03/05/css-sprite-for-asp-net.aspx

In the section "Get the SPRITE already!"... the author says...

"The only real down side to this is that all your images are lined up vertically. I'm not quite sure that this matters, but most other generators have the ability to somehow line the images up vertically and horizontally."

I'm sure this must be a previously solved problem so my question is, "Given a collection of rectangles, can you efficiently lay these out in an optimal way?" (I suppose it depends on my definition of optimal, but being the closest possible to a square as a start. (although this may not actually be the case.)) Also, would you need all the rectangles in advance or could this be done progressively?

like image 772
ChrisV Avatar asked Dec 06 '25 15:12

ChrisV


1 Answers

This is known as the rectangle packing problem. Even in very simple cases, finding the optimal solution is NP-hard, although often there are good heuristics. Googling rectangle packing gives some interesting algorithms and code.

like image 149
David Norman Avatar answered Dec 08 '25 15:12

David Norman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!