HDU-5120-Intersection【几何-相交圆面积】

HDU-5120-Intersection

            Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)

Problem Description
Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The following figures are some famous examples you may know.

A ring is a 2-D figure bounded by two circles sharing the common center. The radius for these circles are denoted by r and R (r < R). For more details, refer to the gray part in the illustration below.
HDU-5120-Intersection【几何-相交圆面积】_第1张图片

Matt just designed a new logo consisting of two rings with the same size in the 2-D plane. For his interests, Matt would like to know the area of the intersection of these two rings.
HDU-5120-Intersection【几何-相交圆面积】_第2张图片

Input
The first line contains only one integer T (T ≤ 105), which indicates the number of test cases. For each test case, the first line contains two integers r, R (0 ≤ r < R ≤ 10).

Each of the following two lines contains two integers xi, yi (0 ≤ xi, yi ≤ 20) indicating the coordinates of the center of each ring.

Output

For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the area of intersection rounded to 6 decimal places.

Sample Input

2
2 3
0 0
0 0
2 3
0 0
5 0

Sample Output

Case #1: 15.707963
Case #2: 2.250778

题目链接:HDU 5120

题目思路:求相交圆环面积,套用求相交圆面积模板

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <stack>
using namespace std;

//-----------求相交圆面积----------
typedef struct node{
    double x,y;
    double r;
};
node n1,n2,n3,n4;
double AREAR(node c1,node c2){
    double d;
    double s,s1,s2,s3,angle1,angle2,temp;

    d = sqrt((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y));
    if(d>=(c1.r+c2.r))//两圆相离
        return 0;
    if((c1.r-c2.r)>=d)//两圆内含,c1大
        return acos(-1.0)*c2.r*c2.r;
    if((c2.r-c1.r)>=d)//两圆内含,c2大
        return acos(-1.0)*c1.r*c1.r;

    angle1=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));
    angle2=acos((c2.r*c2.r+d*d-c1.r*c1.r)/(2*c2.r*d));

    s1=angle1*c1.r*c1.r;s2=angle2*c2.r*c2.r;
    s3=c1.r*d*sin(angle1);
    s=s1+s2-s3;
     return s;
}
//-----------------------------------

int main(){
    int T;
    scanf("%d", &T);
    int cnt = 1;
    while(T --){
        double r1,r2,x1,y1,x2,y2;
        scanf("%lf%lf%lf%lf%lf%lf", &r1, &r2, &x1, &y1, &x2, &y2);
        n1.x = x1,n1.y = y1,n1.r = r2;
        n2.x = x1,n2.y = y1,n2.r = r1;
        n3.x = x2,n3.y = y2,n3.r = r2;
        n4.x = x2,n4.y = y2,n4.r = r1;
        double areatot = AREAR(n1,n3),area1 = AREAR(n1,n4),area2 = AREAR(n2,n3),area3 = AREAR(n2,n4);
        printf("Case #%d: %.6lf\n", cnt++, areatot-area1-area2+area3);
    }
}

你可能感兴趣的:(HDU,5120)