俩圆相交面积

计算俩圆相交面积
首先计算俩圆的距离dis,分析俩圆的位置。(将俩圆用c1和c2表示)。
相离 :面积为0.
相含 :c1包含c2 或者 c2包含c1.
相交:《俩圆相交面积》
用余弦定理(a²=b²+c²-2bc(cosA))计算俩圆心处的夹角,然后计算俩圆的俩个扇形面积,减去三角形面积( 可以用S=1/2SinB*ac 和海伦公式A=sqrt (s(s-a)(s-b)(s-c)),其中 s=(a+b+c)/2 )即为俩圆相交面积。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
const double pi=acos(-1.0);
struct Circle{ 
	double x,y,r;
};
double getarea(Circle c1,Circle c2){ 
	double dis=sqrt((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y));
	if(c1.r+c2.r<=dis) return 0;
	if(c1.r-c2.r>=dis) return pi*c2.r*c2.r;
	if(c2.r-c1.r>=dis) return pi*c1.r*c1.r;
	double angle1=acos((c1.r*c1.r+dis*dis-c2.r*c2.r)/(2*c1.r*dis));
	double angle2=acos((c2.r*c2.r+dis*dis-c1.r*c1.r)/(2*c2.r*dis));
	return c1.r*c1.r*angle1+c2.r*c2.r*angle2-sin(angle1)*c1.r*dis;
}
int main(){ 
	Circle c1,c2;
	scanf("%lf%lf%lf",&c1.x,&c1.y,&c1.r);
	scanf("%lf%lf%lf",&c2.x,&c2.y,&c2.r);
	printf("%lf\n",getarea(c1,c2));
}
    原文作者:Devil Zoey
    原文地址: https://blog.csdn.net/qq_41750091/article/details/84672732
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞