HDU5120 (容斥原理)

题目大意:求两环的相交面积。

容斥原理,两个大圆面积之交集 - 两个大圆与另一个小圆面积之交集 + 两小圆面积之交集。直接拿模板。

#include<iostream>
#include<math.h>
#include<iomanip>

using namespace std;

const double pai=acos(-1.0);
class Circle
{
public:
    double x,y,r;
    Circle(double x1,double Y1,double R):x(x1),y(Y1),r(R){}
    double intersection(Circle b)
    {
        double dis=sqrt( (x-b.x)*(x-b.x)+(y-b.y)*(y-b.y) );
        if(dis>=r+b.r)
            return 0;
        double minr=r<b.r?r:b.r;
        double maxr=r>b.r?r:b.r;
        if(dis<=maxr-minr)
            return pai*minr*minr;
        double ang1=acos( (dis*dis+r*r-b.r*b.r)/(2*dis*r) );
        double ang2=acos( (dis*dis+b.r*b.r-r*r)/(2*dis*b.r) );
        double ans=0;
        ans-=dis*r*sin(ang1)*0.5*2;
        ans+=ang1/(pai*2)*pai*r*r*2+ang2/(pai*2)*pai*b.r*b.r*2;
        return ans;
    }
};

int main()
{
    int times;
    cin>>times;
    for(int time=1;time<=times;++time)
    {
         double r1,r2,x1,x2,y1,y2;
         cin>>r1>>r2>>x1>>y1>>x2>>y2;
         Circle a(x1,y1,r2);
         Circle A(x1,y1,r1);
         Circle B(x2,y2,r1);
         Circle b(x2,y2,r2);
        cout<<"Case #"<<time<<": ";
        cout<<fixed<<setprecision(6)<< A.intersection(B)-A.intersection(b)-B.intersection(a)+a.intersection(b)<<endl;
    }

}








你可能感兴趣的:(C++,算法,ACM,容斥原理)