light oj 1118

来源:互联网 发布:视频教学软件 编辑:程序博客网 时间:2024/06/12 04:42

基础(两圆相交面积)
Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

Description

In the biological lab, you were examining some of the molecules. You got some interesting behavior about some of the molecules. There are some circular molecules, when two of them collide, they overlap with each other, and it's hard to find that which one is over the other one.

Given two molecules as circles, you have to find the common area of the given molecules that is shaded in the picture.

Overlapping Molecules

Input

Input starts with an integer T (≤ 12), denoting the number of test cases.

Each case contains six integers x1, y1, r1 and x2, y2, r2. Where (x1, y1) is the center of the first molecule and r1 is the radius and (x2, y2) is the center of the second molecule and r2 is the radius. Both the radiuses are positive. No integer will contain more than 3 digits.

Output

For each test case, print the case number and the common area of the given molecules. Errors less than 10-6 will be ignored.

Sample Input

3

0 0 10 15 0 10

-10 -10 5 0 -10 10

100 100 20 100 110 20

Sample Output

Case 1: 45.3311753978

Case 2: 35.07666099

Case 3: 860.84369


有时候用double 处理精度时很容易出错,可以直接用表达式表示


#include <iostream>

#include <cstdio>
#include <cmath>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-7;
struct node
{
    double x, y, r;
}p[4];


int main()
{
    int t, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lf %lf %lf %lf %lf %lf",&p[1].x, &p[1].y, &p[1].r, &p[2].x,&p[2].y,&p[2].r);
        if(p[1].r>p[2].r)
        {
            node tmp;
            tmp=p[1], p[1]=p[2], p[2]=tmp;
        }
        double dist=sqrt((p[1].x-p[2].x)*(p[1].x-p[2].x)+(p[1].y-p[2].y)*(p[1].y-p[2].y));
        double s=p[1].r+p[2].r;
        if(dist>=s)
        {
            printf("Case %d: 0\n",ncase++);
        }
        else if(p[2].r>=dist+p[1].r)
        {
            printf("Case %d: %.10f\n",ncase++,pi*p[1].r*p[1].r);
        }
        else
        {
            double x=(p[1].r*p[1].r+dist*dist-p[2].r*p[2].r)/(2*p[1].r*dist);
            double y=(p[2].r*p[2].r+dist*dist-p[1].r*p[1].r)/(2*p[2].r*dist);
            double s=0.5*(p[1].r*dist)*sqrt(1-x*x);
            double s1=(acos(x)/2)*(p[1].r*p[1].r);
            double s2=(acos(y)/2)*(p[2].r*p[2].r);
            double s3=(s1+s2-s)*2;
            printf("Case %d: %.10f\n",ncase++,s3);
        }
    }
    return 0;
}
0 0
原创粉丝点击