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.

Hdu 5120 Intersection【计算圆环相交面积】_第1张图片
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.

Hdu 5120 Intersection【计算圆环相交面积】_第2张图片
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.

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
2014ACM/ICPC亚洲区北京站-重现赛(感谢北师和上交)  


题意:

给出两个相同的圆环的内径和外径,以及圆心的坐标,求两个圆环交叉部分的面积


题解:

前几天周练,数学大神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;
}


你可能感兴趣的:(Hdu 5120 Intersection【计算圆环相交面积】)