首页 > 代码库 > hdu 5120 (求两圆相交的面积

hdu 5120 (求两圆相交的面积

  题意:告诉你两个圆环,求圆环相交的面积。

/*  gyt
       Live up to every day            */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 605;
const ll maxm = 1e7;
const ll mod = 1e9 + 7;
const int INF = 0x3f3f3f;
const ll inf = 1e15 + 5;
const db eps = 1e-9;
int a1, b1, a2, b2;

db getArea(int a, int b) {
    db sa, sb;
    db d=sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));
    db rr=min(a, b);
    db area=0;
    if (d<=abs(a-b))  area=acos(-1.0)*rr*rr;  //内含或者内切
    else if (d>=a+b)  area=0.0;  //外切或不相交
    else {  //相交求面积
        db p=(a+b+d)/2.0;  //海伦公式里边的P
        db sa=acos((a*a+d*d-b*b)/(2.0*a*d));  //c^2=a^2+b^2-2*a*b*cos;求出圆心角
        db sb=acos((b*b+d*d-a*a)/(2.0*b*d));
        area=sa*a*a+sb*b*b-2*sqrt(p*(p-a)*(p-b)*(p-d));  //两个扇形面积和就减去三角形面积,得相交部分面积
    }
    return area;
}
void solve() {
    static int ca=1;
    int R, r;
    scanf("%d%d%d%d%d%d", &R, &r, &a1, &b1, &a2, &b2);
    db s1=getArea(R, R), s2=getArea(r, r), s3=getArea(R, r);
    db sum=0;
    if (R>r)  sum=s1-2*s3+s2;
    else    sum=s2-2*s3+s1;
    printf("Case #%d: %.6f\n", ca++, sum);
}
int main() {
    int t = 1, cas = 1;
   // freopen("in.txt", "r", stdin);
   // freopen("out.txt", "w", stdout);
    scanf("%d", &t);
    while(t--) {
        solve();
    }
    return 0;
}

 

hdu 5120 (求两圆相交的面积