计算两圆交点:给定圆心与半径,返回交点对(若相交)或 false(若不相交或重合)。
Description: Computes the pair of points at which two circles intersect. Returns false in case of no intersection. Status: stress-tested
-
函数签名与参数(据实现)
- bool circleInter(P a, P b, double r1, double r2, pair<P,P>* out)
- a, b 为两圆心,r1/r2 为半径;out 输出交点对。
-
判定条件
- 若 a==b(圆心重合),断言半径不同后返回 false(无交点或无穷交点)。
- vec = b - a;d2 = vec.dist2();sum = r1+r2;dif = r1-r2。
- 不相交条件:sumsum < d2(圆心距大于半径和)或 difdif > d2(一圆内含另一圆),返回 false。
-
交点计算
- p = (d2 + r1^2 - r2^2)/(2*d2):从 a 向 b 方向的投影比例。
- h2 = r1^2 - p^2*d2:弦心距平方(保护负值用 fmax(0, h2))。
- mid = a + vec*p:弦心。
- per = vec.perp() * sqrt(h2/d2):垂直方向位移。
- 返回 {mid+per, mid-per} 作为两交点。