poj 2546 Circular Area

来源:互联网 发布:曲靖市罗平县经济数据 编辑:程序博客网 时间:2024/04/28 22:47

//求两圆公共部分面积, 三种情况:相交、相离、相容

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
const double Pi=acos(-1.0);
const double EP=1e-8;
struct Point{double x, y;};
struct Cir{Point c; double r;}c1, c2;
double dist(Point p1, Point p2){
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main(){
    //freopen("1.txt", "r", stdin);
    double d1, d2, d, a1, a2, area;
 while(scanf("%lf%lf%lf%lf%lf%lf", &c1.c.x, &c1.c.y, &c1.r, &c2.c.x, &c2.c.y, &c2.r)!=EOF){
 d=dist(c1.c, c2.c);
 if(d>=c1.r+c2.r-EP){
        printf("0.000\n");continue;
 }
 if(d<=c1.r-c2.r+EP){
        printf("%.3f\n", Pi*c2.r*c2.r);continue;
 }
 if(c1.r<c2.r){
        Cir t=c1;
        c1=c2;
        c2=t;
 }
 d1=(c1.r*c1.r-c2.r*c2.r)/d/2+d/2;
    d2=d-d1;
 a1=acos(d1/c1.r);
 a2=acos(d2/c2.r);
    area=a1*c1.r*c1.r+a2*c2.r*c2.r-0.5*c1.r*c1.r*sin(2*a1)-0.5*c2.r*c2.r*sin(2*a2);
 printf("%.3f\n", area);
 }
 return 0;
}