Tell me the areaTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2195 Accepted Submission(s): 664
Problem Description
There are two circles in the plane (shown in the below picture), there is a common area between the two circles. The problem is easy that you just tell me the common area.
Input
There are many cases. In each case, there are two lines. Each line has three numbers: the coordinates (X and Y) of the centre of a circle, and the radius of the circle.
Output
For each case, you just print the common area which is rounded to three digits after the decimal point. For more details, just look at the sample.
Sample Input
Sample Output
Author
wangye
|
求两圆相交面积
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define eps 1e-8
#define PI acos(-1.0)
using namespace std;
int sgn(double n){
if(fabs(n)<eps)return 0;
else if(n<0)return -1;
return 1;
}
double MIN(double a,double b){
return a<b?a:b;
}
double dist(double x1,double y1,double x2,double y2){
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
double getArea(double x1,double y1,double x2,double y2,double r1,double r2){
double d=dist(x1,y1,x2,y2);
if(sgn(d-r1-r2)>=0)return 0;
if(sgn(d-fabs(r1-r2))<=0){
double r=MIN(r1,r2);
return PI*r*r;
}
double cos_r1=(d*d+r1*r1-r2*r2)/(2.0*d*r1);
double angle1=acos(cos_r1);
double cos_r2=(d*d+r2*r2-r1*r1)/(2.0*d*r2);
double angle2=acos(cos_r2);
return r1*r1*angle1-0.5*r1*r1*sin(2.0*angle1)+r2*r2*angle2-0.5*r2*r2*sin(2.0*angle2);
}
int main()
{
double x1,x2,y1,y2,r1,r2;
while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&r1,&x2,&y2,&r2)!=EOF){
printf("%.3lf\n",getArea(x1,y1,x2,y2,r1,r2));
}
return 0;
}