2
votes

What I'm doing is testing to see what the level of intersection between a circle and rectangle. I would like to find whether the rectangle is completely inside the circle, partially intersecting it, or if there is no intersection at all.

I've attached the code that I've come up with today, it simply checks the distances from the center of the circle to the corners of the rectangle to determine the level of intersection.

What I'm wondering is there a more efficient way of doing this?

EDIT: Here is my updated, working code. fullIntersect is my own, I found the partialIntersect snippet on Circle-Rectangle collision detection (intersection). I'm going to leave this open, as I'm still curious as to whether there is a better way of doing this.

    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;
    }
1
When there is no intersection, do you want to differentiate between the case where the rectangle lies outside the circle and the case where the circle is contained inside the rectangle?Jeffrey Sax
@user786653 Related, but they do not bring up the question of telling if the rectangle is completely inside the circle or not.Tanner
@JeffreySax No, there does not need to be a differentiation between those cases.Tanner
From your parameter names, can I assume you're dealing with axis-aligned rectangles instead of rectangles?R. Martinho Fernandes

1 Answers

4
votes

I think your code does not consider these intersections:

enter image description here

I'll delete this answer as soon as you enhance your code/question.