首页 > 代码库 > HDU5120 Intersection(求 圆交)

HDU5120 Intersection(求 圆交)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5120


题目大意:

给定两个圆环 求相交部分的面积


分析:

画一个图,然后看一看分析下就可以得到结论  ans=  两个大圆相交的部分 +两个小圆相交的部分 - 一大一小圆相交的部分


代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

const double pi = acos(-1.0);

struct point
{
    double x,y,r;
};

double S(point a)
{
    return pi*a.r*a.r;
}

double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double calu(point a,point b)
{
    point tmpa=a,tmpb=b;
    if(tmpa.r<tmpb.r) swap(tmpa,tmpb);
    double area = 0;
    double dd = dis(tmpa,tmpb);
    if(dd>tmpa.r-tmpb.r&&dd<tmpa.r+tmpb.r){
        double cos1 = (tmpa.r*tmpa.r+dd*dd-tmpb.r*tmpb.r)/(2*tmpa.r*dd);
        double cos2 = (tmpb.r*tmpb.r+dd*dd-tmpa.r*tmpa.r)/(2*tmpb.r*dd);
        double th1 = 2*acos(cos1);
        double th2 = 2*acos(cos2);
        double s1 = 0.5*tmpa.r*tmpa.r*sin(th1);
        double s2 = 0.5*tmpb.r*tmpb.r*sin(th2);
        double s3 = (th1/2)*tmpa.r*tmpa.r;
        double s4 = (th2/2)*tmpb.r*tmpb.r;
        area = s3+s4-s1-s2;
    }
    else if(dd<=tmpa.r-tmpb.r)
        area = S(tmpb);
    return area;
}

int main()
{
    int t,cas=1;
    double r,R,a,b,c,d;
    scanf("%d",&t);
    while(t--){
        point p[4];
        scanf("%lf%lf",&r,&R);
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        p[0].x=a,p[0].y=b,p[0].r=r;
        p[1].x=a,p[1].y=b,p[1].r=R;
        p[2].x=c,p[2].y=d,p[2].r=r;
        p[3].x=c,p[3].y=d,p[3].r=R;
        double s1 = calu(p[0],p[2]);
        double s2 = calu(p[0],p[3]);
        double s3 = calu(p[1],p[2]);
        double s4 = calu(p[1],p[3]);
        printf("Case #%d: %.6lf\n",cas++,s4+s1-s2-s3);
    }
    return 0;
}


HDU5120 Intersection(求 圆交)