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 回答