球的体积并
计算几何模板,两个球的面积再减去两个球交集的面积
1 #include<cstdio> 2 #include<cmath> 3 const double Pi=acos(-1.0); 4 int main() 5 { 6 double x1,y1,z1,r1,x2,y2,z2,r2; 7 scanf("%lf%lf%lf%lf",&x1,&y1,&z1,&r1); 8 scanf("%lf%lf%lf%lf",&x2,&y2,&z2,&r2); 9 double ans1=4.0/3.0*Pi*r1*r1*r1,ans2=4.0/3.0*Pi*r2*r2*r2; 10 double dis=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)); 11 double l1=((r1*r1-r2*r2)/dis+dis)/2,l2=dis-l1; 12 double h1=r1-l1,h2=r2-l2; 13 double v1=Pi*h1*h1*(r1-h1/3),v2=Pi*h2*h2*(r2-h2/3); 14 if(dis>=r1+r2) 15 printf("%.7f\n",ans1+ans2); 16 else if(dis<=fabs(r1-r2)) 17 printf("%.7f\n",ans1>ans2 ? ans1 : ans2); 18 else 19 printf("%.7f\n",ans1+ans2-v1-v2); 20 return 0; 21 }
大佬的推导:
两圆相交到两球相交
我太难了~给个三连吧,亲~~~