title: Clamp Rectangle inside larger Rectangle
body: <p>Lets say I have an XNA Rectangle with </p>
  
  <pre><code>X = 0, Y = 0, Width = 25, Height = 25.
  </code></pre>
  
  <p>What I want is a method to clamp another rectangle inside, so that the smaller rectangle never 'leaves' the bounds of the larger one.</p>
  
  <p>For example, if I make a new Rectangle with:</p>
  
  <pre><code>X = 23, Y = 20, Width = 10, Height = 10,
  </code></pre>
  
  <p>I want the method to return a Rectangle with: </p>
  
  <pre><code>X = 23, Y = 20, Width = 2, Height = 5.
  </code></pre>
  
  <p>Also, if I make a new Rectangle with: </p>
  
  <pre><code>X = -8, Y = 8, Width = 10, = 10,
  </code></pre>
  
  <p>I want the method to return a Rectangle with:</p>
  
  <pre><code>X = 0, Y = 8, Width = 2, Height = 10.
  </code></pre>
  
  <hr>
  
  <p>In the meanwhile I got the clamping to work. Still, its a bulk of ugly code, so the new question is, how can I simplify this?</p>
  
  <pre><code>if (SelectionRectangle.X &lt; 0)
          {
              SelectionRectangle.Width = SelectionRectangle.Width + SelectionRectangle.X;
              SelectionRectangle.X = 0;
          }
          if (SelectionRectangle.X + SelectionRectangle.Width &gt; tileset.Width)
          {
              if (SelectionRectangle.X &gt; tileset.Width)
              {
                  SelectionRectangle.X = tileset.Width;
                  SelectionRectangle.Width = 0;
              }
              else
              {
                  SelectionRectangle.Width = tileset.Width - SelectionRectangle.X;
              }
          }
          if (SelectionRectangle.Y &lt; 0)
          {
              SelectionRectangle.Height = SelectionRectangle.Height + SelectionRectangle.Y;
              SelectionRectangle.Y = 0;
          }
          if (SelectionRectangle.Y + SelectionRectangle.Height &gt; tileset.Height)
          {
              if (SelectionRectangle.Y &gt; tileset.Height)
              {
                  SelectionRectangle.Y = tileset.Height;
                  SelectionRectangle.Height = 0;
              }
              else
              {
                  SelectionRectangle.Height = tileset.Height - SelectionRectangle.Y;
              }
          }
  </code></pre>