HDU - 5120 Intersection(简单几何)——2014ACM/ICPC亚洲区北京站

传送门
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

题目大意:

给两个一样大的圆环,圆心坐标不同,求两个圆环相交的面积

解题思路:

在纸上画画图发现,就是:
两个大圆相交面积-大圆1与小圆2相交面积-大圆2与小圆1相交面积+两个小圆相交面积
求圆相交面积有模板,一套就好了。

代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1);
// 几何误差修正
inline int cmp(double x) {
    return x < -eps ? -1 : (x > eps);
}
// 计算x的平方
inline double sqr(double x) {
    return x * x;
}
// 开方误差修正
inline double mySqrt(double n) {
    return sqrt(max((double)0, n));
}

// 二维点(向量)类
struct Point {
    double x, y;
    Point() {}
    Point(double x, double y): x(x), y(y) {}
    void input() {
        scanf("%lf%lf", &x, &y);
    }
    friend Point operator + (const Point& a, const Point& b) {
        return Point(a.x + b.x, a.y + b.y);
    }
    friend Point operator - (const Point& a, const Point& b) {
        return Point(a.x - b.x, a.y - b.y);
    }
    friend bool operator == (const Point& a, const Point& b) {
        return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
    }
    friend Point operator * (const Point& a, const double& b) {
        return Point(a.x * b, a.y * b);
    }
    friend Point operator * (const double& a, const Point& b) {
        return Point(a * b.x, a * b.y);
    }
    friend Point operator / (const Point& a, const double& b) {
        return Point(a.x / b, a.y / b);
    }
    // 返回本向量的长度
    double norm() {
        return sqrt(sqr(x) + sqr(y));
    }
    // 返回本向量对应的单位向量
    Point unit() {
        return Point(x, y) / norm();
    }
};
//求两圆相交面积
double intersect(double x1,double y1,double r1,double x2,double y2,double r2){
    double s,temp,p,l,ans;
    l=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
    if(l>=r1+r2) ans=0;
    else if(l<=fabs(r1-r2)){
        if(r1<=r2) ans=PI*r1*r1;
        else ans=PI*r2*r2;
    }
    else{
        p=(l+r1+r2)/2;
        s=2*sqrt(p*(p-l)*(p-r1)*(p-r2));
        if(r1>r2){
            temp=x1;x1=x2;x2=temp;
            temp=y1;y1=y2;y2=temp;
            temp=r1;r1=r2;r2=temp;
        }
        ans=acos((r1*r1+l*l-r2*r2)/(2*r1*l))*r1*r1+acos((r2*r2+l*l-r1*r1)/(2*r2*l))*r2*r2-s;
    }
    return ans;
}

int main()
{
    int T; scanf("%d", &T);
    for(int cas=1; cas<=T; cas++){
        double r, R; scanf("%lf%lf", &r, &R);
        double x1, y1, x2, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
        double ans1 = intersect(x1, y1, R, x2, y2, R);
        double ans2 = intersect(x1, y1, R, x2, y2, r);
        double ans3 = intersect(x1, y1, r, x2, y2, R);
        double ans4 = intersect(x1, y1, r, x2, y2, r);
        double ans = ans1-ans2-ans3+ans4;
        printf("Case #%d: %.6f\n",cas,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值