簡體   English   中英

帶填充的矩形圓交點

[英]Rectangle Circle Intersection with Padding

我有以下方法可以很好地測試矩形是否與圓相交。 我怎么能修改它來說提供一個額外的參數,填充,這意味着矩形和圓圈需要相隔一定數量的像素?

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

而不是使用rect.getWidth()和rect.getHeight,您可以添加填充到寬度和高度以及新的填充尺寸。

const int PADDING = 5;
int rectHeight = rect.getHeight() + PADDING;
int rectWidth = rect.getWidth() + PADDING;

//use rectHeight and rectWidth for calculation now

編輯:要考慮左邊的填充,你應該調整計算中使用的位置,

int posX = rect.getX() - PADDING/2;
int posY = rect.getY() - PADDING/2;
int rectHeight = rect.getHeight() + PADDING/2;
int rectWidth = rect.getWidth() + PADDING/2;

僅供參考,您基本上使用該填充做的是在當前矩形周圍創建一個更大的矩形。

您可以安全地填充圓圈,然后檢查它們是否相交。 填充矩形不太合適,因為填充矩形的角將是距離原始矩形的角的填充的sqrt(2)倍。

所以,假設上面的代碼工作得很好,並且半徑是一個int,你會得到:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM