Everything Has Changed
题目
Edward is a worker for Aluminum Cyclic Machinery. His work is operating mechanical arms to cut out designed models. Here is a brief introduction of his work.
Assume the operating plane as a two-dimensional coordinate system. At first, there is a disc with center coordinates
(
0
,
0
)
(0, 0)
(0,0) and radius
R
R
R. Then,
m
m
m mechanical arms will cut and erase everything within its area of influence simultaneously, the
i
i
i-th area of which is a circle with center coordinates
(
x
i
,
y
i
)
(x_i, y_i)
(xi,yi) and radius
r
i
r_i
ri
(
i
=
1
,
2
,
⋯
 
,
m
)
(i = 1, 2, \cdots, m)
(i=1,2,⋯,m). In order to obtain considerable models, it is guaranteed that every two cutting areas have no intersection and no cutting area contains the whole disc.
Your task is to determine the perimeter of the remaining area of the disc excluding internal perimeter.
Here is an illustration of the sample, in which the red curve is counted but the green curve is not.
Input:
The first line contains one integer
T
T
T, indicating the number of test cases.
The following lines describe all the test cases. For each test case:
The first line contains two integers
m
m
m and
R
R
R.
The
i
i
i-th line of the following
m
m
m lines contains three integers
x
i
,
y
i
x_i, y_i
xi,yi and
r
i
r_i
ri, indicating a cutting area.
1
≤
T
≤
1000
1 \leq T \leq 1000
1≤T≤1000,
1
≤
m
≤
100
1 \leq m \leq 100
1≤m≤100,
−
1000
≤
x
i
,
y
i
≤
1000
-1000 \leq x_i, y_i \leq 1000
−1000≤xi,yi≤1000,
1
≤
R
,
r
i
≤
1000
1 \leq R, r_i \leq 1000
1≤R,ri≤1000
(
i
=
1
,
2
,
⋯
 
,
m
)
(i = 1, 2, \cdots, m)
(i=1,2,⋯,m).
Output:
For each test case, print the perimeter of the remaining area in one line. Your answer is considered correct if its absolute or relative error does not exceed
1
0
−
6
10^{-6}
10−6.
Formally, let your answer be
a
a
a and the jury’s answer be
b
b
b. Your answer is considered correct if
∣
a
−
b
∣
max
(
1
,
∣
b
∣
)
≤
1
0
−
6
\frac{|a - b|}{\max(1, |b|)} \leq 10^{-6}
max(1,∣b∣)∣a−b∣≤10−6.
题目大意
给定一个大圆,圆心为原点 ( 0 , 0 ) (0, 0) (0,0),半径是 R R R, 然后给定很多个小圆的圆心和半径,每个小圆不会相交,求如图所示的弧长(也就是小圆与大圆相交的里面的弧长,完全包含的小圆的弧长不算)
分析
如图所示, 设
α
=
∠
E
A
C
,
β
=
∠
C
B
A
\alpha = \angle EAC, \beta = \angle CBA
α=∠EAC,β=∠CBA,则由余弦定理有
α
=
arccos
R
2
+
d
2
−
r
2
2
⋅
R
⋅
d
\alpha = \arccos \frac{R^2 + d^2 - r^2}{2 \cdot R \cdot d}
α=arccos2⋅R⋅dR2+d2−r2
β
=
arccos
r
2
+
d
2
−
R
2
2
⋅
r
⋅
d
\beta = \arccos \frac{r^2 + d^2 - R^2}{2 \cdot r \cdot d}
β=arccos2⋅r⋅dr2+d2−R2
由弧度的定义有每个小圆的贡献为:
x
=
r
⋅
2
β
−
R
⋅
2
α
x = r \cdot 2\beta - R \cdot 2\alpha
x=r⋅2β−R⋅2α
设原来圆的周长为
C
=
2
π
R
C = 2 \pi R
C=2πR,再把贡献加起来就行了。
这里要注意的是,要把两圆内含,外离,外切的情况去掉。
由半径与圆心距的关系有:
R
−
r
≤
d
<
R
+
r
R - r \le d < R + r
R−r≤d<R+r
计算几何记得全部开double
参考代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#ifdef LOCAL
#define frein(x) freopen(x, "r", stdin)
#define freout(x) freopen(x, "w", stdout)
#else
#define frein(x)
#define freout(x)
#endif
#define mem(x, v) memset(x, v, sizeof(x))
#define squ(x) ((x) * (x))
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1);
int main(void)
{
frein("data.in");
//freout("data.out");
int T;
scanf("%d", &T);
while (T--)
{
double m, R;
scanf("%lf%lf", &m, &R);
double ans = 2 * pi * R;
for (int i = 0; i < m; i++)
{
double x, y, r;
scanf("%lf%lf%lf", &x, &y, &r);
double d = sqrt(squ(x) + squ(y));
if (R - r <= d && d < R + r)
{
double t1 = acos((squ(R) + squ(d) - squ(r)) / (2 * R * d));
double t2 = acos((squ(r) + squ(d) - squ(R)) / (2 * r * d));
ans += 2 * r * t2 - 2 * R * t1;
}
}
printf("%.15f\n", ans);
}
return 0;
}