HDOJ Tell me the area 1798

2154 ワード

Tell me the area
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1954    Accepted Submission(s): 591
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

   
   
   
   
0 0 2 2 2 1

 
Sample Output

   
   
   
   
0.108

 
Author
wangye
 
Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(4)
 
Recommend
wangye   |   We have carefully selected several similar problems for you:   1797  1793  1796  1795  1794 
#include<stdio.h>
#include<math.h>
int main()
{
	double x1,y1,r1,x2,y2,r2;
	while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&r1,&x2,&y2,&r2)!=EOF)
	{
		double d,a,b,s1,s2,s,tri1,tri2,x;
		d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
		if(d>=(r1+r2)||!r1||!r2) //          。
		s=0; //       %.3lf   0.000。 
		else if(d<=fabs(r1-r2))
		{
		x=r1<r2?r1:r2;
		s=x*x*acos(-1.0);		//acos(-1.0),     double 。	
		}
		else 
		{
		a=acos((d*d+r1*r1-r2*r2)/(2*d*r1));
		b=acos((d*d+r2*r2-r1*r1)/(2*d*r2));
		s1=a*r1*r1; //S = θ/2*r*r. 
		s2=b*r2*r2;
//		tri1=r1*d*sin(a);	//      。
//		s=s1+s2-tri1;
		tri1=r1*cos(a)*r1*sin(a);
		tri2=r2*cos(b)*r2*sin(b);	
		s=(s1+s2)-(tri1+tri2);	
		}
		printf("%.3lf
",s); } return 0; }