2014ACM/ICPC亚洲区北京站-I - Intersection

这篇博客介绍了如何计算两个相同大小的同心圆相交部分的面积。通过应用容斥原理,可以得出相交面积等于两个大圆相交面积减去两个大圆和小圆相交面积的两倍,再加上两个小圆相交的面积。文章提供了测试案例并要求求解相交区域的精确数值。
摘要由CSDN通过智能技术生成

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. 


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.

Input

The first line contains only one integer T (T ≤ 10 5), 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 x i, y i (0 ≤ x i, y i ≤ 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

用容斥定律推出公式,两个圆环相交的面积 = 两个大圆相交的面积 - 2 * 大圆和小圆相交的面积 + 两个小圆相交的面积

#include <bits/stdc++.h>
#define PI 3.14159265358979323846
using namespace std;
 
struct Round
{
    int x,y;
    int rr;
}yuan[5];
 
double dis(Round a,Round b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
 
double solve(Round a,Round b)
{
    double d=dis(a,b);
    if (d>= a.rr+b.rr)
        return 0;
    if (d<=abs(a.rr-b.rr))
    {
        double r=a.rr<b.rr?a.rr:b.rr;
        return PI*r*r;
    }
    double ang1=acos((a.rr*a.rr+d*d-b.rr*b.rr)/2./a.rr/d);
    double ang2=acos((b.rr*b.rr+d*d-a.rr*a.rr)/2./b.rr/d);
    double ret=ang1*a.rr*a.rr+ang2*b.rr*b.rr-d*a.rr*sin(ang1);
    return ret;
}
int main()
{
    int t;
    int r,R;
    int x1,y1,x2,y2;
    double sum1,sum2,sum3,sum4;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%d%d",&r,&R);
        scanf("%d%d",&x1,&y1);
        scanf("%d%d",&x2,&y2);
        yuan[1].x=x1;yuan[1].y=y1;yuan[1].rr=r;//小
        yuan[2].x=x1;yuan[2].y=y1;yuan[2].rr=R;//大
        yuan[3].x=x2;yuan[3].y=y2;yuan[3].rr=r;//小
        yuan[4].x=x2;yuan[4].y=y2;yuan[4].rr=R;//大
        sum1=solve(yuan[2],yuan[4]);
        sum2=solve(yuan[1],yuan[4]);
        sum3=solve(yuan[1],yuan[3]);
        sum4=sum1-2*sum2+sum3;
        printf("Case #%d: %.6lf\n",i,sum4);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值