Geometry 二维圆与矩形相交测试

Geometry 二维圆与矩形相交测试,geometry,collision-detection,Geometry,Collision Detection,我正在做的是测试一个圆和一个矩形之间的相交程度。我想知道矩形是否完全在圆内,部分相交,或者根本没有相交 我附上了我今天提出的代码,它只是检查从圆心到矩形角的距离,以确定相交的水平 我想知道有没有更有效的方法 编辑: 这是我更新的工作代码。fullIntersect是我自己的,我在上找到了partialIntersect片段。我将把这个问题留给大家,因为我仍然好奇是否有更好的方法来解决这个问题 public boolean fullIntersect(float circleX, floa

我正在做的是测试一个圆和一个矩形之间的相交程度。我想知道矩形是否完全在圆内,部分相交,或者根本没有相交

我附上了我今天提出的代码,它只是检查从圆心到矩形角的距离,以确定相交的水平

我想知道有没有更有效的方法

编辑: 这是我更新的工作代码。fullIntersect是我自己的,我在上找到了partialIntersect片段。我将把这个问题留给大家,因为我仍然好奇是否有更好的方法来解决这个问题

    public boolean fullIntersect(float circleX, float circleY, float radius)
    {
        float radsq = radius * radius;
        double xsq = Math.pow(circleX - xPosition, 2);
        double xpwsq = Math.pow(circleX - (xPosition + width), 2);
        double ysq = Math.pow(circleY - yPosition, 2);
        double yphsq = Math.pow(circleY - (yPosition + height), 2);

        if(xsq + ysq > radsq || xsq + yphsq > radsq || xpwsq + yphsq > radsq || xpwsq + ysq > radsq)
            return false;

        return true;

        /* this is what the one if statement does
        double disBotLeft = xsq + ysq;
        double disTopLeft = xsq + yphsq;
        double disTopRight = xpwsq + yphsq;
        double disBotRight = xpwsq + ysq;

        if(disBotRight > radsq) return false;
        if(disBotLeft > radsq) return false;
        if(disTopLeft > radsq) return false;
        if(disTopRight > radsq) return false;

        return true;
        */
    }

    public int intersects(float circleX, float circleY, float radius)
    {
        if(!enabled) return 0;
        double wo2 = width / 2.0d;
        double ho2 = height / 2.0d;

        double circleDistanceX = Math.abs(circleX - xPosition - wo2);
        double circleDistanceY = Math.abs(circleY - yPosition - ho2);

        if (circleDistanceX > (wo2 + radius)) { return 0; }
        if (circleDistanceY > (ho2 + radius)) { return 0; }

        if(fullIntersect(circleX, circleY, radius)) { return 2; }

        if (circleDistanceX <= (wo2)) { return 1; } 
        if (circleDistanceY <= (ho2)) { return 1; }

        double cornerDistance_sq = Math.pow(circleDistanceX - wo2,2) +
                             Math.pow(circleDistanceY - ho2,2);

        return cornerDistance_sq <= (radius*radius) ? 1 : 0;
    }
public boolean fullIntersect(浮点圆、浮点圆、浮点半径)
{
浮动radsq=半径*半径;
double xsq=Math.pow(circleX-xPosition,2);
double xpwsq=Math.pow(circleX-(xPosition+width),2);
双ysq=数学功率(circleY-yPosition,2);
双yphsq=数学功率(圆圈-(yPosition+height),2);
如果(xsq+ysq>radsq | xsq+yphsq>radsq | xpwsq+yphsq>radsq | xpwsq+ysq>radsq)
返回false;
返回true;
/*这就是one-if语句所做的
双排左=xsq+ysq;
双disTopLeft=xsq+yphsq;
双disTopRight=xpwsq+yphsq;
双重解除授权=xpwsq+ysq;
如果(disBotRight>radsq)返回false;
if(disBotLeft>radsq)返回false;
if(disTopLeft>radsq)返回false;
if(disTopRight>radsq)返回false;
返回true;
*/
}
公共整数相交(浮点圆、浮点圆、浮点半径)
{
如果(!enabled)返回0;
双wo2=宽度/2.0d;
双ho2=高度/2.0d;
双圈距离x=Math.abs(圈-xPosition-wo2);
双圈距离y=Math.abs(圈-位置-ho2);
如果(circleDistanceX>(wo2+半径)){返回0;}
如果(圆距离>(ho2+半径)){返回0;}
if(fullIntersect(circleX,circleY,radius)){return 2;}

如果我认为你的代码没有考虑这些交集:


一旦您增强代码/问题,我将删除此答案。

相关讨论:当没有交叉点时,您想区分矩形位于圆外的情况和圆包含在矩形内的情况吗?@user786653相关,但他们没有提出告诉我f矩形完全在圆圈内或不在。@ jffryyxx不,在这些情况下不需要区分。从参数名称,我可以假设你正在处理轴对齐矩形而不是矩形?是的,使它工作可能在加速之前考虑。