{"id":2456,"date":"2011-09-25T02:17:22","date_gmt":"2011-09-25T00:17:22","guid":{"rendered":"https:\/\/iguanademos.com\/Jare\/wp\/?page_id=2456"},"modified":"2011-09-25T02:30:15","modified_gmt":"2011-09-25T00:30:15","slug":"rectangle-packing-code","status":"publish","type":"page","link":"https:\/\/iguanademos.com\/Jare\/wp\/?page_id=2456","title":{"rendered":"Rectangle Packing Code"},"content":{"rendered":"<p>(Note: this article first appeared as a <A HREF=\"http:\/\/www.flipcode.com\/cgi-bin\/msg.cgi?showThread=COTD-RectPlacement&#038;forum=cotd&#038;id=-1\" TARGET=\"_blank\">Tip Of The Day<\/A> in <A HREF=\"http:\/\/www.flipcode.com\" TARGET=\"_blank\">Flipcode<\/A>. The C++ to HTML formating comes from Kurt&#8217;s internal tools)<\/p>\n<p>You have a bunch of rectangular pieces. You need to arrange them in a rectangular surface so that they don&#8217;t overlap, keeping the total area of the rectangle as small as possible. This is fairly common when arranging characters in a bitmapped font, lightmaps for a 3D engine, and I guess other situations as well.<\/p>\n<p>The idea of this algorithm is that, as we add rectangles, we can pre-select &#8220;interesting&#8221; places where we can try to add the next rectangles. For optimal results, the rectangles should be added in order. I initially tried using area as a sorting criteria, but it didn&#8217;t work well with very tall or very flat rectangles. I then tried using the longest dimension as a selector, and it worked much better. So much for intuition&#8230;<\/p>\n<p>These &#8220;interesting&#8221; places are just to the right and just below the currently added rectangle. The first rectangle, obviously, goes at the top left, the next one would go either to the right or below this one, and so on. It is a weird way to do it, but it seems to work very nicely.<\/p>\n<p>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 a japanese font with around 8500 characters and all the time was spent generating the bitmaps.<\/p>\n<p>Also, for all we care, we could grow the parent rectangle in a different way than power of two. It just happens that power of 2 is very convenient for graphics hardware textures.<\/p>\n<p>I&#8217;d be interested in hearing of other approaches to this problem. Make sure to post them on <A href=\"http:\/\/www.flipcode.com\">http:\/\/www.flipcode.com<\/A>.<\/p>\n<p>File: RectPlacement.h<br \/>\n<TABLE cellSpacing=0 cellPadding=10 width=\"100%\" bgColor=#ffffff border=1><TBODY><TR><TD width=\"100%\" bgColor=#ffffff><\/p>\n<pre><FONT face=\"Courier, Courier New\" color=#000000 size=2><FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : RectPlacement.h<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : A class that allocates subrectangles into power-of-2 rectangles<\/FONT>\r\n<FONT color=#007f00>\/\/               (C) Copyright 2000-2002 by Javier Arevalo<\/FONT>\r\n<FONT color=#007f00>\/\/               This code is free to use and modify for all purposes<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n\r\n<FONT color=#0000ff>#ifndef<\/FONT> _RECT_PLACEMENT_H_\r\n<FONT color=#0000ff>#define<\/FONT> _RECT_PLACEMENT_H_\r\n\r\n\r\n<FONT color=#0000ff>#include<\/FONT> &lt;vector&gt;\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n\r\n<FONT color=#0000ff>class<\/FONT> CRectPlacement\r\n{\r\n  <FONT color=#0000ff>public<\/FONT>:\r\n\r\n    <FONT color=#007f00>\/\/ Helper classes<\/FONT>\r\n    <FONT color=#0000ff>struct<\/FONT> TPos\r\n    {\r\n      <FONT color=#0000ff>int<\/FONT> x, y;\r\n\r\n      TPos() { }\r\n      TPos(<FONT color=#0000ff>int<\/FONT> _x, <FONT color=#0000ff>int<\/FONT> _y): x(_x), y(_y) { }\r\n\r\n      <FONT color=#0000ff>bool<\/FONT> <FONT color=#0000ff>operator<\/FONT> ==(<FONT color=#0000ff>const<\/FONT> TPos &amp;p) <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> x == p.x &amp;&amp; y == p.y; }\r\n    };\r\n\r\n    <FONT color=#0000ff>struct<\/FONT> TRect: <FONT color=#0000ff>public<\/FONT> TPos\r\n    {\r\n      <FONT color=#0000ff>int<\/FONT> w, h;\r\n\r\n      TRect() { }\r\n      TRect(<FONT color=#0000ff>int<\/FONT> _x, <FONT color=#0000ff>int<\/FONT> _y, <FONT color=#0000ff>int<\/FONT> _w, <FONT color=#0000ff>int<\/FONT> _h): TPos(_x, _y), w(_w &gt; 0? _w : 0), h(_h &gt; 0? _h : 0) { }\r\n\r\n      <FONT color=#0000ff>bool<\/FONT> Contains   (<FONT color=#0000ff>const<\/FONT> TPos &amp;p)  <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> (p.x &gt;= x &amp;&amp; p.y &gt;= y &amp;&amp;\r\n                                                       p.x &lt; (x+w) &amp;&amp; p.y &lt; (y+h)); }\r\n      <FONT color=#0000ff>bool<\/FONT> Contains   (<FONT color=#0000ff>const<\/FONT> TRect &amp;r) <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> (r.x &gt;= x &amp;&amp; r.y &gt;= y &amp;&amp;\r\n                                                       (r.x+r.w) &lt;= (x+w) &amp;&amp; (r.y+r.h) &lt;= (y+h)); }\r\n      <FONT color=#0000ff>bool<\/FONT> Intersects (<FONT color=#0000ff>const<\/FONT> TRect &amp;r) <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> w &gt; 0 &amp;&amp; h &gt; 0 &amp;&amp; r.w &gt; 0 &amp;&amp; r.h &gt; 0 &amp;&amp;\r\n                                                      ((r.x+r.w) &gt; x &amp;&amp; r.x &lt; (x+w) &amp;&amp;\r\n                                                       (r.y+r.h) &gt; y &amp;&amp; r.y &lt; (y+h)); }\r\n\r\n    <FONT color=#007f00>\/\/  Greater rect area. Not as good as the next heuristic<\/FONT>\r\n    <FONT color=#007f00>\/\/  static bool Greater(const TRect &amp;a, const TRect &amp;b) { return a.w*a.h &gt; b.w*b.h; }<\/FONT>\r\n\r\n      <FONT color=#007f00>\/\/ Greater size in at least one dim.<\/FONT>\r\n      <FONT color=#0000ff>static<\/FONT> <FONT color=#0000ff>bool<\/FONT> Greater(<FONT color=#0000ff>const<\/FONT> TRect &amp;a, <FONT color=#0000ff>const<\/FONT> TRect &amp;b) { <FONT color=#0000ff>return<\/FONT> (a.w &gt; b.w &amp;&amp; a.w &gt; b.h) ||\r\n                                                                   (a.h &gt; b.w &amp;&amp; a.h &gt; b.h); }\r\n    };\r\n\r\n    <FONT color=#007f00>\/\/ ---------------------<\/FONT>\r\n\r\n    <FONT color=#0000ff>typedef<\/FONT> std::vector&lt;TPos&gt;  CPosArray;\r\n    <FONT color=#0000ff>typedef<\/FONT> std::vector&lt;TRect&gt; CRectArray;\r\n\r\n    <FONT color=#007f00>\/\/ ---------------------<\/FONT>\r\n\r\n    CRectPlacement()                    { Init(); }\r\n    ~CRectPlacement()                   { End(); }\r\n\r\n    <FONT color=#0000ff>void<\/FONT>      Init    (<FONT color=#0000ff>int<\/FONT> w = 1, <FONT color=#0000ff>int<\/FONT> h = 1);\r\n    <FONT color=#0000ff>void<\/FONT>      End     ();\r\n    <FONT color=#0000ff>bool<\/FONT>      IsOk    ()                      <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> m_size.w &gt; 0; }\r\n\r\n    <FONT color=#0000ff>int<\/FONT>       GetW    ()                      <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> m_size.w; }\r\n    <FONT color=#0000ff>int<\/FONT>       GetH    ()                      <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> m_size.h; }\r\n    <FONT color=#0000ff>long<\/FONT>      GetArea ()                      <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> m_area; }\r\n    <FONT color=#0000ff>long<\/FONT>      GetTotalArea ()                 <FONT color=#0000ff>const<\/FONT> { <FONT color=#0000ff>return<\/FONT> m_size.w*m_size.h; }\r\n\r\n    <FONT color=#0000ff>bool<\/FONT> AddAtEmptySpotAutoGrow (TRect *pRect, <FONT color=#0000ff>int<\/FONT> maxW, <FONT color=#0000ff>int<\/FONT> maxH);\r\n\r\n  <FONT color=#0000ff>private<\/FONT>:\r\n    TRect       m_size;\r\n    CRectArray  m_vRects;\r\n    CPosArray   m_vPositions;\r\n    <FONT color=#0000ff>long<\/FONT>        m_area;\r\n\r\n    <FONT color=#007f00>\/\/ ---------------------<\/FONT>\r\n\r\n    <FONT color=#0000ff>bool<\/FONT> IsFree                 (<FONT color=#0000ff>const<\/FONT> TRect &amp;r) <FONT color=#0000ff>const<\/FONT>;\r\n    <FONT color=#0000ff>void<\/FONT> AddPosition            (<FONT color=#0000ff>const<\/FONT> TPos &amp;p);\r\n    <FONT color=#0000ff>void<\/FONT> AddRect                (<FONT color=#0000ff>const<\/FONT> TRect &amp;r);\r\n    <FONT color=#0000ff>bool<\/FONT> AddAtEmptySpot         (TRect &amp;r);\r\n};\r\n\r\n\r\n<FONT color=#0000ff>#endif<\/FONT> <FONT color=#007f00>\/\/_RECT_PLACEMENT_H_<\/FONT>\r\n <\/FONT><\/pre>\n<p><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n<p>File: RectPlacement.cpp<br \/>\n<TABLE cellSpacing=0 cellPadding=10 width=\"100%\" bgColor=#ffffff border=1><TBODY><TR><TD width=\"100%\" bgColor=#ffffff><\/p>\n<pre><FONT face=\"Courier, Courier New\" color=#000000 size=2><FONT color=#007f00>\/\/ ----------------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : RectPlacement.cpp<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : A class that fits subrectangles into a power-of-2 rectangle<\/FONT>\r\n<FONT color=#007f00>\/\/               (C) Copyright 2000-2002 by Javier Arevalo<\/FONT>\r\n<FONT color=#007f00>\/\/               This code is free to use and modify for all purposes<\/FONT>\r\n<FONT color=#007f00>\/\/ ----------------------------------------------------------------------------------------<\/FONT>\r\n\r\n<FONT color=#007f00>\/*\r\n  You have a bunch of rectangular pieces. You need to arrange them in a \r\n  rectangular surface so that they don't overlap, keeping the total area of the \r\n  rectangle as small as possible. This is fairly common when arranging characters \r\n  in a bitmapped font, lightmaps for a 3D engine, and I guess other situations as \r\n  well.\r\n\r\n  The idea of this algorithm is that, as we add rectangles, we can pre-select \r\n  \"interesting\" places where we can try to add the next rectangles. For optimal \r\n  results, the rectangles should be added in order. I initially tried using area \r\n  as a sorting criteria, but it didn't work well with very tall or very flat \r\n  rectangles. I then tried using the longest dimension as a selector, and it \r\n  worked much better. So much for intuition...\r\n\r\n  These \"interesting\" places are just to the right and just below the currently \r\n  added rectangle. The first rectangle, obviously, goes at the top left, the next \r\n  one would go either to the right or below this one, and so on. It is a weird way \r\n  to do it, but it seems to work very nicely.\r\n\r\n  The way we search here is fairly brute-force, the fact being that for most off-\r\n  line purposes the performance seems more than adequate. I have generated a \r\n  japanese font with around 8500 characters and all the time was spent generating \r\n  the bitmaps.\r\n\r\n  Also, for all we care, we could grow the parent rectangle in a different way \r\n  than power of two. It just happens that power of 2 is very convenient for \r\n  graphics hardware textures.\r\n\r\n  I'd be interested in hearing of other approaches to this problem. Make sure\r\n  to post them on http:\/\/www.flipcode.com\r\n*\/<\/FONT>\r\n\r\n<FONT color=#0000ff>#include<\/FONT> \"RectPlacement.h\"\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : <\/FONT>\r\n<FONT color=#007f00>\/\/ Description : <\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#0000ff>void<\/FONT> CRectPlacement::Init    (<FONT color=#0000ff>int<\/FONT> w, <FONT color=#0000ff>int<\/FONT> h)\r\n{\r\n  End();\r\n  m_size = TRect(0, 0, w, h);\r\n  m_vPositions.push_back(TPos(0,0));\r\n  m_area = 0;\r\n}\r\n\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : <\/FONT>\r\n<FONT color=#007f00>\/\/ Description : <\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#0000ff>void<\/FONT> CRectPlacement::End     ()\r\n{\r\n  m_vPositions.clear();\r\n  m_vRects.clear();\r\n  m_size.w = 0;\r\n}\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : IsFree<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : Check if the given rectangle is partially or totally used<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#0000ff>bool<\/FONT> CRectPlacement::IsFree (<FONT color=#0000ff>const<\/FONT> TRect &amp;r) <FONT color=#0000ff>const<\/FONT>\r\n{\r\n  <FONT color=#0000ff>if<\/FONT> (!m_size.Contains(r))\r\n    <FONT color=#0000ff>return<\/FONT> <FONT color=#0000ff>false<\/FONT>;\r\n  <FONT color=#0000ff>for<\/FONT> (CRectArray::const_iterator it = m_vRects.begin();\r\n       it != m_vRects.end();\r\n       ++it)\r\n    <FONT color=#0000ff>if<\/FONT> (it-&gt;Intersects(r))\r\n      <FONT color=#0000ff>return<\/FONT> <FONT color=#0000ff>false<\/FONT>;\r\n  <FONT color=#0000ff>return<\/FONT> <FONT color=#0000ff>true<\/FONT>;\r\n}\r\n\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : AddPosition<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : Add new anchor point<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#0000ff>void<\/FONT> CRectPlacement::AddPosition    (<FONT color=#0000ff>const<\/FONT> TPos &amp;p)\r\n{\r\n  <FONT color=#007f00>\/\/ Try to insert anchor as close as possible to the top left corner<\/FONT>\r\n  <FONT color=#007f00>\/\/ So it will be tried first<\/FONT>\r\n  <FONT color=#0000ff>bool<\/FONT> bFound = <FONT color=#0000ff>false<\/FONT>;\r\n  CPosArray::iterator it;\r\n  <FONT color=#0000ff>for<\/FONT> (it = m_vPositions.begin();\r\n       !bFound &amp;&amp; it != m_vPositions.end();\r\n       ++it)\r\n  {\r\n    <FONT color=#0000ff>if<\/FONT> (p.x+p.y &lt; it-&gt;x+it-&gt;y)\r\n      bFound = <FONT color=#0000ff>true<\/FONT>;\r\n  }\r\n  <FONT color=#0000ff>if<\/FONT> (bFound)\r\n    m_vPositions.insert(it, p);\r\n  <FONT color=#0000ff>else<\/FONT>\r\n\r\n    m_vPositions.push_back(p);\r\n}\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : AddRect<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : Add the given rect and updates anchor points<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#0000ff>void<\/FONT> CRectPlacement::AddRect  (<FONT color=#0000ff>const<\/FONT> TRect &amp;r)\r\n{\r\n  m_vRects.push_back(r);\r\n  m_area += r.w*r.h;\r\n\r\n  <FONT color=#007f00>\/\/ Add two new anchor points<\/FONT>\r\n  AddPosition(TPos(r.x, r.y+r.h));\r\n  AddPosition(TPos(r.x+r.w, r.y));\r\n}\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : AddAtEmptySpot<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : Add the given rectangle<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#0000ff>bool<\/FONT> CRectPlacement::AddAtEmptySpot   (TRect &amp;r)\r\n{\r\n  <FONT color=#007f00>\/\/ Find a valid spot among available anchors.<\/FONT>\r\n\r\n  <FONT color=#0000ff>bool<\/FONT> bFound = <FONT color=#0000ff>false<\/FONT>;\r\n  CPosArray::iterator it;\r\n  <FONT color=#0000ff>for<\/FONT> (it = m_vPositions.begin();\r\n       !bFound &amp;&amp; it != m_vPositions.end();\r\n       ++it)\r\n  {\r\n    TRect Rect(it-&gt;x, it-&gt;y, r.w, r.h);\r\n\r\n    <FONT color=#0000ff>if<\/FONT> (IsFree(Rect))\r\n    {\r\n      r = Rect;\r\n      bFound = <FONT color=#0000ff>true<\/FONT>;\r\n      <FONT color=#0000ff>break<\/FONT>; <FONT color=#007f00>\/\/ Don't let the loop increase the iterator.<\/FONT>\r\n    }\r\n  }\r\n  <FONT color=#0000ff>if<\/FONT> (bFound)\r\n  {\r\n    <FONT color=#007f00>\/\/ Remove the used anchor point<\/FONT>\r\n    m_vPositions.erase(it);\r\n\r\n    <FONT color=#007f00>\/\/ Sometimes, anchors end up displaced from the optimal position<\/FONT>\r\n    <FONT color=#007f00>\/\/ due to irregular sizes of the subrects.<\/FONT>\r\n    <FONT color=#007f00>\/\/ So, try to adjut it up &amp; left as much as possible.<\/FONT>\r\n    <FONT color=#0000ff>for<\/FONT> (<FONT color=#0000ff>int<\/FONT> x = 1; x &lt;= r.x; x++)\r\n      <FONT color=#0000ff>if<\/FONT> (!IsFree(TRect(r.x - x, r.y, r.w, r.h)))\r\n        <FONT color=#0000ff>break<\/FONT>;\r\n    <FONT color=#0000ff>for<\/FONT> (<FONT color=#0000ff>int<\/FONT> y = 1; y &lt;= r.y; y++)\r\n      <FONT color=#0000ff>if<\/FONT> (!IsFree(TRect(r.x, r.y - y, r.w, r.h)))\r\n        <FONT color=#0000ff>break<\/FONT>;\r\n    <FONT color=#0000ff>if<\/FONT> (y &gt; x)\r\n      r.y -= y-1;\r\n    <FONT color=#0000ff>else<\/FONT>\r\n\r\n      r.x -= x-1;\r\n    AddRect(r);\r\n  }\r\n  <FONT color=#0000ff>return<\/FONT> bFound;\r\n}\r\n\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : AddAtEmptySpotAutoGrow<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : Add a rectangle of the given size, growing our area if needed<\/FONT>\r\n<FONT color=#007f00>\/\/               Area grows only until the max given.<\/FONT>\r\n<FONT color=#007f00>\/\/               Returns the placement of the rect in the rect's x,y coords<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n\r\n<FONT color=#0000ff>bool<\/FONT> CRectPlacement::AddAtEmptySpotAutoGrow   (TRect *pRect, <FONT color=#0000ff>int<\/FONT> maxW, <FONT color=#0000ff>int<\/FONT> maxH)\r\n{\r\n  <FONT color=#0000ff>if<\/FONT> (pRect-&gt;w &lt;= 0)\r\n    <FONT color=#0000ff>return<\/FONT> <FONT color=#0000ff>true<\/FONT>;\r\n\r\n  <FONT color=#0000ff>int<\/FONT> orgW = m_size.w;\r\n  <FONT color=#0000ff>int<\/FONT> orgH = m_size.h;\r\n\r\n  <FONT color=#007f00>\/\/ Try to add it in the existing space<\/FONT>\r\n  <FONT color=#0000ff>while<\/FONT> (!AddAtEmptySpot(*pRect))\r\n  {\r\n    <FONT color=#0000ff>int<\/FONT> pw = m_size.w;\r\n    <FONT color=#0000ff>int<\/FONT> ph = m_size.h;\r\n\r\n    <FONT color=#007f00>\/\/ Sanity check - if area is complete.<\/FONT>\r\n    <FONT color=#0000ff>if<\/FONT> (pw &gt;= maxW &amp;&amp; ph &gt;= maxH)\r\n    {\r\n      m_size.w = orgW;\r\n      m_size.h = orgH;\r\n      <FONT color=#0000ff>return<\/FONT> <FONT color=#0000ff>false<\/FONT>;\r\n    }\r\n\r\n    <FONT color=#007f00>\/\/ Try growing the smallest dim<\/FONT>\r\n    <FONT color=#0000ff>if<\/FONT> (pw &lt; maxW &amp;&amp; (pw &lt; ph || ((pw == ph) &amp;&amp; (pRect-&gt;w &gt;= pRect-&gt;h))))\r\n      m_size.w = pw*2;\r\n    <FONT color=#0000ff>else<\/FONT>\r\n      m_size.h = ph*2;\r\n    <FONT color=#0000ff>if<\/FONT> (AddAtEmptySpot(*pRect))\r\n      <FONT color=#0000ff>break<\/FONT>;\r\n\r\n    <FONT color=#007f00>\/\/ Try growing the other dim instead<\/FONT>\r\n    <FONT color=#0000ff>if<\/FONT> (pw != m_size.w)\r\n    {\r\n      m_size.w = pw;\r\n      <FONT color=#0000ff>if<\/FONT> (ph &lt; maxW)\r\n        m_size.h = ph*2;\r\n    }\r\n    <FONT color=#0000ff>else<\/FONT>\r\n    {\r\n      m_size.h = ph;\r\n      <FONT color=#0000ff>if<\/FONT> (pw &lt; maxW)\r\n        m_size.w = pw*2;\r\n    }\r\n\r\n    <FONT color=#0000ff>if<\/FONT> (pw != m_size.w || ph != m_size.h)\r\n      <FONT color=#0000ff>if<\/FONT> (AddAtEmptySpot(*pRect))\r\n        <FONT color=#0000ff>break<\/FONT>;\r\n\r\n    <FONT color=#007f00>\/\/ Grow both if possible, and reloop.<\/FONT>\r\n    m_size.w = pw;\r\n    m_size.h = ph;\r\n    <FONT color=#0000ff>if<\/FONT> (pw &lt; maxW)\r\n      m_size.w = pw*2;\r\n    <FONT color=#0000ff>if<\/FONT> (ph &lt; maxH)\r\n      m_size.h = ph*2;\r\n  }\r\n  <FONT color=#0000ff>return<\/FONT> <FONT color=#0000ff>true<\/FONT>;\r\n}\r\n <\/FONT><\/pre>\n<p><\/TD><\/TR><\/TBODY><\/TABLE><br \/>\nFile: Test.cpp<br \/>\n<TABLE cellSpacing=0 cellPadding=10 width=\"100%\" bgColor=#ffffff border=1><TBODY><TR><TD width=\"100%\" bgColor=#ffffff><\/p>\n<pre><FONT face=\"Courier, Courier New\" color=#000000 size=2><FONT color=#007f00>\/\/ ----------------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : Test.cpp<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : Test the RectPlacement class<\/FONT>\r\n<FONT color=#007f00>\/\/               (C) Copyright 2000-2002 by Javier Arevalo<\/FONT>\r\n<FONT color=#007f00>\/\/               This code is free to use and modify for all purposes<\/FONT>\r\n<FONT color=#007f00>\/\/ ----------------------------------------------------------------------------------------<\/FONT>\r\n\r\n<FONT color=#0000ff>#include<\/FONT> &lt;stdio.h&gt;\r\n<FONT color=#0000ff>#include<\/FONT> &lt;algorithm&gt;\r\n\r\n<FONT color=#0000ff>#include<\/FONT> \"RectPlacement.h\"\r\n\r\n<FONT color=#0000ff>#define<\/FONT> MAX_TEXTURE_W 256\r\n<FONT color=#0000ff>#define<\/FONT> MAX_TEXTURE_H 256\r\n\r\n<FONT color=#0000ff>#define<\/FONT> NUM_SUBRECTS  500\r\n<FONT color=#0000ff>#define<\/FONT> MIN_SUBRECT    15\r\n<FONT color=#0000ff>#define<\/FONT> MAX_SUBRECT    50\r\n\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n\r\n<FONT color=#0000ff>struct<\/FONT> TSubRect: <FONT color=#0000ff>public<\/FONT> CRectPlacement::TRect\r\n{\r\n  <FONT color=#0000ff>int<\/FONT> n;      <FONT color=#007f00>\/\/ Original index of this subrect, before sorting<\/FONT>\r\n  <FONT color=#0000ff>int<\/FONT> nTex;   <FONT color=#007f00>\/\/ Texture in which this subrect will be placed.<\/FONT>\r\n\r\n  TSubRect() { }\r\n  TSubRect(<FONT color=#0000ff>int<\/FONT> _w, <FONT color=#0000ff>int<\/FONT> _h, <FONT color=#0000ff>int<\/FONT> _n): TRect(0, 0, _w, _h), n(_n), nTex(0) { }\r\n};\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n\r\n<FONT color=#0000ff>typedef<\/FONT> std::vector&lt;TSubRect&gt;       CSubRectArray;\r\n<FONT color=#0000ff>typedef<\/FONT> std::vector&lt;CRectPlacement&gt; CTextureArray;\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : CreateLetters<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : Here's where you calculate your rectangles,<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n\r\n<FONT color=#0000ff>void<\/FONT> CreateLetters  (CSubRectArray &amp;vecSubRects)\r\n{\r\n  vecSubRects.clear();\r\n\r\n  <FONT color=#007f00>\/\/ Let's just fill this with random stuff<\/FONT>\r\n  <FONT color=#0000ff>for<\/FONT> (<FONT color=#0000ff>int<\/FONT> i = 0; i &lt; NUM_SUBRECTS; i++)\r\n    vecSubRects.push_back(TSubRect((rand() % MAX_SUBRECT) + MIN_SUBRECT,\r\n                                   (rand() % MAX_SUBRECT) + MIN_SUBRECT, i));\r\n}\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : CreateTextures<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : Create array of textures containing all subrects<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#0000ff>void<\/FONT> CreateTextures (CTextureArray &amp;vecTextures, CSubRectArray &amp;vecSubRects, <FONT color=#0000ff>int<\/FONT> maxTexW, <FONT color=#0000ff>int<\/FONT> maxTexH)\r\n{\r\n  <FONT color=#007f00>\/\/ Sort the subRects based on dimensions, larger dimension goes first.<\/FONT>\r\n  std::sort(vecSubRects.begin(), vecSubRects.end(), CRectPlacement::TRect::Greater);\r\n\r\n  <FONT color=#007f00>\/\/ Generate the first texture<\/FONT>\r\n  vecTextures.clear();\r\n  vecTextures.push_back(CRectPlacement());\r\n\r\n  <FONT color=#007f00>\/\/ Add all subrects<\/FONT>\r\n  <FONT color=#0000ff>for<\/FONT> (CSubRectArray::iterator it = vecSubRects.begin();\r\n       it != vecSubRects.end();\r\n       ++it)\r\n  {\r\n    <FONT color=#007f00>\/\/ We make sure we leave one pixel between subrects, so texels don't bleed with bilinear.<\/FONT>\r\n    CRectPlacement::TRect r(0, 0, it-&gt;w+1, it-&gt;h+1);\r\n\r\n    <FONT color=#007f00>\/\/ If an existing texture has actual space<\/FONT>\r\n    <FONT color=#0000ff>bool<\/FONT> bPlaced = <FONT color=#0000ff>false<\/FONT>;\r\n    <FONT color=#0000ff>for<\/FONT> (<FONT color=#0000ff>int<\/FONT> i = 0; !bPlaced &amp;&amp; i &lt; vecTextures.size(); i++)\r\n    {\r\n      bPlaced = vecTextures[i].AddAtEmptySpotAutoGrow(&amp;r, maxTexW, maxTexH);\r\n      <FONT color=#0000ff>if<\/FONT> (bPlaced)\r\n        it-&gt;nTex = i;\r\n    }\r\n\r\n    <FONT color=#007f00>\/\/ Try starting a new texture and fit the rect in there<\/FONT>\r\n    <FONT color=#0000ff>if<\/FONT> (!bPlaced)\r\n    {\r\n      vecTextures.push_back(CRectPlacement());\r\n      bPlaced = vecTextures[vecTextures.size()-1].AddAtEmptySpotAutoGrow(&amp;r, maxTexW, maxTexH);\r\n      <FONT color=#0000ff>if<\/FONT> (bPlaced)\r\n        it-&gt;nTex = vecTextures.size()-1;\r\n      <FONT color=#0000ff>else<\/FONT>\r\n        printf(\"ERROR: Subrect is too big to fit in texture!\", it-&gt;w, it-&gt;h);\r\n    }\r\n\r\n    <FONT color=#007f00>\/\/ If correctly placed in a texture, the coords are returned in r.x and r.y<\/FONT>\r\n    <FONT color=#007f00>\/\/ Store them.<\/FONT>\r\n    <FONT color=#0000ff>if<\/FONT> (bPlaced)\r\n    {\r\n      it-&gt;x = r.x;\r\n      it-&gt;y = r.y;\r\n    }\r\n  }\r\n}\r\n\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#007f00>\/\/ Name        : main<\/FONT>\r\n<FONT color=#007f00>\/\/ Description : heh<\/FONT>\r\n<FONT color=#007f00>\/\/ --------------------------------------------------------------------------------<\/FONT>\r\n<FONT color=#0000ff>int<\/FONT> main()\r\n{\r\n  CTextureArray vecTextures;\r\n  CSubRectArray vecSubRects;\r\n\r\n  CreateLetters(vecSubRects);\r\n  CreateTextures(vecTextures, vecSubRects, MAX_TEXTURE_W, MAX_TEXTURE_H);\r\n\r\n  {\r\n    <FONT color=#0000ff>for<\/FONT> (CSubRectArray::const_iterator it = vecSubRects.begin();\r\n         it != vecSubRects.end();\r\n         ++it)\r\n    {\r\n      printf(\"Subrect %d (originally %d), size %dx%d, goes into texture %d at pos %d,%d\\n\",\r\n             it - vecSubRects.begin(), it-&gt;n, it-&gt;w, it-&gt;h, it-&gt;nTex, it-&gt;x, it-&gt;y);\r\n    }\r\n  }\r\n\r\n  printf(\"Created a total of %d textures\\n\", vecTextures.size());\r\n  {\r\n    <FONT color=#0000ff>for<\/FONT> (CTextureArray::const_iterator it = vecTextures.begin();\r\n         it != vecTextures.end();\r\n         ++it)\r\n    {\r\n      printf(\"  Texture %d, size %dx%d, Coverage %d \/ %d (%d%%)\\n\",\r\n             it - vecTextures.begin(), it-&gt;GetW(), it-&gt;GetH(),\r\n             it-&gt;GetArea(), it-&gt;GetTotalArea(), it-&gt;GetArea()*100\/it-&gt;GetTotalArea());\r\n    }\r\n  }\r\n  <FONT color=#0000ff>return<\/FONT> 0;\r\n}\r\n<\/FONT><\/pre>\n<p><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Note: this article first appeared as a Tip Of The Day in Flipcode. The C++ to HTML formating comes from Kurt&#8217;s internal tools) You have a bunch of rectangular pieces. You need to arrange them in a rectangular surface so &hellip; <a href=\"https:\/\/iguanademos.com\/Jare\/wp\/?page_id=2456\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2432,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2456","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=\/wp\/v2\/pages\/2456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2456"}],"version-history":[{"count":7,"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=\/wp\/v2\/pages\/2456\/revisions"}],"predecessor-version":[{"id":2461,"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=\/wp\/v2\/pages\/2456\/revisions\/2461"}],"up":[{"embeddable":true,"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=\/wp\/v2\/pages\/2432"}],"wp:attachment":[{"href":"https:\/\/iguanademos.com\/Jare\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}