hdu 5120(求两个圆环相交的面积 2014北京现场赛 I题)

时间:2021-12-03 21:30:41

两个圆环的内外径相同 给出内外径 和 两个圆心 求两个圆环相交的面积

画下图可以知道 就是两个大圆交-2*小圆与大圆交+2小圆交

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 <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <string>
# include <cmath>
# include <queue>
# include <list>
# define LL long long
using namespace std ; const double eps = 1e-;
const double PI = acos(-1.0); struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
//绕原点旋转角度B(弧度值),后x,y的变化
void transXY(double B)
{
double tx = x,ty = y;
x = tx*cos(B) - ty*sin(B);
y = tx*sin(B) + ty*cos(B);
}
}; //*两点间距离
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
} //两个圆的公共部分面积
double Area_of_overlap(Point c1,double r1,Point c2,double r2)
{
double d = dist(c1,c2);
if(r1 + r2 < d + eps)return ;
if(d < fabs(r1 - r2) + eps)
{
double r = min(r1,r2);
return PI*r*r;
}
double x = (d*d + r1*r1 - r2*r2)/(*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()
{
//freopen("in.txt","r",stdin) ;
int T ;
scanf("%d" , &T) ;
int Case = ;
while(T--)
{
Case++ ;
double r , R ;
double x1 , y1 , x2 , y2 ;
scanf("%lf%lf%lf%lf%lf%lf" , &r , &R , &x1 , &y1 , &x2 , &y2) ;
Point c1(x1 ,y1) ;
Point c2(x2 ,y2) ;
double ans = ;
ans = Area_of_overlap(c1,R,c2,R) + Area_of_overlap(c1,r,c2,r);
ans -= Area_of_overlap(c1,r,c2,R) ;
ans -= Area_of_overlap(c1,R,c2,r) ;
printf("Case #%d: %.6lf\n" , Case , ans) ; }
return ;
}