标签:
已知两圆圆心坐标和半径,求相交部分面积:
1 #include <iostream>
2 using namespace std;
3 #include<cmath>
4 #include<stdio.h>
5 #define PI 3.141593
6 struct point//点
7 {
8 double x,y;
9 };
10 struct circle//圆
11 {
12 point center;
13 double r;
14 };
15 float dist(point a,point b)//求圆心距
16 {
17 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
18 }
19
20 int main()
21 {
22 circle a,b;
23 while(cin>>a.center.x>>a.center.y>>a.r>>b.center.x>>b.center.y>>b.r)
24 {
25 if((dist(a.center,b.center)+min(a.r,b.r))<=max(a.r,b.r))//两圆内交或包含
26 {
27 if(a.r<b.r) printf("%.3lf\n",PI*a.r*a.r);
28 else printf("%.3lf\n",PI*b.r*b.r);
29 }
30 else if(dist(a.center,b.center)>=(a.r+b.r))//两圆外交或相离
31 cout<<"0.000"<<endl;
32 else
33 {
34 double length=dist(a.center,b.center);
35 double d1=2*acos((a.r*a.r+length*length-b.r*b.r)/(2*a.r*length)); //求两扇形圆心角
36 double d2=2*acos((b.r*b.r+length*length-a.r*a.r)/(2*b.r*length));
37 double area1=a.r*a.r*d1/2-a.r*a.r*sin(d1)/2;
38 //根据圆心角求扇形面积,减去三角形面积,得到相交部分面积
39 //扇形面积:S=PI*r*r*θ/(2*PI) 三角形面积:S=1/2*a*c*sin(B)
40 double area2=b.r*b.r*d2/2-b.r*b.r*sin(d2)/2;
41 double area=area1+area2;
42 printf("%.3lf\n",area);
43 }
44 }
45 return 0;
46 }
标签:
原文地址:http://www.cnblogs.com/loki1412/p/4970324.html