HDU - 1798 Tell me the area【圆与圆求面积交】

HDU - 1798  Tell me the area

题意:求圆与圆面积交

#include<cstdio>
#include<vector>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1005;
const int MOD=1e9+7;
const double EPS=1e-10;
int sign(double x) {  //三态函数,减少精度问题
    return abs(x) < EPS ? 0 : x < 0 ? -1 : 1;
}
struct Point { //点的定义
    double x, y;
    Point(double x=0.0, double y=0.0) : x(x), y(y) {}
    Point operator + (const Point &rhs) const {  //向量加法
        return Point(x + rhs.x, y + rhs.y);
    }
    Point operator - (const Point &rhs) const {  //向量减法
        return Point(x - rhs.x, y - rhs.y);
    }
    Point operator * (double p) const {  //向量乘以标量
        return Point(x * p, y * p);
    }
    Point operator / (double p) const {  //向量除以标量
        return Point(x / p, y / p);
    }
    bool operator < (const Point &rhs) const {  //点的坐标排序
        return x < rhs.x || (x == rhs.x && y < rhs.y);
    }
    bool operator == (const Point &rhs) const {  //判断同一个点
        return sign(x - rhs.x) == 0 && sign(y - rhs.y) == 0;
    }
};
struct Circle {  //圆的定义
    Point c;  //圆心
    double r;  //半径
    Circle() {}
    Circle(Point c, double r) : c(c), r(r) {}
    Point point(double a) {  //圆上的一点
        return Point(c.x+cos(a)*r, c.y+sin(a)*r);
    }
};
typedef Point Vector;
Point p[N];

double dot(Vector A,Vector B){
    return A.x*B.x+A.y*B.y;
}
double length(Vector A){
    return sqrt(dot(A,A));
}
double cross(Vector A,Vector B){
    return A.x*B.y-A.y*B.x;
}
double Angle(Vector A,Vector B){
    double t=dot(A,B)/length(A)/length(B);
    return acos(t);
}

double cosine(double a, double b, double c) {  //输入三角形的三条边,角C的余弦值
    return (a*a+b*b-c*c)/2.0/a/b;
}
//两圆相交求相交面积,返回面积值
double cir_cir_inter_area(Circle C1, Circle C2) {
    double dist = length(C1.c-C2.c);
    if (sign(C1.r + C2.r - dist) < 0 ) {return 0.0;}  //相离
    else if (sign(abs(C1.r-C2.r)-dist) >= 0) {  //内含
        int r = C2.r;
        return PI*r*r;
    } else {  //相交
        double ang1 = 2.0 * acos(cosine(C1.r, dist, C2.r));
        double ang2 = 2.0 * acos(cosine(C2.r, dist, C1.r));
        double ret = (C1.r)*C1.r*ang1/2 + (C2.r)*C2.r*ang2/2 - C1.r*(C1.r)*sin(ang1)/2 - C2.r*(C2.r)*sin(ang2)/2;  //面积容斥
        return ret;
    }
}


int main(void){
    double x1,y1,r1,x2,y2,r2;
    while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&r1,&x2,&y2,&r2)==6){
        Circle C1=Circle({x1,y1},r1);
        Circle C2=Circle({x2,y2},r2);
        if(sign(r1-r2)<0)   swap(C1,C2);//保证C1是半径大的圆
        double ans=cir_cir_inter_area(C1, C2);
        printf("%.3f\n",ans);
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值