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;
}