Rectangle Circle Intersection with Padding
<p>I have the following method which works great to test if a rectangle intersects a circle. How could I modify it to say provide an additional parameter, the padding, which means that the rectangle and the circle need to be a certain number of pixels away from each other?</p>

<pre><code>public static boolean rectangleCircleIntersection(RectangleRegion rect, CircularRegion circle, int padding) {
        int circleDistance_x = PsyMath.abs((circle.getX()+circle.getRadius()) - (rect.getX()+rect.getWidth()/2));
        int circleDistance_y = PsyMath.abs((circle.getY()+circle.getRadius()) - (rect.getY()+rect.getHeight()/2));

        if (circleDistance_x &gt; (rect.getWidth()/2 + circle.getRadius())) { return false; }
        if (circleDistance_y &gt; (rect.getHeight()/2 + circle.getRadius())) { return false; }

        if (circleDistance_x &lt;= (rect.getWidth()/2)) { return true; } 
        if (circleDistance_y &lt;= (rect.getHeight()/2)) { return true; }

        int cornerDistance_sq = (int)Math.pow((circleDistance_x - rect.getWidth()/2),2) +
                             (int)Math.pow((circleDistance_y - rect.getHeight()/2),2);

        return (cornerDistance_sq &lt;= (int)Math.pow(circle.getRadius(),2));
    }
</code></pre>

<p>This was my attempt, but I'm not too confident it's correct:</p>

<pre><code>public static boolean rectangleCircleIntersection(RectangleRegion rect, CircularRegion circle, int padding) {
        int circleDistance_x = PsyMath.abs((circle.getX()+circle.getRadius()) - (rect.getX()+rect.getWidth()/2));
        int circleDistance_y = PsyMath.abs((circle.getY()+circle.getRadius()) - (rect.getY()+rect.getHeight()/2));

        if (circleDistance_x &gt; (rect.getWidth()/2 + circle.getRadius() + padding)) { return false; }
        if (circleDistance_y &gt; (rect.getHeight()/2 + circle.getRadius() + padding)) { return false; }

        if ((circleDistance_x+padding) &lt;= (rect.getWidth()/2)) { return true; } 
        if ((circleDistance_y+padding) &lt;= (rect.getHeight()/2)) { return true; }

        int cornerDistance_sq = (int)Math.pow((circleDistance_x - rect.getWidth()/2),2) +
                             (int)Math.pow((circleDistance_y - rect.getHeight()/2),2);

        return (cornerDistance_sq &lt;= (int)Math.pow(circle.getRadius(),2));
    }
</code></pre>
