Circle-Rectangle collision detection (intersection)

前端 未结 24 1714
无人共我
无人共我 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条回答
  •  无人共我
    2020-11-22 03:07

    I created class for work with shapes hope you enjoy

    public class Geomethry {
      public static boolean intersectionCircleAndRectangle(int circleX, int circleY, int circleR, int rectangleX, int rectangleY, int rectangleWidth, int rectangleHeight){
        boolean result = false;
    
        float rectHalfWidth = rectangleWidth/2.0f;
        float rectHalfHeight = rectangleHeight/2.0f;
    
        float rectCenterX = rectangleX + rectHalfWidth;
        float rectCenterY = rectangleY + rectHalfHeight;
    
        float deltax = Math.abs(rectCenterX - circleX);
        float deltay = Math.abs(rectCenterY - circleY);
    
        float lengthHypotenuseSqure = deltax*deltax + deltay*deltay;
    
        do{
            // check that distance between the centerse is more than the distance between the circumcircle of rectangle and circle
            if(lengthHypotenuseSqure > ((rectHalfWidth+circleR)*(rectHalfWidth+circleR) + (rectHalfHeight+circleR)*(rectHalfHeight+circleR))){
                //System.out.println("distance between the centerse is more than the distance between the circumcircle of rectangle and circle");
                break;
            }
    
            // check that distance between the centerse is less than the distance between the inscribed circle
            float rectMinHalfSide = Math.min(rectHalfWidth, rectHalfHeight);
            if(lengthHypotenuseSqure < ((rectMinHalfSide+circleR)*(rectMinHalfSide+circleR))){
                //System.out.println("distance between the centerse is less than the distance between the inscribed circle");
                result=true;
                break;
            }
    
            // check that the squares relate to angles
            if((deltax > (rectHalfWidth+circleR)*0.9) && (deltay > (rectHalfHeight+circleR)*0.9)){
                //System.out.println("squares relate to angles");
                result=true;
            }
        }while(false);
    
        return result;
    }
    
    public static boolean intersectionRectangleAndRectangle(int rectangleX, int rectangleY, int rectangleWidth, int rectangleHeight, int rectangleX2, int rectangleY2, int rectangleWidth2, int rectangleHeight2){
        boolean result = false;
    
        float rectHalfWidth = rectangleWidth/2.0f;
        float rectHalfHeight = rectangleHeight/2.0f;
        float rectHalfWidth2 = rectangleWidth2/2.0f;
        float rectHalfHeight2 = rectangleHeight2/2.0f;
    
        float deltax = Math.abs((rectangleX + rectHalfWidth) - (rectangleX2 + rectHalfWidth2));
        float deltay = Math.abs((rectangleY + rectHalfHeight) - (rectangleY2 + rectHalfHeight2));
    
        float lengthHypotenuseSqure = deltax*deltax + deltay*deltay;
    
        do{
            // check that distance between the centerse is more than the distance between the circumcircle
            if(lengthHypotenuseSqure > ((rectHalfWidth+rectHalfWidth2)*(rectHalfWidth+rectHalfWidth2) + (rectHalfHeight+rectHalfHeight2)*(rectHalfHeight+rectHalfHeight2))){
                //System.out.println("distance between the centerse is more than the distance between the circumcircle");
                break;
            }
    
            // check that distance between the centerse is less than the distance between the inscribed circle
            float rectMinHalfSide = Math.min(rectHalfWidth, rectHalfHeight);
            float rectMinHalfSide2 = Math.min(rectHalfWidth2, rectHalfHeight2);
            if(lengthHypotenuseSqure < ((rectMinHalfSide+rectMinHalfSide2)*(rectMinHalfSide+rectMinHalfSide2))){
                //System.out.println("distance between the centerse is less than the distance between the inscribed circle");
                result=true;
                break;
            }
    
            // check that the squares relate to angles
            if((deltax > (rectHalfWidth+rectHalfWidth2)*0.9) && (deltay > (rectHalfHeight+rectHalfHeight2)*0.9)){
                //System.out.println("squares relate to angles");
                result=true;
            }
        }while(false);
    
        return result;
      } 
    }
    

提交回复
热议问题