我有以下方法可以很好地测试矩形是否与圆相交。我怎么能修改它说提供一个额外的参数,填充,这意味着矩形和圆形需要彼此相距一定数量的像素?
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 > (rect.getWidth()/2 + circle.getRadius())) { return false; }
if (circleDistance_y > (rect.getHeight()/2 + circle.getRadius())) { return false; }
if (circleDistance_x <= (rect.getWidth()/2)) { return true; }
if (circleDistance_y <= (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 <= (int)Math.pow(circle.getRadius(),2));
}
这是我的尝试,但我不太相信它是正确的:
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 > (rect.getWidth()/2 + circle.getRadius() + padding)) { return false; }
if (circleDistance_y > (rect.getHeight()/2 + circle.getRadius() + padding)) { return false; }
if ((circleDistance_x+padding) <= (rect.getWidth()/2)) { return true; }
if ((circleDistance_y+padding) <= (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 <= (int)Math.pow(circle.getRadius(),2));
}