hdu 5120 圆相交面积+组合数学

本文提供了一种计算两个圆环交集面积的算法实现。通过定义圆环的交集为大圆交集减去两倍的大圆与小圆交集再加上小圆交集的方式,有效地解决了圆环交集的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

圆环的交 = 大圆交-2个大小圆交+小圆交

最讨厌这些模板题!干.

<span style="font-size:14px;">#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 1000000000;
const int maxn = 1e6+5;

const double Pi=acos(-1.0);

double Rad2Deg(double rad){
    return (rad*180.0/Pi);
}

double Deg2Rad(double deg){
    return (deg*Pi/180.0);
}

double ArcCos(double val){
    return Rad2Deg(acos(val));
}

double Sin(double deg){
    return sin(Deg2Rad(deg));
}

struct CIRCLE{
    double x;
    double y;
    double r;
    CIRCLE(){}
    CIRCLE(double xx,double yy,double rr){
        x=xx; y=yy; r=rr;
    }
};

struct POINT{
    double x;
    double y;
    POINT():x(0),y(0){};
    POINT(double xx,double yy):x(xx),y(yy){    }
};

double Distance(const POINT& a,const POINT& b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

POINT Center(const CIRCLE& circle){
    return POINT(circle.x,circle.y);
}

double Area(const CIRCLE& circle){
    return Pi*circle.r*circle.r;
}

double CommonArea(const CIRCLE& A,const CIRCLE &B){
    double area=0.0;
    const CIRCLE& M=(A.r>B.r)?A:B;
    const CIRCLE& N=(A.r>B.r)?B:A;
    double D=Distance(Center(M),Center(N));
    if((D<M.r+N.r)&&(D>M.r-N.r)){
        double cosM=(M.r*M.r+D*D-N.r*N.r)/(2.0*M.r*D);
        double cosN=(N.r*N.r+D*D-M.r*M.r)/(2.0*N.r*D);
        double alpha=2.0*ArcCos(cosM);
        double beta=2.0*ArcCos(cosN);

        double TM=0.5*M.r*M.r*Sin(alpha);
        double TN=0.5*N.r*N.r*Sin(beta);
        double FM=(alpha/360.0)*Area(M);
        double FN=(beta/360.0)*Area(N);
        area=FM+FN-TM-TN;
    }else if(D<=M.r-N.r){
        area=Area(N);
    }
    return area;
}

double work()
{
    double r,R;
        double x1,y1;
        double x2,y2;
        //cin>>r>>R>>x1>>y1>>x2>>y2;
        scanf("%lf%lf%lf%lf%lf%lf",&r,&R,&x1,&y1,&x2,&y2);
        CIRCLE cr1(x1,y1,r);
        CIRCLE cr2(x2,y2,r);

        CIRCLE cR1(x1,y1,R);
        CIRCLE cR2(x2,y2,R);
        double ans=CommonArea(cR1,cR2)+CommonArea(cr1,cr2);
        ans-=CommonArea(cr1,cR2);
        ans-=CommonArea(cR1,cr2);
    return ans;
}
int main()
{
    int _,cas = 1;
    RD(_);
    while(_--){
        printf("Case #%d: %.6lf\n",cas++,work());
    }
    return 0;
}
/*

*/
</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值