HDU5120 Intersection 相交环总面积 (2014北京现场赛)
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.
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.
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
S = A大B大 - A大B小 - A小B大 + A小B小。(A表示A环,大表示大圆,B同)。然后直接套模板。
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<map>
#include<string>
#include<set>
#include<math.h>
using namespace std;
const double pi = acos(-1);
struct cyc
{
int x,y;
int r;
double ran;
double s;
double ts;
};
cyc t,c1,c2;
double d;
double dis(int x1,int y1,int x2,int y2)
{
double ans1;
double ans2;
double ans;
ans1 = (double)(x2-x1)*(x2-x1);
ans2 = (double)(y2-y1)*(y2-y1);
ans = sqrt(ans1 + ans2);
return ans;
}
int abs(int a)
{
if(a<0)
return 0 - a;
return a;
}
double findx()
{
double x;
c2.ran = acos(((c2.r*c2.r)+(d*d)-(c1.r*c1.r))/(double)(2*d*c2.r));
c1.ran = acos(((c1.r*c1.r)+(d*d)-(c2.r*c2.r))/(double)(2*d*c1.r));
x = c2.r*(double)sin(c2.ran);
return x;
}
double lwtg()//long way to go
{
double xx = findx();
c1.s = c1.r*c1.r*c1.ran;
c2.s = c2.r*c2.r*c2.ran;
c1.ts = c1.r*d*sin(c1.ran);
return 0;
}
double banana()
{
double ans;
if(c1.r<c2.r)
{
t = c1;c1 = c2;c2 = t;
}
d = dis(c1.x,c1.y,c2.x,c2.y);
// printf("dis = %.2f\n",d);
double dd = (double)c1.r + c2.r;
double dd_ = (double)abs(c1.r - c2.r);
if(d>=dd)
{
ans = 0.000000;
}
else if(d<=dd_)
{
ans=pi*(double)(c2.r*c2.r);
}
else
{
lwtg();
ans = c1.s+c2.s-c1.ts;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
for(int TT = 0;TT<T;TT++)
{
// c1.r = 0;c1.ran = 0;c1.s = 0;c1.ts = 0;c1.x = 0;c1.y = 0;
// c2.r = 0;c2.ran = 0;c2.s = 0;c2.ts = 0;c2.x = 0;c2.y = 0;
int c1x,c1y,c1r,c2x,c2y,c2r;
scanf("%d%d",&c2r,&c1r);
scanf("%d%d",&c2.x,&c2.y);
scanf("%d%d",&c1.x,&c1.y);
double ans1,ans2,ans3,ans4;
c1.r = c1r;
c2.r = c1r;
ans1 = banana();
c1.r = c2r;
c2.r = c2r;
ans2 = banana();
c1.r = c1r;
c2.r = c2r;
ans3 = banana();
c2.r = c1r;
c1.r = c2r;
ans4 = banana();
//!!!!环 !!香蕉的答案
printf("Case #%d: %.6f\n",TT+1,ans1+ans2-ans3-ans4);
}
}