A class that fits subrectangles into a power-of-2 rectangle(C) Copyright 2000-2002 by Javier ArevaloThis code is free to use and modify for all purposesYou have a bunch of rectangular pieces. You need to arrange them in arectangular surface so that they don't overlap, keeping the total area of therectangle as small as possible. This is fairly common when arranging charactersin a bitmapped font, lightmaps for a 3D engine, and I guess other situations aswell.The idea of this algorithm is that, as we add rectangles, we can pre-select"interesting" places where we can try to add the next rectangles. For optimalresults, the rectangles should be added in order. I initially tried using areaas a sorting criteria, but it didn't work well with very tall or very flatrectangles. I then tried using the longest dimension as a selector, and itworked much better. So much for intuition...These "interesting" places are just to the right and just below the currentlyadded rectangle. The first rectangle, obviously, goes at the top left, the nextone would go either to the right or below this one, and so on. It is a weird wayto do it, but it seems to work very nicely.The way we search here is fairly brute-force, the fact being that for most off-line purposes the performance seems more than adequate. I have generated ajapanese font with around 8500 characters and all the time was spent generatingthe bitmaps.Also, for all we care, we could grow the parent rectangle in a different waythan power of two. It just happens that power of 2 is very convenient forgraphics hardware textures.I'd be interested in hearing of other approaches to this problem. Make sureto post them on http://www.flipcode.comSee alsohttp://www.flipcode.com/archives/Rectangle_Placement.shtmlhttp://kossovsky.net/index.php/2009/07/cshar-rectangle-packing