title: Filling a square neatly with smaller squares
body: <p>I am developing a game in which the world is divided up into units called sectors, which the player passes through. Each of these sectors is a different size, and each is divided up into units called SubSectors, which can be different sizes and fill the sector completely. I made a quick drawing of how I would like the generation to work:</p>
  
  <p><a href="http://jsydie.com/artlib/misc/tiles.gif" rel="nofollow noreferrer">tiles http://jsydie.com/artlib/misc/tiles.gif</a></p>
  
  <p>EDIT: I am not trying to draw random pictures like this. The SubSector object (represented by the squares above) knows where it's upper-left corner is, and how big it is. I am trying to fit these "squares" onto a grid the size of the parent Sector object.</p>
  
  <p>I am trying to work on the generation for the sectors, but I don't know how to take the next step from here:</p>
  
  <pre><code>package world;
  
  import java.util.ArrayList;
  import java.util.Random;
  
  public class Sector {
  
  private static int size;
  private static ArrayList&lt;SubSector&gt; sub;
  
  public Sector(int par0) {
      size = par0;
      sub = this.generateSubs();
  }
  
  private ArrayList&lt;SubSector&gt; generateSubs() {
      ArrayList&lt;SubSector&gt; out = new ArrayList&lt;SubSector&gt;();
  
      int side = this.size;
      int spaces = side * side;
  
      boolean[][] available = new boolean[side][side];
  
      Random r = new Random();
      int subSide;
      int loc;
      SubSector a;
      boolean anyAvailable = true;
  
      while(anyAvailable) {
          subSide = r.nextInt(1+side)-1;
          System.out.println(subSide);
          a = new SubSector(subSide);
          loc = r.nextInt(4);
          System.out.println(loc);
          out.add(a);
          if(loc == 0) {
              for(int i=0; i&lt;subSide; i++) {
                  for(int j=0; j&lt;subSide; j++) {
                      available[i][j] = false;
                  }
              }
          }
          if(loc == 1) {
              for(int i=subSide; i&gt;0; i--) {
                  for(int j=0; j&lt;subSide; j++) {
                      available[i][j] = false;
                  }
              }
          }
          if(loc == 2) {
              for(int i=subSide; i&gt;0; i--) {
                  for(int j=subSide; j&gt;0; j--) {
                      available[i][j] = false;
                  }
              }
          }
          if(loc == 3) {
              for(int i=0; i&lt;subSide; i++) {
                  for(int j=subSide; j&gt;0; j--) {
                      available[i][j] = false;
                  }
              }
          }
          breakloop:
          for(int i=0; i&lt;side; i++) {
              for(int j=0; j&lt;side; j++) {
                  if(available[i][j] == true) {
                  anyAvailable = true;
                  break breakloop;
                  }
                  else
                      anyAvailable = false;
              }
          }
      }
  
  
      return out;
  }
  }
  </code></pre>
  
  <p>I have no idea how to make it check what size SubSectors are now available to it in the remaining space, then create one and place it in a random location.</p>