现在的位置: 首页 > 综合 > 正文

HDOJ 5120 Intersection(求两圆相交面积)

2019年02月11日 ⁄ 综合 ⁄ 共 1557字 ⁄ 字号 评论关闭

求两个圆环相交面积,直接容斥即可。

圆环面积 = 两个大圆面积交 - 大圆和小圆交 * 2 + 两个小圆交。

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)

int sig(double x)
{
    return (x > eps) - (x < -eps);
}

typedef struct Point
{
        double x, y;
        Point() {}
        Point(double _x, double _y):
                x(_x), y(_y) {}
        Point operator -(const Point &argu) const
        {
                return Point(x - argu.x, y - argu.y);
        }
        bool operator <(const Point &argu) const
        {
                if(sig(x - argu.x) == 0)
                        return y < argu.y;
                return x < argu.x;
        }
        double dis(const Point &argu) const
        {
                return sqrt((x - argu.x) * (x - argu.x) + (y - argu.y) * (y - argu.y));
        }
}pp;

typedef struct Circle
{
    Point o;
    double r;
    Circle() {}
    Circle(Point _o, double _r):
        o(_o), r(_r) {}
    bool operator <(const Circle &argu) const
    {
        if(sig(r - argu.r) == 0)
            return o < argu.o;
        else
            return r < argu.r;

    }
    ///参数圆半径更小些
    double Intersection_Area(const Circle &argu) const
    {
        double d = ((*this).o).dis(argu.o);

        if(sig(d - r - argu.r) >= 0)
            return 0;
        if(sig(r - argu.r - d) >= 0 || sig(d) == 0)
            return pi * argu.r * argu.r;
        double ang1 = acos((argu.r * argu.r + d * d - r * r) / (2 * argu.r * d));
        double ang2 = acos((r * r + d * d - argu.r * argu.r) / (2 * r * d));
        return ang1 * argu.r * argu.r + ang2 * r * r - argu.r * d * sin(ang1);
    }
}cc;

cc c1[2], c2[2];

double calc()
{
    scanf("%lf %lf", &c1[0].r, &c1[1].r);
    c2[0].r = c1[0].r, c2[1].r = c1[1].r;

    scanf("%lf%lf", &c1[0].o.x, &c1[0].o.y);
    c1[1].o.x = c1[0].o.x, c1[1].o.y = c1[0].o.y;

    scanf("%lf%lf", &c2[0].o.x, &c2[0].o.y);
    c2[1].o.x = c2[0].o.x, c2[1].o.y = c2[0].o.y;

    return c1[1].Intersection_Area(c2[1]) - c1[1].Intersection_Area(c2[0]) - c2[1].Intersection_Area(c1[0]) + c1[0].Intersection_Area(c2[0]);
}

int main()
{
//    freopen("5120.in", "r", stdin);

    int t;
    scanf("%d", &t);
    for(int cas = 1; cas <= t; cas++)
    {
        printf("Case #%d: %.6lf\n", cas, calc());
    }
	return 0;
}

抱歉!评论已关闭.