1

我有这三种方法来检查一个圆是否在另一个圆内,除了相交的圆被标记为内部和相交之外,一切正常。我一直在阅读文章,但建议的选项似乎都无法使其正常工作。这是我的方法:

    public boolean isInside(Circle c2) {
    // get the distance between the two center points
    double distance = Math.sqrt((x - c2.x) * (x - c2.x) + (y - c2.y) * (y - c2.y));
    // check to see if we are inside the first circle, if so then return
    // true, otherwise return false.
    if (distance <= ((radius) + (c2.radius))) {
        System.out.println(distance);
        return true;
    } else {
        return false;
    }
}

public boolean isOutside(Circle c2) {
    double distance = Math.sqrt((x - c2.x) * (x - c2.x) + (y - c2.y) * (y - c2.y));
    if (distance > ((radius) + (c2.radius))) {
        System.out.println(distance);
        return true;
    } else {
        return false;
    }

}

public boolean isIntersecting(Circle c2) {
    double distance = Math.sqrt((x - c2.x) * (x - c2.x) + (y - c2.y) * (y - c2.y));
    if (Math.abs((radius - c2.radius)) <= distance && distance <= (radius + c2.radius)) {
        System.out.println(distance);
        return true;
    } else {
        return false;
    }
}
4

1 回答 1

5

isInside() 计算只是进行交叉测试。如果要测试一个圆是否完全包围了另一个圆,则需要测试两个圆之间的距离加上较小圆的半径是否小于较大圆的半径。

例如:

    public boolean isInside(Circle c2) {
        return distanceTo(c2) + radius() <= c2.radius();
    }
于 2012-12-12T03:28:38.050 回答