[英]Finding if a circle is inside another circle
我有點麻煩。 我有一項任務,要求我查找第二個圓圈是重疊,內部還是第二個圓圈。 但是,我無法檢查重疊,如果第二個圓圈在第一個圓圈內。
(使用的變量是x1,x2,y1,y2,r1,r2,距離)
這就是我所擁有的:
if (distance > (r1 + r2)) {
// No overlap
System.out.println("Circle2 does not overlap Circle1");
} else if (distance <= Math.abs(r1 + r2)) {
// Overlap
System.out.println("Circle2 overlaps Circle1");
} else if ((distance <= Math.abs(r1 - r2)) {
// Inside
System.out.println("Circle2 is inside Circle1");
}
我擔心問題在於重疊和內部檢查,但我無法弄清楚如何正確設置它以便我可以可靠地檢查第二個圓是否在第一個圓圈內。
任何幫助或建議將非常感激,因為我嘗試了多種方法,但解決方案每次都讓我逃避。
你需要在重疊之前檢查內部,因為內部距離是<=重疊距離
if (distance > (r1 + r2))
{
// No overlap
System.out.println("Circle2 does not overlap Circle1");
}
else if ((distance <= Math.abs(r1 - r2))
{
// Inside
System.out.println("Circle2 is inside Circle1");
}
else // if (distance <= r1 + r2)
{
// Overlap
System.out.println("Circle2 overlaps Circle1");
}
根據克里斯的評論修改了答案
這個問題可能最容易在視覺上解決,然后編寫代碼。 你看起來像是沒有內部和完全內部的正確邏輯。
解決這個問題的簡單方法是,如果它們沒有完全在內部而不是完全在外面,那么它們必須重疊。 這當然是我編碼的方式。 數學比其他兩個有點棘手。
if (distance > (r1 + r2)) {
// No overlap
System.out.println("Circle2 does not overlap Circle1");
} else if ((distance <= Math.abs(r1 - r2)) {
// Inside
System.out.println("Circle2 is inside Circle1");
{ else {
// Overlap
System.out.println("Circle2 overlaps Circle1");
}
實際情況是:
r2>r1-d
, r2 < r1+d
通過對稱,我們不需要同時進行兩種方式(如果你在兩者中交換r2和r1並進行一些重新排列,你會得到同一對方程式)。
最容易將其留在“其他”類別中,而不是編碼,除非您出於某種原因需要。
那么,如果距離和較小半徑的總和小於另一個半徑,則較小的圓應該在較大的圓內。
通過評論代理編輯顯而易見性:
空間點之間的距離由畢達哥拉斯描述:
distance = sqrt( travelled_x_squared + travelled_y_squared );
這當然轉化為代碼
distance = Math.sqrt( (x1-x2)*(x1-x2) + (y1 - y2)*(y1 - y2) );
距離在r1 + r2處接觸。
在編輯線索之前:您需要圓圈之間的角度。
然后計算從circle1到圓圈2的距離。如果它小於radii1 + radii2,你就在里面。
atan2可能是一個感興趣的函數。
或者直接選擇畢達哥拉斯的距離。
你幾乎就在那里。 只是條件的順序是錯誤的。
if (distance > (r1 + r2)) {
// No overlap
System.out.println("Circle2 does not overlap Circle1");
} else if ((distance <= Math.abs(r1 - r2)) {
// Inside
System.out.println("Circle2 is inside Circle1");
} else {
// Overlap
System.out.println("Circle2 overlaps Circle1");
}
在“非重疊”情況下檢查“內部”情況可確保不會意外地將其視為重疊。 然后所有其余的必須重疊。
這是一項簡單的任務,
取兩個圓的半徑之和。 說r1 + r2。 現在找到兩個圓的中心之間的距離sqrt((x1-x2)^ 2 +(y1-y2)^ 2) if r1+r2 = sqrt((x1-x2)^2 + (y1-y2)^2) they just touch each other. if r1+r2 > sqrt((x1-x2)^2 + (y1-y2)^2) the circle overlaps(intersect) if r1+ r2 < sqrt((x1-x2)^2 + (y1-y2)^2) the circle doesnot intersect
if r1+r2 = sqrt((x1-x2)^2 + (y1-y2)^2) they just touch each other. if r1+r2 > sqrt((x1-x2)^2 + (y1-y2)^2) the circle overlaps(intersect) if r1+ r2 < sqrt((x1-x2)^2 + (y1-y2)^2) the circle doesnot intersect
/**
*
* @param values { x0, y0, r0, x1, y1, r1 }
* @return true if circles is intersected
*/
public static boolean isCircleIntersect(double... values)
{
/* check using mathematical relation: ABS(R0-R1) <= SQRT((x0-x1)^2+(y0-y1)^2) <= (R0+R1) */
if (values.length == 6)
{
/* get values from first circle */
double x0 = values[0];
double y0 = values[1];
double r0 = values[2];
/* get values from second circle */
double x1 = values[3];
double y1 = values[4];
double r1 = values[5];
/* returun result */
return (Math.abs(r0 - r1) <= Math.sqrt(Math.pow((x0 - x1), 2) + Math.pow((y0 - y1), 2)))
&& (Math.sqrt(Math.pow((x0 - x1), 2) + Math.pow((y0 - y1), 2)) <= (r0 + r1));
}
else
{
/* return default result */
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.