Circle-Rectangle collision detection (intersection)

前端 未结 24 1402
无人共我
无人共我 2020-11-22 02:55

How can I tell whether a circle and a rectangle intersect in 2D Euclidean space? (i.e. classic 2D geometry)

相关标签:
24条回答
  • This is the fastest solution:

    public static boolean intersect(Rectangle r, Circle c)
    {
        float cx = Math.abs(c.x - r.x - r.halfWidth);
        float xDist = r.halfWidth + c.radius;
        if (cx > xDist)
            return false;
        float cy = Math.abs(c.y - r.y - r.halfHeight);
        float yDist = r.halfHeight + c.radius;
        if (cy > yDist)
            return false;
        if (cx <= r.halfWidth || cy <= r.halfHeight)
            return true;
        float xCornerDist = cx - r.halfWidth;
        float yCornerDist = cy - r.halfHeight;
        float xCornerDistSq = xCornerDist * xCornerDist;
        float yCornerDistSq = yCornerDist * yCornerDist;
        float maxCornerDistSq = c.radius * c.radius;
        return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
    }
    

    Note the order of execution, and half the width/height is pre-computed. Also the squaring is done "manually" to save some clock cycles.

    0 讨论(0)
  • 2020-11-22 02:58

    Here is the modfied code 100% working:

    public static bool IsIntersected(PointF circle, float radius, RectangleF rectangle)
    {
        var rectangleCenter = new PointF((rectangle.X +  rectangle.Width / 2),
                                         (rectangle.Y + rectangle.Height / 2));
    
        var w = rectangle.Width  / 2;
        var h = rectangle.Height / 2;
    
        var dx = Math.Abs(circle.X - rectangleCenter.X);
        var dy = Math.Abs(circle.Y - rectangleCenter.Y);
    
        if (dx > (radius + w) || dy > (radius + h)) return false;
    
        var circleDistance = new PointF
                                 {
                                     X = Math.Abs(circle.X - rectangle.X - w),
                                     Y = Math.Abs(circle.Y - rectangle.Y - h)
                                 };
    
        if (circleDistance.X <= (w))
        {
            return true;
        }
    
        if (circleDistance.Y <= (h))
        {
            return true;
        }
    
        var cornerDistanceSq = Math.Pow(circleDistance.X - w, 2) + 
                                        Math.Pow(circleDistance.Y - h, 2);
    
        return (cornerDistanceSq <= (Math.Pow(radius, 2)));
    }
    

    Bassam Alugili

    0 讨论(0)
  • 2020-11-22 02:58

    Works, just figured this out a week ago, and just now got to testing it.

    double theta = Math.atan2(cir.getX()-sqr.getX()*1.0,
                              cir.getY()-sqr.getY()*1.0); //radians of the angle
    double dBox; //distance from box to edge of box in direction of the circle
    
    if((theta >  Math.PI/4 && theta <  3*Math.PI / 4) ||
       (theta < -Math.PI/4 && theta > -3*Math.PI / 4)) {
        dBox = sqr.getS() / (2*Math.sin(theta));
    } else {
        dBox = sqr.getS() / (2*Math.cos(theta));
    }
    boolean touching = (Math.abs(dBox) >=
                        Math.sqrt(Math.pow(sqr.getX()-cir.getX(), 2) +
                                  Math.pow(sqr.getY()-cir.getY(), 2)));
    
    0 讨论(0)
  • 2020-11-22 03:01

    Here is another solution that's pretty simple to implement (and pretty fast, too). It will catch all intersections, including when the sphere has fully entered the rectangle.

    // clamp(value, min, max) - limits value to the range min..max
    
    // Find the closest point to the circle within the rectangle
    float closestX = clamp(circle.X, rectangle.Left, rectangle.Right);
    float closestY = clamp(circle.Y, rectangle.Top, rectangle.Bottom);
    
    // Calculate the distance between the circle's center and this closest point
    float distanceX = circle.X - closestX;
    float distanceY = circle.Y - closestY;
    
    // If the distance is less than the circle's radius, an intersection occurs
    float distanceSquared = (distanceX * distanceX) + (distanceY * distanceY);
    return distanceSquared < (circle.Radius * circle.Radius);
    

    With any decent math library, that can be shortened to 3 or 4 lines.

    0 讨论(0)
  • 2020-11-22 03:01

    Here's a fast one-line test for this:

    if (length(max(abs(center - rect_mid) - rect_halves, 0)) <= radius ) {
      // They intersect.
    }
    

    This is the axis-aligned case where rect_halves is a positive vector pointing from the rectangle middle to a corner. The expression inside length() is a delta vector from center to a closest point in the rectangle. This works in any dimension.

    0 讨论(0)
  • 2020-11-22 03:02

    Actually, this is much more simple. You need only two things.

    First, you need to find four orthogonal distances from the circle centre to each line of the rectangle. Then your circle will not intersect the rectangle if any three of them are larger than the circle radius.

    Second, you need to find the distance between the circle centre and the rectangle centre, then you circle will not be inside of the rectangle if the distance is larger than a half of the rectangle diagonal length.

    Good luck!

    0 讨论(0)
提交回复
热议问题