[HDOJ6354] 18多校05 Everything Has Changed (计算几何)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=6354

给你一个圆和互不相交的n个小圆,让你计算这个大圆被n个小圆切割后的周长,大圆内部的不计算。

很容易发现小圆和大圆相交以后,周长增加了小圆的弧长-大圆的弧长。用余弦定理计算一下大小圆关于交处的角度,然后用弧长公式算一下即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;

typedef struct Node {
double x, y, r;
}Node;
const double pi = acos(-1.0);
const int maxn = 111;
int n;
double r;
Node p[maxn];

double dis(Node a, Node b) {
return (double)sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double gao(Node a, Node b) {
if(b.x < 0) b.x = -b.x;
if(b.y < 0) b.y = -b.y;
double d = dis(a, b);
double theta1 = 2.0 * acos((d*d+a.r*a.r-b.r*b.r)/(2*d*a.r));
double theta2 = 2.0 * acos((d*d+b.r*b.r-a.r*a.r)/(2*d*b.r));
double L1 = theta1 * a.r;
double L2 = theta2 * b.r;
return L2 - L1;
}

signed main() {
// freopen("in", "r", stdin);
int T, _ = 1;
scanf("%d", &T);
while(T--) {
cin >> n >> r;
for(int i = 1; i <= n; i++) {
cin >> p[i].x >> p[i].y >> p[i].r;
}
double ret = 2 * pi * r;
Node o = {0, 0, r};
for(int i = 1; i <= n; i++) {
double d = dis(o, p[i]);
double R = o.r, r = p[i].r;
if(d >= R + r) continue;
if(d == R - r) {
ret += p[i].r * pi * 2.0;
}
if(R - r < d && d < R + r) {
ret += gao(o, p[i]);
}
}
printf("%.15f\n", ret);
}
return 0;
}