title: Automatically Merging Ajacent Rectangles
body: <p>I've been making a top-down shooter game in XNA that requires rectangular collision for the map. </p>
  
  <p>The collision walls for a map is stored in a text file in the format of:<code>rect[0,0,1024,8]</code></p>
  
  <p>The values correspond to defining a rectangle (x, y, width, height).</p>
  
  <p>I've been thinking that I could write a separate application that can illiterate through the data of the map image, find out the pixels that are black (or any color of the wall) and make rectangles there. Basically, this program will generate the rectangles required for the collision. Ideally, it would be pixel perfect, which would require something like a thousand rectangles each 1 pixel wide that covers all the walls.</p>
  
  <p>Is there a possible way to detect which of these rectangles (or squares I should say) are adjacent to one another, then connect them into the a bigger (but still covering the same area) rectangle?</p>
  
  <p>EG. Lets say I have a wall that is 10 by 2. The program would generate 20 different rectangles, each 1 pixel high. How would I efficiently detect that these rectangles are adjacent and automatically make a 10 by 2 rectangle covering the whole wall instead of having 20 different little pixel rectangles?</p>
  
  <p><strong>EDIT:</strong> I've worked out a solution that fits my purposes, for future reference, my code is below:</p>
  
  <pre><code>//map is a bitmap, horizontalCollisions and collisions are List&lt;Rectangle&gt;s
  for (int y = 0; y &lt; map.Height; y++) //loop through pixels
          {
              for (int x = 0; x &lt; map.Width; x++)
              {
                  if (map.GetPixel(x, y).Name == "ff000000") //wall color
                  {
                      int i = 1;
                      while (map.GetPixel(x + i, y).Name == "ff000000")
                      {
                          if (i != map.Width - x)
                          {
                              i++;
                          }
                          if (i == map.Width - x)
                          {
                              break;
                          }
                      }
                      Rectangle r = new Rectangle(x, y, i, 1);//create and add
                      x += i - 1;
                      horizontalCollisions.Add(r);
                  }
              }
          }
          for (int j = 0; j &lt; horizontalCollisions.Count; j++)
          {
              int i = 1;
              Rectangle current = horizontalCollisions[j];
              Rectangle r = new Rectangle(current.X, current.Y + 1, current.Width, 1);
              while(horizontalCollisions.Contains(r))
              {
                  i++;
                  horizontalCollisions.Remove(r);
                  r = new Rectangle(current.X, current.Y + i, current.Width, 1);
              }
              Rectangle add = new Rectangle(current.X, current.Y, current.Width, i);
              collisions.Add(add);
          }
  
              //collisions now has all the rectangles
  </code></pre>
  
  <p>Basically, it will loop through the pixel data horizontally. When it encounters a wall pixel, it will stop the counter and (using a while loop) move the counter towards the right, one by one until it hits a non-wall pixel. Then, it will create a rectangle of that width, and continue on. After this process, there will be a big list of rectangles, each 1px tall. Basically, a bunch of horizontal lines. The next loop will run through the horizontal lines, and using the same process as above, it will find out of there are any rectangles with the same X value and the same Width value under it (y+1). This will keep incrementing until there are none, in which one big rectangle will be created, and the used rectangles are deleted from the List. The final resulting list contains all the rectangles that will make up all the black pixels on the image (pretty efficiently, I think).</p>