1. 程式人生 > >Hdu 5120 Intersection【計算圓環相交面積】

Hdu 5120 Intersection【計算圓環相交面積】

Intersection

Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 1861 Accepted Submission(s): 709
Problem 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.
Input The first line contains only one integer T (T ≤ 105
), 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 xi, yi (0 ≤ xi, yi ≤ 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
Source

題意:

給出兩個相同的圓環的內徑和外徑,以及圓心的座標,求兩個圓環交叉部分的面積

題解:

前幾天周練,數學大神1A此題,賽後,個人進行嘗試,測試樣例都過不去......真心比不起啊......

一個函式,引數傳遞是兩個圓的半徑,以及兩個圓的圓心座標,返回兩個圓的交叉部分面積

然後就是容斥原理的一部分了:

S=S(兩個外環大圓相交部分)-2* S(一個的外環和另一個的內環的相交面積)+S(兩個內環的圓的相交部分)

好久沒接觸過幾何題,感覺自己好水.......

/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cmath>
#include<algorithm> 
using namespace std;
const double pi=acos(-1.0);
struct node
{
	double x,y;
};
double dis(node a,node b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double circle_cross(double r1,double r2,node a,node b)
{
	double d=dis(a,b);//兩圓的間距
	if(r1<r2)//保證大圓在前 
	{
		swap(r1,r2);
	}
	if(d>=r1+r2)
	{
		return 0;
	}
	if(d<=r1-r2)
	{
		return pi*r2*r2;
	}
	double deg1=acos((r1*r1+d*d-r2*r2)/(2*r1*d));
	double deg2=acos((r2*r2+d*d-r1*r1)/(2*r2*d));
	return deg1*r1*r1+deg2*r2*r2-r1*sin(deg1)*d;
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int k=1;k<=t;++k)
	{
		int r,R;
		node p[5];
		scanf("%d%d",&r,&R);
		for(int i=1;i<=2;++i)
		{
			scanf("%lf%lf",&p[i].x,&p[i].y);
		}
		double d=dis(p[1],p[2]);
		double a=circle_cross(R,R,p[1],p[2]);
		double b=circle_cross(R,r,p[1],p[2]);
		double c=circle_cross(r,r,p[1],p[2]);
		double ans=a+c-2*b;
		printf("Case #%d: %.6f\n",k,ans);
	}
	return 0;
}