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;
}