C# Rectangle Packing
Sometimes, you’re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without wasting much space.
What you need then, is a rectangle packer, an algorithm that arranges as many smaller rectangles on a larger rectangle as can possibly fit.
The Nuclex.Support assembly contains several rectangle packing algorithms in the Nuclex.Support.Packing namespace offering various levels of compromises between space efficiency and runtime performance. All of these algorithms have been implemented as classes with a common interface, so, once implemented, you can easily switch forth and back between different algorithms to find the one best suites for your purpose.
Currently, there are three different packing algorithms for you to choose:
The Simple Packer This packer is optimized for runtime performance. It does sacrifice a bit of space, but the time needed to find a position for a new rectangle is O(1). In english, that means it doesn’t take longer to search for a suitable position, no matter how many rectangles have been added to the packing area. 

The Cygon Packer Named after your humble webmaster, this algorithm is highly efficient in its space usage and offers reasonable performance. It will never exceed O(n) time but generally achieves almost O(1) on average. English translation: Search time is usually constant and guaranteed to never exceed a linear increase (so when you’ve got 99 rectangles already packed and add the 100th one, it guarantees that search time will at most be 1% longer than the time taken for the previous rectangle). 

The Arevalo Packer Named after Javier Arevalo, who posted his implementation of this packer on flipcode back in the golden times. This algorithm offers very good space efficiency and runs in slightly less then O(n) time. Just to be consistent, in english, that means, the time needed to find a suitable place for a new rectangle will only increase linearly. 
You can download the C# version of the algorithms from here.
The usage is very simple :
Point PP; var ARP = new ArevaloRectanglePacker(200, 200); if(!ARP.TryPack(Width, Height, out PP)) { // Do something } else { // Do something else }C# Rectangle Packing,
Nice. I have translated & improved the Arevalo packer to Python and D:
http://www.fantascienza.net/leonardo/so/index.html#rectpack
Very nice!
I use your code to create spritesheets. Do you know if soritng the images by height/width or something like this improves the packrate ?
Hi!
I’m beginer developer and I need some help!
I would like to use this code, but I don’t know to implement it.
Can somebody provide simple code/solution of using this engine(one form, button, list of input rectangles, and panel, so when button is clicked, I would be able to see sort rectangles in panel)? This would really help me!!
Thanks anyway!!!
Hi! Very nice!
I’m beginer developer, and I need some help
I would like to use this engine in my code, but I don’t know how to implement it. So, if somebody can provide simple solution(one form, one button, one list with 10 rectangles, and one panel, so when i click on the button i get packed/sorted my rectangles in panel) I would be grateful!!!
p.s. Can this code be used in win applications, not just for game developing?
Thanks anyway!!!
I came up with a fast algorithm that does the same thing – packing rectangles of varying sizes into an enclosing rectangle of minimum size. I wrote it in C#. Also did a web site that graphically shows step by step how the algorithm works out the best enclosing algorithm. It’s at
http://www.codeproject.com/KB/aspnet/rectanglepacker.aspx