# 【计算几何模板】求两个圆的交点

#### 问题分析：

《训练指南》上求两圆交点的模板用了atan2acos等库函数，精度损失比较严重。

• 圆的标准方程：$(x-x_0)^2+(y-y_0)^2=r^2$
• 圆的参数方程：$\left\{\begin{matrix} x=x_0+r\cdot cos\theta \\ y=y_0+r\cdot sin\theta \end{matrix}\right.$

$(x_1+r_1cos\theta-x_2)^2+(y_1+r_1sin\theta-y_2)^2=r_2^2$

$2r_1(x_1-x_2)cos\theta+2r_1(y_1-y_2)sin\theta=r_2^2-r_1^2-(x_1-x_2)^2-(y_1-y_2)^2$

$a=2r_1(x_1-x_2)$
$b=2r_1(y_1-y_2)$
$c=r_2^2-r_1^2-(x_1-x_2)^2-(y_1-y_2)^2$

$a cos\theta+b sin\theta=c$
$cos\theta=x, \, sin\theta=\sqrt{1-x^2}$，关于$sin\theta$的正负后面再判断。

$sin\theta$$cos\theta$代回到第一个圆的参数方程，能得到交点的坐标。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>

typedef long double LD;

const LD eps = 1e-10;

int dcmp(LD x) {
if(fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}

LD sqr(LD x) { return x * x; }

struct Point
{
LD x, y;
Point(LD x = 0, LD y = 0):x(x), y(y) {}
void read() { cin >> x >> y; }
};

Point operator - (const Point& A, const Point& B) {
return Point(A.x - B.x, A.y - B.y);
}

bool operator == (const Point& A, const Point& B) {
return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.x) == 0;
}

LD Dot(const Point& A, const Point& B) {
return A.x * B.x + A.y * B.y;
}

LD Length(const Point& A) { return sqrt(Dot(A, A)); }

struct Circle
{
Point c;
LD r;
Circle() {}
Circle(Point c, LD r):c(c), r(r) {}
};

int getCircleIntersection(Circle C1, Circle C2) {
LD &r1 = C1.r, &r2 = C2.r;
LD &x1 = C1.c.x, &x2 = C2.c.x, &y1 = C1.c.y, &y2 = C2.c.y;
LD d = Length(C1.c - C2.c);
if(dcmp(fabs(r1-r2) - d) > 0) return -1;
if(dcmp(r1 + r2 - d) < 0) return 0;
LD d2 = Dot(C1.c - C2.c, C1.c - C2.c);
LD a = r1*(x1-x2)*2, b = r1*(y1-y2)*2, c = r2*r2-r1*r1-d*d;
LD p = a*a+b*b, q = -a*c*2, r = c*c-b*b;

LD cosa, sina, cosb, sinb;
//One Intersection
if(dcmp(d - (r1 + r2)) == 0 || dcmp(d - fabs(r1 - r2)) == 0) {
cosa = -q / p / 2;
sina = sqrt(1 - sqr(cosa));
Point p(x1 + C1.r * cosa, y1 + C1.r * sina);
if(!OnCircle(p, C2)) p.y = y1 - C1.r * sina;
inter.push_back(p);
return 1;
}
//Two Intersections
LD delta = sqrt(q * q - p * r * 4);
cosa = (delta - q) / p / 2;
cosb = (-delta - q) / p / 2;
sina = sqrt(1 - sqr(cosa));
sinb = sqrt(1 - sqr(cosb));
Point p1(x1 + C1.r * cosa, y1 + C1.r * sina);
Point p2(x1 + C1.r * cosb, y1 + C1.r * sinb);
if(!OnCircle(p1, C2)) p1.y = y1 - C1.r * sina;
if(!OnCircle(p2, C2)) p2.y = y1 - C1.r * sinb;
if(p1 == p2)  p1.y = y1 - C1.r * sina;
inter.push_back(p1);
inter.push_back(p2);
return 2;
}
posted @ 2016-01-04 12:11  AOQNRMGYXLMV  阅读(...)  评论(...编辑  收藏