長方形が円と交差するかどうかをテストするのに最適な次の方法があります。追加のパラメータであるパディングを提供するように変更するにはどうすればよいですか。つまり、長方形と円は互いに特定のピクセル数だけ離れている必要があります。
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));
}