title: Rectangle packing in Java
body: <p>I'm trying to implement a rectangle packing algorithm.</p>
  
  <p>I have some rectanges on my input Each rectangle have a heigth and a width(and an index number, so i will be able to identify them when i log them)</p>
  
  <p>I have a matrix (an Integer[][]), and i want to store my rectangles in this matrix. After initialization the matrix cannot be modified, so it has a fixed size.</p>
  
  <p>Let's see and example, where we have two rectangles: </p>
  
  <pre><code>new Rectangle(2,3,1) //height=2, width=3, index=1
  new Rectangle(1,2,2) //height=1, width=1, index=2
  </code></pre>
  
  <p>And let have a 3x3 matrix:</p>
  
  <pre><code>Integer[][] matrix = new Integer[4][3]; //height=4, width=3
  </code></pre>
  
  <p>After i'm done with running the algorithm, i would like to see something like this (If I print the matrix to system.out):</p>
  
  <pre><code>1 1 1
  1 1 1
  2 2 0
  0 0 0
  </code></pre>
  
  <p>The first two row stores the 1st rectangle (it's 2 high and 3 wide), and we find the 2nd rectangle in the third row. The 0s mean there are empty spaces left (we could store some other rectangle there as well)</p>
  
  <p>I have trouble finding a good way for a greedy (or any) algorithm.</p>
  
  <p>I sort the rectangles from biggest to smallest.
  Then i should startsomehow storing the rectangles: if the matrix have enough space, we put it there, if not, we try another cell. we continue on every row and on every cell until we find a place.</p>
  
  <p>Can anyone point me to right direction? A description about a good algorith, pseudo-code, or something i could use to figure my way out.</p>
  
  <p>Thanks,
  Mark</p>