title: Packing rectangles in a rectangle, generate grids coordinates
body: <p>Looking to generate some randoms grids of rectangles in a bigger rectangle. </p>
  
  <p>This looks like a fairly easy problem, but it's not, seeking for advises here. </p>
  
  <p>This is <a href="https://en.wikipedia.org/wiki/Packing_problems" rel="nofollow noreferrer">not a packing problem</a>, since the inner rectangles can have any width and height.</p>
  
  <p>But the amount of rectangles aren't always the same.</p>
  
  <p>I have some results already with different kinds of loops, but none are really efficient.</p>
  
  <p>For example, with <strong>15 rectangles</strong>, a possible way to represent them could be:</p>
  
  <pre><code>  O          10                                            50         60
    +----------+---------------------------------------------+----------+-------+
    |          |                                             |          |       |
    |          |                                             |          |       |
    |          |                                             |          |       |
  5 +----------+---------------------------------------------+----------+-------+
    |          |                                             |                  |
    |          |                                             |                  |
    |          |                                             |                  |
    |          |                                             |                  |
    |          |                                             |                  |
    |          |                                             |                  |
  15+----------+---------------------------------------------+----------+-------+
    |          |                                             |          |       |
    |          |                                             |          |       |
    |          |                                             |          |       |
    +----------+---------------------------------------------+----------+-------+
    |          |                                             |          |       |
    |          |                                             |          |       |
    |          |                ↓                            |          |       |
    +----------+---------------------------------------------+----------+-------+
  </code></pre>
  
  <p>The coordinates would then be something like an array of <strong>[x,y]</strong> point of the top left corner (<strong>+</strong>) of each inner squares:</p>
  
  <pre><code>[[0,0],[10,0],[50,0],[60,0],[5,0],[5,10],[5,50], ...]
  </code></pre>
  
  <p>Or even better an array of <strong>[x,y,w,h]</strong> values (<em>Top left x, top left y, width, height</em>)</p>
  
  <pre><code>[[0,0,10,5],[10,0,40,5],[50,0,10,5],[60,0,10,5],[5,0,10,10],[5,10,40,10],[5,50,20,10], ...]
  </code></pre>
  
  <hr>
  
  <p>But the goal is to make a function that generate coordinates for any amount of inner squares:</p>
  
  <p>For example, with <strong>14 rectangles</strong>, a possible way to represent them could be:  </p>
  
  <pre><code>  +----------+----------------------------------+---------------------+-------+
    |          |                                  |                     |       |
    |          |                                  |                     |       |
    |          |                                  |                     |       |
    |          |                                  |                     |       |
    |          |                                  |                     |       |
    +----------+----------------------------------+----------+----------+-------+
    |          |                                             |          |       |
    |          |                                             |          |       |
    +----------+--------+------------------------------------+----------+-------+
    |                   |                                    |          |       |
    |                   |                                    |          |       |
    +-------------------+-----------+------------------------+----------+-------+
    |                               |                                           |
    |                               |                                           |
    |                               |                                           |
    |                               |                                           |
    +-------------------------------+-----------------------------------+-------+
  </code></pre>
  
  <p>Related links:</p>
  
  <p><a href="https://stackoverflow.com/questions/263932/packing-rectangular-image-data-into-a-square-texture">Packing rectangular image data into a square texture</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1213394/what-algorithm-can-be-used-for-packing-rectangles-of-different-sizes-into-the-sm">What algorithm can be used for packing rectangles of different sizes into the smallest rectangle possible in a fairly optimal way?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/14271261/how-to-arrange-n-rectangles-to-cover-minimum-area">How to arrange N rectangles to cover minimum area</a></p>