Educational Codeforces Round 2 D. Area of Two Circles' Intersection

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dd_lucky/article/details/52489644

D. Area of Two Circles' Intersection

题目链接:点击打开链接

题意:求两个圆的相交面积;

思路: 大家都知道两圆的位置关系有五种:

①两圆外离 d>R+r 
②两圆外切 d=R+r 
③两圆相交 R-r<d<R+r(R>r) 
④两圆内切 d=R-r(R>r) 
⑤两圆内含d<R-r(R>r)

按着分类来做即可 我把外切和外离,内切和内含放在一起,方便计算;

此题不能用海伦公式来求面积,会丢失精度,还有一定要用 long daouble 型;

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
#define pi acos(-1)
struct point 
{
	ld x,y;
};
ld dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
	point a,b;
	ld r1,r2;
	while(cin>>a.x>>a.y>>r1)
	{
		cin>>b.x>>b.y>>r2;
		ld d=dis(a,b);
		//printf("%lf %lf %lf\n",fabs(r1-r2),(r1+r2),d);
		if(d>=r1+r2)
		{
			printf("0.000000\n");
		}
		else if((d>fabs(r1-r2))&&(d<(r1+r2)))
		{
			
			//double p=(r1+r2+d)/2.0;
			//double s=sqrt(p*(p-r1)*(p-r2)*(p-d));
			ld a=(r2*r2+d*d-r1*r1)/(2*r2*d);
			ld b=(r1*r1+d*d-r2*r2)/(2*r1*d);
			ld s1=acos(a) *  r2 * r2;
			ld s2=acos(b) *  r1 * r1;
			ld s=sin(acos(a))*cos(acos(a))*r2*r2+sin(acos(b))*cos(acos(b))*r1*r1;
			//cout<<(s2+s1-s)<<endl;
			printf("%.6f\n",(double) (s2+s1-s));
		
		}
		else
		{
			ld r=min(r1,r2);
			printf("%.6f\n",(double) (r*r*pi));
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/dd_lucky/article/details/52489644