1

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

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));
    }
4

2 に答える 2

2

rect.getWidth()とrect.getHeightを使用する代わりに、幅と高さ、およびその新しいパディングされた寸法にパディングを追加できます。

const int PADDING = 5;
int rectHeight = rect.getHeight() + PADDING;
int rectWidth = rect.getWidth() + PADDING;

//use rectHeight and rectWidth for calculation now

編集:左側のパディングを考慮して、計算で使用する位置を調整する必要があります、

int posX = rect.getX() - PADDING/2;
int posY = rect.getY() - PADDING/2;
int rectHeight = rect.getHeight() + PADDING/2;
int rectWidth = rect.getWidth() + PADDING/2;

参考までに、そのパディングで基本的に行っているのは、現在の長方形の周りに大きな長方形を作成することです。

于 2013-01-13T23:05:05.697 に答える
2

円を安全に埋めてから、それらが交差するかどうかを確認できます。四角形のパディングは、パディングされた四角形の角が元の四角形の角からパディングの sqrt(2) 倍になるため、まったく問題ありません。

したがって、上記のコードがうまく機能し、半径が int であると仮定すると、次のようになります。

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

    if (circleDistance_x > (rect.getWidth()/2 + paddedRadius)) { return false; }
    if (circleDistance_y > (rect.getHeight()/2 + paddedRadius)) { 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(paddedRadius,2));
}
于 2013-01-13T23:15:22.047 に答える