Educational Codeforces Round 2D. Area of Two Circles' Intersection(计算几何+圆相交的面积)

题目链接
题意:计算圆的相交面积
解法:模板来一发,但是坑的是,精度。用long double ,cout.precision(9);设置输出精度9位



#define CF

#ifndef CF
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#else
#include<bits/stdc++.h>
#endif // CF
using namespace std;
#define LL long long
#define pb push_back
#define X first
#define Y second
#define cl(a,b) memset(a,b,sizeof(a))
typedef pair<long long ,long long > P;
const int maxn=2*100005;
const LL inf=1LL<<50;
const LL mod=1e9+7;


typedef long double db;
const db pi=acos(-1.0);

db Deg2Rad(db deg){return (deg*pi/180.0);}
db Rad2Deg(db rad){return (rad*180.0/pi);}
db Sin(db deg){return sin(Deg2Rad(deg));}
db ArcSin(db val){return Rad2Deg(asin(val));}
db ArcCos(db val){return Rad2Deg(acos(val));}
struct circle{
    db x,y,r;
    circle(){}
    circle(db _x,db _y, db _r):x(_x),y(_y),r(_r){}
};
struct point{
    db x,y;
    point(){x=y=0;}
    point(db a,db b){
        x=a,y=b;
    }
};
point Center(const circle&c){
    return point(c.x,c.y);
}
db 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));
}
db Area(const circle& x){
    return pi*x.r*x.r;
}
db commonArea(const circle&A,const circle &B){
    db area=0.0;
    const circle&M=(A.r>B.r)?A:B;
    const circle&N=(A.r>B.r)?B:A;
    db D=Distance(Center(M),Center(N));
    if((D<M.r+N.r)&&(D>M.r-N.r)){
        db cosM=(M.r*M.r+D*D-N.r*N.r)/(2.0*M.r*D);
        db cosN=(N.r*N.r+D*D-M.r*M.r)/(2.0*N.r*D);
        db alpha=2.0*ArcCos(cosM);
        db beta= 2.0*ArcCos(cosN);
        db TM=0.5*M.r*M.r*Sin(alpha);
        db TN=0.5*N.r*N.r*Sin(beta);
        db FM=(alpha/360.0)*Area(M);
        db FN=(beta/360.0)*Area(N);
        area=FM+FN-TM-TN;
    }
    else if(D<=M.r-N.r){
        area=Area(N);
    }
    return area;
}
int main(){
    db x1,x2,y1,y2,r1,r2;
    cin>>x1>>y1>>r1;
    cin>>x2>>y2>>r2;
    circle A(x1,y1,r1),B(x2,y2,r2);
    cout.precision(9);
    cout<<commonArea(A,B)<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值