当前位置:首页 » PHP技术

POJ 2546 || Circular Area(两圆面积交

2015-05-04 16:16 本站整理 浏览(15)

求两个圆的相交面积,扔一个模板。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
struct circle {
    double x, y, r;
    circle( double xx=0,double yy= 0):x(xx),y(yy){}
    circle operator - (const circle& b) const {
        return circle(x-b.x,y-b.y);
    }
    int pscanf(){
        return  scanf("%lf %lf %lf",&x,&y,&r);
    }
}A,B;

 // 两圆面积交
double cir_area_inst(circle A, double r1, circle B, double r2) {

    double a1, a2, d, ans;
    circle c = A - B;
    d = sqrt(c.x*c.x + c.y*c.y);

    if ( d > r1 + r2 - eps )//外离或者外切
        return 0;
    if ( d < r2 - r1 + eps )//
        return PI*r1*r1;
       if ( d < r1 - r2 + eps )
        return PI*r2*r2;
    a1 = acos((r1*r1+d*d-r2*r2)/2/r1/d);
    a2 = acos((r2*r2+d*d-r1*r1)/2/r2/d);
    ans = (a1-0.5*sin(2*a1))*r1*r1 + (a2-0.5*sin(2*a2))*r2*r2;
    return ans;

}
int main()
{
    A.pscanf();
    B.pscanf();
    double area = cir_area_inst(A,A.r,B,B.r);
    printf("%.3f\n",area);
    return 0;
}