# mathematics - learn math

## 如何檢測圓與同一平面中任何其他圓之間的交點? (5)

Java中的這個解決方案使用了上面描述的數學表達式：

``````/**
*
* @param values
*            { x0, y0, r0, x1, y1, r1 }
* @return true if circles is intersected
*
*         Check if circle is intersect to another circle
*/
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;
}
}
``````

XNA / C＃解決方案

``````    class Circle
{
public Vector2 Center;

public bool Intersects(Circle circle)
{
float distanceX = Center.X - circle.Center.X;
float distanceY = Center.Y - circle.Center.Y;
return distanceX * distanceX + distanceY * distanceY <= radiusSum * radiusSum;
}
public bool Contains(Circle circle)
{
return false;
float distanceX = Center.X - circle.Center.X;
float distanceY = Center.Y - circle.Center.Y;
return distanceX * distanceX + distanceY * distanceY <= radiusD * radiusD;
}
}``````

• x0，y0，r0 =中心和圓的半徑0。
• x1，y1，r1 =中心和圓的半徑1。

``````boolean intersects = Math.hypot(x0-x1, y0-y1) <= (r0 + r1);
``````

``````ABS(R0 - R1) <= SQRT((x0 - x1)^2 + (y0 - y1)^2) <= (R0 + R1)
``````

``````(R0 - R1)^2 <= (x0 - x1)^2 + (y0 - y1)^2 <= (R0 + R1)^2
``````