HDU - 3264

来源:互联网 发布:记忆小游戏知乎 编辑:程序博客网 时间:2024/05/05 13:19

求两个圆相交面积

1:相离   -> 0

2: 内含 -> min(R,r) * min(R,r) *PI

3: 相交 -> 锲形面积  -> 两个扇形减去两个三角形

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>


using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
struct cir
{
    double x, y, r;
} sta[30];


double dis(cir a, cir b)
{
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}


double get_inarea(cir a, cir b)
{
    double len = dis(a, b);
    if(len >= a.r + b.r) return 0;
    else if(len <= fabs(a.r - b.r))
    {
        double r = min(a.r, b.r);
        return r*r*PI;
    }
    else
    {
        double res = 0.0;
        double angle = acos((a.r*a.r - b.r*b.r + len*len)/(2.0*a.r*len))*2.0;
        double s1 = a.r*a.r*PI*(angle/(2.0*PI));
        double s2 = a.r*a.r*sin(angle)/2.0;
        res += s1 - s2;
        angle = acos((-a.r*a.r + b.r*b.r + len*len)/(2.0*len*b.r))*2.0;
        s1 = b.r*b.r*PI*(angle/(2.0*PI));
        s2 = b.r*b.r*sin(angle)/2.0;
        res += s1 - s2;
        return res;
    }
}


bool cal( int pos, double r,int n)
{
    cir res = sta[pos];
    res.r = r;
    for( int i = 0; i < n; i++)
        if(get_inarea(res, sta[i])*2.0 < sta[i].r*sta[i].r*PI) return false;
    return true;
}


void solve(int n)
{
    double ans = 100000000.0;
    for( int i = 0; i < n; i++)
    {
        double l = 0, r = 0;
        double mid;


        for( int j = 0; j < n; j++)
            r = max(r, dis(sta[i], sta[j]) + sta[i].r);


            while(fabs(l-r) >= eps)
            {
                mid = (l + r)/2.0;
                if(cal(i, mid, n))
                    r = mid;
                else
                    l = mid + eps;
            }
       // cout<<mid<<endl;
        ans = min(ans, mid);
    }
    printf("%.4f\n",ans);
}
int main()
{
    int t, n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for( int i = 0; i < n; i++)
            scanf("%lf%lf%lf",&sta[i].x, &sta[i].y, &sta[i].r);
        solve(n);
    }
    return 0;
}

0 0
原创粉丝点击