java - 带填充的矩形圆交点

标签 java

我有以下方法非常适合测试矩形是否与圆相交。我如何修改它以提供一个额外的参数,即填充,这意味着矩形和圆形需要彼此相距一定数量的像素?

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

最佳答案

您可以安全地填充圆圈,然后检查它们是否相交。 填充矩形不太好,因为填充矩形的角与原始矩形角的间距是填充的 sqrt(2) 倍。

因此,假设上面的代码运行良好,并且半径是一个整数,您将得到:

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

关于java - 带填充的矩形圆交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14309221/

相关文章:

Java泛型成员初始化

java - 不能从静态上下文中引用非静态类

java - 使用数据库作为我的上下文绑定(bind)而不是文件系统

java - 使用java : list. item(i).getFirstChild()遍历xml DOM

java - 尝试有意共享 mp3,但不包含文件扩展名

java - DateFormat Android - 正确吗?

java - Xuggler读取某些FLV文件

Java Double 转换为 Int round

java - 如何在不使用反射的情况下查看对象是否为数组?

java - 如何使用 Java Apache POI 在 Excel 工作表中隐藏以下未使用的行?