1

円が別の円の内側にあるかどうかを確認するこれらの 3 つの方法があります。交差する円が内側と交差しているとマークされているという事実を除いて、すべてが機能します。私は記事を読んできましたが、提案されたオプションのどれも正しく機能していないようです。ここに私の方法があります:

    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() の計算は交差テストを行っているだけです。円が別の円を完全に包み込んでいるかどうかをテストする場合は、2 つの円の間の距離に小さい方の円の半径を加えた値が、大きい方の円の半径よりも小さいかどうかをテストする必要があります。

例えば:

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