贴一发两圆相交面积模板
1 #include<bits/stdc++.h> 2 #define pi acos(-1.0) 3 using namespace std; 4 const double eps=1e-6; 5 double _abs(double x) 6 { 7 if(x<0) return -x; 8 else return x; 9 } 10 struct point 11 { 12 point(double _x=0,double _y=0){x=_x; y=_y;} 13 double x,y; 14 }p1,p2; 15 double R; 16 double AREA(point a, double r1, point b, double r2) 17 { 18 double d=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 19 if(d>=r1+r2) return 0; 20 if(r1>r2) 21 { 22 double tmp=r1; 23 r1=r2; 24 r2=tmp; 25 } 26 if(r2-r1>=d) 27 return pi*r1*r1; 28 double ang1=acos((r1*r1+d*d-r2*r2)/(2*r1*d)); 29 double ang2=acos((r2*r2+d*d-r1*r1)/(2*r2*d)); 30 return ang1*r1*r1+ang2*r2*r2-r1*d*sin(ang1); 31 } 32 int main() 33 { 34 int T; scanf("%d",&T); 35 while(T--) 36 { 37 scanf("%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&R); 38 double l=0,r=1000000,area=pi*R*R/2; 39 while(_abs(l-r)>eps) 40 { 41 double mid=(l+r)/2; 42 if(AREA(p1,R,p2,mid)>=area) r=mid; 43 else l=mid; 44 } 45 printf("%.4f ",l); 46 } 47 return 0; 48 }