title: Return union of all rectangles
body: <p>I'm kinda new to Java and have a problem I just can't wrap my head around.</p>
  
  <ul>
  <li><strong>union (Rectangle ... rectangles)</strong> should return the rectangle given by the
  Union of all rectangles. If rectangles is empty, return null.</li>
  </ul>
  
  <p>I've created a helper-Method to compute the union of 2 Rectangles and then somehow tried to integrate it into the union-Method with no success. I kinda have to do the same for the <strong>intersection</strong> of 2 Rectangles but also can't get it done.</p>
  
  <p>Could you guys give me some help? Below is my code.
  </p>
  
  <pre><code>public class Rectangle {
      int x, y, width, height;
  
      public Rectangle(int xInput, int yInput, int widthInput, int heightInput) {
          if (xInput &lt;= 0 || yInput &lt;= 0 || widthInput &lt;= 0 || heightInput &lt;= 0) {
              return;
          }
          this.x = xInput;
          this.y = yInput;
          this.width = widthInput;
          this.height = heightInput;
  
      }
  
      public static Rectangle union(Rectangle... rectangles) {
          Rectangle s = new Rectangle(0, 0, 0, 0);
          if (rectangles.length != 0) {
              for (Rectangle r : rectangles) {
                  s = unionOfTwo(s, r);
              }
              return s;
          } else {
              return null;
          }
  
      }
  
       public static Rectangle unionOfTwo(Rectangle rec1, Rectangle rec2) {
  
          int x1 = Utils.min(rec1.x, rec2.x);
          int x2 = Utils.max(rec1.x + rec1.width, rec2.x + rec2.width) - x1;
          int y1 = Utils.min(rec1.y, rec2.y);
          int y2 = Utils.max(rec1.y + rec1.height, rec2.y + rec2.height) - y1;
          return new Rectangle(x1, y1, x2, y2);
      }
  }
  </code></pre>