hdu 5120 Intersection(两圆相交面积)(模板)

Intersection

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5120

解题思路:

在纸上画画图,不难发现,根据容斥定理:所要求的面积为两外环的交集(ans1)-外环和内环的交集(ans2*2,对称)+两内环的

交集(多减去的部分,现在补上)。

http://blog.csdn.net/piaocoder/article/details/41649089

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const double eps = 1e-6;
const double pi = acos(-1.0);
struct Point{
    double x,y;
    Point(double x = 0,double y = 0):x(x),y(y){} // 构造函数,方便代码编写
};

double dis(Point p1,Point p2){
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

double area(Point c1,double r1,Point c2,double r2){
    double d = dis(c1,c2);
    if (r1+r2 < d+eps)
        return 0;//相离
    if (d < fabs(r1-r2)+eps){//内含
        double r = min(r1,r2);
        return pi*r*r;
    }
    double x = (d*d+r1*r1-r2*r2)/(2*d);
    double t1 = acos(x/r1);
    double t2 = acos((d-x)/r2);
    return r1*r1*t1+r2*r2*t2-d*r1*sin(t1);//相交
}

int main(){
    int T,t = 1;
    scanf("%d",&T);
    while(T--){
        Point p1,p2;
        double r1,r2;
        scanf("%lf%lf%lf%lf%lf%lf",&r1,&r2,&p1.x,&p1.y,&p2.x,&p2.y);
        double ans1 = area(p1,r2,p2,r2);
        double ans2 = area(p1,r1,p2,r2);
        double ans3 = area(p1,r1,p2,r1);
        printf("Case #%d: %.6lf\n",t++,ans1-2*ans2+ans3);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值