Calculating minimum size of rectangle that contains two smaller ones My Rectangle class contains one vertex (x,y), its width a, and its height b; the opposite vertex will be (x+a,y+b). I had a task to take 2 rectangles and compute the minimal 3rd rectangle to contain 2 smaller ones.  How can I simplify this code?

Rectangle operator+(Rectangle &amp;o){
    double x1 = x, x2 = o.Gx(), y1 = y, y2 = o.Gy(), a1 = a, a2 = o.Ga(), b1 = b, b2 = o.Gb();
    double retx, rety, reta, retb;
    if (x2 &gt; x1){
        retx = x1;
        if (x1 + a1 &lt;= x2)
            reta = a1 + a2 + x2 - (x1 + a1);
        else if (x1 + a1 &gt; x2 + a2)
            reta = a1;

        else
            reta = x2 + a2 - x1;
    }
    else{
        retx = x2;
        if (x1 &lt;= x2 + a2)
            reta = x1 + a1 - x1;
        else if (x2 + a2 &gt; x1 + a1)
            reta = a2;
        else
            reta = a1 + a2 + x1 - (x2 + a2);
    }
    if (x1 == x2 &amp;&amp; a1 &gt; 0 &amp;&amp; a2 &gt; 0){
        retx = x1;
        if (a1 &gt; a2)
            reta = a1;
        else
            reta = a2;
    }

    if (y2 &gt; y1){
        rety = y1;
        if (y1 + b1 &lt;= y2)
            retb = b1 + b2 + y2 - (y1 + b1);
        else if (y1 + b1 &gt; y2 + b2)
            retb = b1;

        else
            retb = y2 + b2 - y1;
    }
    else{
        rety = y2;
        if (y1 &lt;= y2 + b2)
            retb = y1 + b1 - y1;
        else if (y2 + b2 &gt; y1 + b1)
            retb = b2;
        else
            retb = b1 + b2 + y1 - (y2 + b2);
    }
    if (y1 == y2 &amp;&amp; b1 &gt; 0 &amp;&amp; b2 &gt; 0){
        rety = y1;
        if (b1 &gt; b2)
            retb = b1;
        else
            retb = b2;
    }
    return Rectangle(retx,rety,reta,retb);
}
For the moment, I'm assuming a typical graphics system with the origin at the top left. At the very least, the variable names I've used depend upon that to really make any sense. Anyway, I believe the logic you want can be embodied in code something like this (please forgive the sexist variable names):

Rectangle operator+(Rectangle &amp;o) {
    double my_left = std::min(x, x + a);
    double his_left = std::min(o.x, o.x + o.a);
    double left = std::min(my_left, his_left);

    double my_right = std::max(x, x + a);
    double his_right = std::max(o.x, o.x + o.a);
    double right = std::max(my_right, his_right);

    double my_top = std::min(y, y + b);
    double his_top = std::min(o.y, o.y + o.b);
    double top = std::min(my_top, his_top);

    double my_bottom = std::max(y, y + b);
    double his_bottom = std::max(o.y, o.y + o.b);
    double bottom = std::max(my_bottom, his_bottom);

    return Rectangle(left, top, right - left, bottom - top);
}


If you can be sure that a and b are non-negative, you can simplify that quite a bit to just:

Rectangle operator+(Rectangle &amp;o) {
    double left = std::min(x, o.x);
    double right = std::max(x + a, o.x + o.a);
    double top = std::min(y, o.y);
    double bottom = std::max(y + b, o.y + o.b);

    return Rectangle(left, top, right - left, bottom - top);
}


As for the rest of the code, I see two obvious things. First of all, the variable names aren't particularly helpful. I guess I can sort of keep things like x1 and x2 straight, but only just.

Second, the logic strikes me as almost a complete mess. I guess I can understand what it needs to do, and if I was willing to spend enough time on it I could verify that it works correctly--but it's not immediately obvious to me at first glance, and it would take some fairly exhaustive (and exhausting) analysis to be sure that it works for every possible case, not to mention being sure exactly how it works.

There's also one final caveat I feel obliged to add: an IEEE floating point number can be set to a NaN, which means all comparisons to that value will return false (even if (a == a) will yield false). If either of your rectangles contains a NaN, all of these will produce garbage results.