Circular Area(两圆相交的面积)

Circular Area

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6682 Accepted: 2507

Description

Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of three digits after decimal point.

Input

In the single line of input file there are space-separated real numbers x1 y1 r1 x2 y2 r2. They represent center coordinates and radii of two circles.

Output

The output file must contain single real number – the area.

Sample Input

20.0 30.0 15.0 40.0 30.0 30.0

Sample Output

608.366

 

#include <iostream>
#include <cstdio>
#include <cmath>
#define Pi acos(-1) 
using namespace std;
int main() {
    double x1,x2,r1,r2,y1,y2;
    while (scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&r1,&x2,&y2,&r2)!=EOF) {
        double x=abs(x1-x2);
        double y=abs(y1-y2);
        double l=sqrt(x*x+y*y);//计算两圆心的距离 
        if (r1<r2) swap(r1,r2);
        if (r1+r2<=l)  //两圆相离或外切
        printf("0.000\n");
        else if (abs(r1-r2)>=l) 
        printf("%.3f\n",Pi*min(r1*r1,r2*r2));
        else {
            double a=r1,b=r2,c=l;
            double A=acos((c*c+b*b-a*a)/(2*c*b));
            double B=acos((c*c+a*a-b*b)/(2*c*a));
            double s=(a+b+c)/2;
            s=sqrt(s*(s-a)*(s-b)*(s-c));
            double area=a*a*B+b*b*A-2*s;
            printf("%.3f\n",area);
        }
    }
    return 0;
}

 

点赞