[HDU多校05] HDU 6354 Everything Has Changed 计算几何

Everything Has Changed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 414    Accepted Submission(s): 234
Special Judge

 

Problem Description

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) and radius R. Then, m mechanical arms will cut and erase everything within its area of influence simultaneously, the i-th area of which is a circle with center coordinates (xi,yi) and radius ri (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, 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 and R.
The i-th line of the following m lines contains three integers xi,yi and ri, indicating a cutting area.
1≤T≤1000, 1≤m≤100, −1000≤xi,yi≤1000, 1≤R,ri≤1000 (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 10−6.
Formally, let your answer be a and the jury's answer be b. Your answer is considered correct if |a−b|max(1,|b|)≤10−6.

 

Sample Input

1

4 10

6 3 5

10 -4 3

-2 -4 4

0 9 1

Sample Output

81.62198908430238475376

Source

2018 Multi-University Training Contest 5


题解

签到计算几何也还行

不过qls一句话确实说到点上 : 当大部分选手被开场的题支配的时候,就会放弃去思考其他题,然后节奏就崩了。

那题解就是利用余弦定理求出每个圆与起始圆的圆心角。

加上一段弧长,减去一段弧长即可。

需要注意的是 acos函数的取值在(-1,1)需要norm函数归正下取值。

然后就是圆与圆的位置关系判断。


代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int maxn = 110;
int sgn(double x) {
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    else return 1;
}
double norm(double x) { /// 防止 acos(x) 越界
    return min(max(x,-1.0),1.0);
}
struct Point {
    double x,y;
    Point() {}
    Point(double _x,double _y) { x = _x;y = _y;}
    Point operator - (const Point &b) const {
        return Point(x - b.x,y - b.y);
    }
    double distance(Point p) {
        return hypot(x-p.x,y-p.y);
    }
};
struct circle {
    Point p;
    double r;
    circle(){}
    circle(Point _p,double _r) {
        p = _p;
        r = _r;
    }
    circle(double x,double y,double _r) {
        p = Point(x,y);
        r = _r;
    }
    double circumference() {
        return 2 * pi * r;
    }
    int relationcircle(circle v) { // 判断两圆的位置关系
        double d = p.distance(v.p);
        if(sgn(d-r-v.r) > 0) return 5; // 相离
        if(sgn(d-r-v.r) == 0) return 4;// 相切
        double l = fabs(r - v.r);
        if(sgn(d-r-v.r)<0 && sgn(d-l)>0) return 3;// 相交
        if(sgn(d-l) == 0) return 2; // 内切
        if(sgn(d-l) < 0) return 1;  // 内离
    }
};
circle cir[maxn];
int n;double R;
int main()
{
    int caset;scanf("%d",&caset);
    while(caset--) {
        scanf("%d%lf",&n,&cir[0].r);
        cir[0].p.x = cir[0].p.y = 0;
        for(int i=1;i<=n;i++) scanf("%lf%lf%lf",&cir[i].p.x,&cir[i].p.y,&cir[i].r);
        double res = cir[0].circumference();
        for(int i=1;i<=n;i++) {
            if(cir[0].relationcircle(cir[i]) == 3 ) { // 相交
                // 余弦定理求圆心角
                double dist = cir[i].p.distance(cir[0].p);
                double add = acos(norm((cir[i].r*cir[i].r+dist*dist-cir[0].r*cir[0].r)/2.0/cir[i].r/dist));
                res += 2 * add * cir[i].r;
                double sub = acos(norm((cir[0].r*cir[0].r+dist*dist-cir[i].r*cir[i].r)/2.0/cir[0].r/dist));
                res -= 2 * sub * cir[0].r;
            }
            else if(cir[0].relationcircle(cir[i]) == 2) { // 内切
                res += cir[i].circumference();
            }
        }
        printf("%.7f\n",res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38013346/article/details/81463273
今日推荐