HDU5120 - Intersection

Posted rrrr-wys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5120 - Intersection相关的知识,希望对你有一定的参考价值。

HDU5120 - Intersection


做法:答案就是(A1 cap B1 - A2 cap B1 - A1 cap B2 + A2 cap B2),圆形面积交码了好久,先把两个圆转到x轴上,大的放到原点,小的放在大圆的左边,计算一下交点坐标,然后讨论即可。

#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
const double eps = 1e-10;
const double PI = acos(-1.0);
using namespace std;
int sgn(double x) {
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    return 1;
}
int n;
struct cir{
    double x,y,r;
    cir(){}
    cir(double _x,double _y,double _r) {
        x=_x; y=_y; r=_r;
    }
    void output() {
        cout << x <<‘ ‘<< y <<‘ ‘<< r << endl;
    }
}a1, a2, b1, b2;
double dis(double ax,double ay,double bx,double by) {
    return sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by));
}
double area_t(double a,double b,double c) {
    if(a+b <= c) return 0;
    if(a+c <= b) return 0;
    if(c+b <= a) return 0;
    if(abs(a-b) >= c) return 0;
    if(abs(c-b) >= b) return 0;
    if(abs(a-c) >= a) return 0;

    double p = (a+b+c)*0.5;
    return sqrt((p-a)*(p-b)*(p-c)*p);
}
void chg(cir &a, cir &b) {
    if(a.r < b.r) swap(a,b);
    double D = dis(a.x,a.y,b.x,b.y);
    a.x = a.y = 0;
    b.x = D; b.y = 0;
}
double cal(cir a, cir b) {
    chg(a, b);
    double D = dis(a.x,a.y,b.x,b.y), ans = 0;
    if(sgn(D- (a.r+b.r))>= 0) return 0;
    if(sgn(a.r-b.r-D)>=0) return PI*b.r*b.r;
    double cx = (a.r*a.r + D*D - b.r*b.r)*0.5/D;
    double cy = sqrt(a.r*a.r - cx*cx);
    if(sgn(cx - b.x) <= 0) {
        double S1 = D*cy;
        double tha = asin(cy/a.r)*2;
        double thb = asin(cy/b.r)*2;
        double Sa = a.r*a.r*tha*0.5;
        double Sb = b.r*b.r*thb*0.5;
        ans = Sa + Sb - S1;
    }
    else {
        double tha = asin(cy/a.r)*2;
        double thb = 2*PI-asin(cy/b.r)*2;
        double Sa = a.r*a.r*tha*0.5 - area_t(a.r,a.r,cy*2);
        double Sb = b.r*b.r*thb*0.5 + area_t(b.r,b.r,cy*2);
        ans = Sa + Sb;
    }
    return ans;
}
int T, CC = 0;
int main() {
    scanf("%d",&T);
    while(T--) {double r,R,tx1,tx2,ty1,ty2;
        scanf("%lf%lf",&r,&R);
        scanf("%lf%lf",&tx1,&ty1);
        scanf("%lf%lf",&tx2,&ty2);
        a1 = cir(tx1,ty1,R);
        a2 = cir(tx1,ty1,r);
        b1 = cir(tx2,ty2,R);
        b2 = cir(tx2,ty2,r);
        double ans = cal(a1,b1) - cal(a2,b1) - cal(a1,b2) + cal(a2,b2);
        printf("Case #%d: %.6f
",++CC,ans);
    }
}

以上是关于HDU5120 - Intersection的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5120 Intersection

HDU 5120 Intersection (求圆环相交面积)

两圆相交求面积 hdu5120

hdu 5120 (求两圆相交的面积

hdu 5120 (求两圆相交的面积的公式)

hdu 5852 :Intersection is not allowed! 行列式