2012-12-12 29 views
0

我有這三種方法來檢查一個圓是否在另一個圓內,除了相交圓標記爲內部和相交。我一直在閱讀文章,但沒有任何建議的選項似乎使其正常工作。這裏是我的方法:試圖檢查一個圓是否與第二個圓重疊,位於第二個圓之內或之外

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

isInside()計算只是做一個相交測試。如果你想測試一個圓是否完全包圍另一個圓,你會想測試兩個圓之間的距離加上小圓的半徑是否小於大圓的半徑。

例如:

public boolean isInside(Circle c2) { 
     return distanceTo(c2) + radius() <= c2.radius(); 
    } 
+0

稀釋是,就是這樣,完美!非常感謝! – Kynian

+0

沒問題,很高興幫助。 –

相關問題