HDU 5120 Intersection (计算几何)2014ICPC 北京站现场赛

Intersection

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 1253    Accepted Submission(s): 487


Problem Description
Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The following figures are some famous examples you may know.


A ring is a 2-D figure bounded by two circles sharing the common center. The radius for these circles are denoted by r and R (r < R). For more details, refer to the gray part in the illustration below.


Matt just designed a new logo consisting of two rings with the same size in the 2-D plane. For his interests, Matt would like to know the area of the intersection of these two rings.
 

Input
The first line contains only one integer T (T ≤ 10 5), which indicates the number of test cases. For each test case, the first line contains two integers r, R (0 ≤ r < R ≤ 10).

Each of the following two lines contains two integers x i, y i (0 ≤ x i, y i ≤ 20) indicating the coordinates of the center of each ring.
 

Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the area of intersection rounded to 6 decimal places.
 

Sample Input
  
  
2 2 3 0 0 0 0 2 3 0 0 5 0
 

Sample Output
  
  
Case #1: 15.707963 Case #2: 2.250778
 

题意:给出两个圆环的半径和圆心,求他们重合部分的面积,听说是模板题,队友做的。

分析:计算几何。

#include <cstdio>
#include <cmath>
using namespace std;

#define PI acos(-1.0) //定义PI

struct Circle { // 定义圆
    double x, y;
    double r;
};

struct Ring { // 定义圆环
    double x, y;
    double R, r;
};

struct Get_Intersection_RingAndRing {

    //求圆心距,即两个圆心之间的距离
    double get_dis(double x1, double y1, double x2, double y2) {
        return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }

    // 求两圆的相交面积
    double get_CircleIntersectionArea(Circle c1, Circle c2) {
        double dis = get_dis(c1.x, c1.y, c2.x, c2.y);

        // 圆心距大于半径和,两圆相交面积为0
        if(dis >= c1.r + c2.r) return 0;

        double min_r = c1.r < c2.r ? c1.r : c2.r;
        double max_r = c1.r > c2.r ? c1.r : c2.r;
        if(min_r + dis <= max_r)  //圆心距小于半径之差,两圆包含关系
            return PI * min_r * min_r;

        double a = acos((c1.r * c1.r + dis * dis - c2.r * c2.r) / 2 / c1.r / dis);
        double b = acos((c2.r * c2.r + dis * dis - c1.r * c1.r) / 2 / c2.r / dis);
        double area1 = a * c1.r * c1.r; //第一个圆中扇形的面积, 弧长L=a*c1.r,面积等于0.5*L*c1.r
        double area2 = b * c2.r * c2.r; //第二个圆中扇形的面积
        double ans = area1 + area2; //两个扇形的面积和等于四边形的面积加上两圆相交的面积
        double area_qua = sin(a) * c1.r * dis; //四边形的面积
        ans -= area_qua;
        return ans;
    }

    //求圆环和圆环的相交面积
    double Get_IntersectionArea(Ring r1, Ring r2) {
        Circle a1, a2, b1, b2;
        a1.x = r1.x, a1.y = r1.y, a1.r = r1.r;
        a2.x = r1.x, a2.y = r1.y, a2.r = r1.R;
        b1.x = r2.x, b1.y = r2.y, b1.r = r2.r;
        b2.x = r2.x, b2.y = r2.y, b2.r = r2.R;
        return get_CircleIntersectionArea(a2, b2) - get_CircleIntersectionArea(a1, b2) - get_CircleIntersectionArea(a2, b1) + get_CircleIntersectionArea(a1, b1);
    }
};

int main()
{
    int t;
    Ring r1, r2;
    Get_Intersection_RingAndRing x;
    scanf("%d",&t);
    int cas=1;
    while(t--)
   {
        scanf("%lf%lf",&r1.r, &r1.R);
        r2.r = r1.r, r2.R = r1.R;
        scanf("%lf%lf",&r1.x, &r1.y);
        scanf("%lf%lf", &r2.x, &r2.y);

        printf("Case #%d: %.6lf\n", cas++, x.Get_IntersectionArea(r1, r2));
    }
    return 0;
}

题目链接: 点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值