1. 程式人生 > >HDU 5120 Intersection 求兩圓相交面積模板

HDU 5120 Intersection 求兩圓相交面積模板

這題容斥原理求覆蓋面積就行

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#define maxn 2200005
#define LL long long
#define lson i<<1
#define rson (i<<1)+1
using namespace std;
int r,R,x11,y11,x22,y22;
//這個函式返回兩個圓的相交面積
double cal(int
r,int x1,int y1,int R,int x2,int y2) { if((x11-x22)*(x11-x22)+(y11-y22)*(y11-y22)>=(r+R)*(r+R))return 0.0; if(r>=R) { swap(r,R); swap(x11,x22); swap(y11,y22); } if(sqrt((x11-x22)*(x11-x22)+(y11-y22)*(y11-y22))+r<=R)return acos(-1.0
)*r*r; if((x11-x22)*(x11-x22)+(y11-y22)*(y11-y22)>=R*R) { double area = 0.0; int ed = (x11-x22)*(x11-x22)+(y11-y22)*(y11-y22); double jiao = ((double)R*R+ed-r*r)/(2.0*R*sqrt((double)ed)); jiao = acos(jiao); jiao*=2.0; area+=R*R
*jiao/2; jiao = sin(jiao); area-=R*R*jiao/2; jiao = ((double)r*r+ed-R*R)/(2.0*r*sqrt((double)ed)); jiao = acos(jiao); jiao*=2; area+=r*r*jiao/2; jiao = sin(jiao); area-=r*r*jiao/2; return area; } double area = 0.0; int ed = (x11-x22)*(x11-x22)+(y11-y22)*(y11-y22); double jiao = ((double)r*r+ed-R*R)/(2.0*r*sqrt(ed)); jiao = acos(jiao); area+=r*r*jiao; jiao = ((double)R*R+ed-r*r)/(2.0*R*sqrt(ed)); jiao =acos(jiao); area+= R*R*jiao-R*sqrt(ed)*sin(jiao); return area; } int main() { int t,i1 = 1,n,m; scanf("%d",&t); while(t--) { scanf("%d%d%d%d%d%d",&r,&R,&x11,&y11,&x22,&y22); double sum = 0.0; sum+=cal(R,x11,y11,R,x22,y22); sum-=cal(R,x11,y11,r,x22,y22); sum-=cal(r,x11,y11,R,x22,y22); sum+=cal(r,x11,y11,r,x22,y22); printf("Case #%d: ",i1); i1++; printf("%.6f\n",sum); } return 0; }