几何模板

俩圆环的相交面积(包含俩圆相交面积)


代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
//#define PI 3.141592653
using namespace std;
double r,R;
const double eps = 1e-8;
const double pi = acos(-1.0);
int sgn(double x)
{
    if(fabs(x) < eps) return 0;
    if(x < 0) return - 1;
    else return 1;
}
struct Point
{
    double x, y;
    Point() {} Point(double _x, double _y)
    {
        x = _x;
        y = _y;
    } Point operator -( const Point &b) const
    {
        return Point(x - b. x, y - b. y);
    } //叉积
    double operator ^ (const Point &b) const
    {
        return x*b. y - y*b. x;    //点积
    }
    double operator * (const Point &b) const
    {
        return x*b. x + y*b. y;    //绕原点旋转角度B(弧度值),后x,y的变化
    }
    void transXY(double B)
    {
        double tx = x,ty = y;
        x = tx* cos(B) - ty*sin(B);
        y = tx* sin(B) + ty*cos(B);
    }
}; //*两点间距离
double dist( Point a, Point b)
{
    return sqrt((a-b)*(a- b));    //两个圆的公共部分面积
}
double inarea(Point c1, double r1, Point c2, double r2)
{
    double d = dist(c1,c2);
    if(r1 + r2 < d + eps) return 0;
    if(d < fabs(r1 - r2) + eps)
    {
        double r = min(r1,r2);
        return pi*r*r;
    }
    double x = (d*d + r1*r1 - r2*r2)/(2*d);
    double t1 = acos(x / r1);
    double t2 = acos((d - x)/r2);
    return r1*r1*t1 + r2*r2*t2 - d*r1*sin(t1);
}
int main()
{
    int t,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%lf",&r,&R);
        Point a,b;
        scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
        double res1=inarea(a,R,b,R);
        double res2=inarea(a,r,b,r);
        double res3=inarea(a,r,b,R);
        double res=res1-res3*2+res2;
        printf("Case #%d: %.6lf\n",++cnt,res);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值