Compute shortest distance between point and a rectangle
Bug

If either x == x_min or x == x_max, your function will return 0 even if the point is outside of the rectangle.  This is because none of your if statements included the case where x == x_min or x == x_max, so the flow falls through to the end of the function where you return 0.

Extraneous variable

Your function has a variable r that is set but never used.  You should remove that variable so that lines like this:


        return r = x_min - x;



can turn into this:

        return x_min - x;


Extraneous third comparison

Consider this chain of three if statements:


    if(y &lt; y_min){
        return r = sqrt( sqr(x_min-x) + sqr(y_min-y) );
    }else if(y &gt; y_max){
        return r = sqrt( sqr(x_min-x) + sqr(y_max-y) );
    }else if((y &gt;= y_min) &amp;&amp; (y &lt;= y_max)){
        return r = x_min - x;
    }



The point can either be above the rectangle, below the rectangle, or within the rectangle.  If the point is neither above nor below the rectangle, then it must be within the rectangle.  Therefore, the third comparison can be removed, like this:

    if(y &lt; y_min){
        return r = sqrt( sqr(x_min-x) + sqr(y_min-y) );
    }else if(y &gt; y_max){
        return r = sqrt( sqr(x_min-x) + sqr(y_max-y) );
    }else{
        return r = x_min - x;
    }


Rewrite

In this rewrite, I defined a HYPOT macro to simplify the expression that you use 4 times.  I also modified the indentation so that each case would be at the same indentation level.  Lastly, I reordered the if statements so the order would be "below, inside, above" instead of "below, above, inside", although the order really doesn't matter.  This rewrite was a tiny bit faster (2%) than the original code, possibly due to removing the extraneous third comparisons mentioned above.

#define HYPOT(x, y) sqrt(sqr(x) + sqr(y))

double distanceBoxParticle2D(double x, double y, double x_min, double y_min,
        double x_max, double y_max)
{
    if (x &lt; x_min) {
        if (y &lt; y_min)
            return HYPOT(x_min-x, y_min-y);
        else if (y &lt;= y_max)
            return x_min - x;
        else
            return HYPOT(x_min-x, y_max-y);
    } else if (x &lt;= x_max) {
        if (y &lt; y_min)
            return y_min - y;
        else if (y &lt;= y_max)
            return 0;
        else
            return y - y_max;
    } else {
        if (y &lt; y_min)
            return HYPOT(x_max-x, y_min-y);
        else if (y &lt;= y_max)
            return x - x_max;
        else
            return HYPOT(x_max-x, y_max-y);
    }
}


Here is a more concise variant you might prefer:

double distanceBoxParticle2D(double x, double y, double x_min, double y_min,
        double x_max, double y_max)
{
    if (x &lt; x_min) {
        if (y &lt;  y_min) return HYPOT(x_min-x, y_min-y);
        if (y &lt;= y_max) return x_min - x;
                        return HYPOT(x_min-x, y_max-y);
    } else if (x &lt;= x_max) {
        if (y &lt;  y_min) return y_min - y;
        if (y &lt;= y_max) return 0;
                        return y - y_max;
    } else {
        if (y &lt;  y_min) return HYPOT(x_max-x, y_min-y);
        if (y &lt;= y_max) return x - x_max;
                        return HYPOT(x_max-x, y_max-y);
    }
}