1. 程式人生 > >【POJ】題目 2546:Circular Area

【POJ】題目 2546:Circular Area

此題是基礎題,大致題意是給定圓心和半徑,算兩個圓的交叉面積。

經典做法,分三種情況,外離為0,內含為小圓面積,相交即為2個扇形面積 - 三角形面積。此時扇形面積是S = Θ*r*r/2.0
tip1:一般PI用此來計算 const double pi = acos(-1);
tip2:double型別的變數不要隨意進行大小比較,一般是用差值和eps進行比較。
tip3:此處計算弧度時用的是餘弦定理,acos計算出來的結果是弧度。
tip3:儘量少用sqrt函式,減少精度損失。但是這道題在計算圓心間距離時不用sqrt反而會WA,期待有大佬解釋。

#include <iostream>
#include <cmath> #include <cstdio> using namespace std; const double pi = acos(-1); const double eps = 1e-8; //const double pi = 3.141592653; int main() { double x1,y1, r1, x2, y2, r2; while(~scanf("%lf%lf%lf%lf%lf%lf", &x1,&y1,&r1,&x2,&y2,&r2)) { double
d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double rz = r1+r2; double rf = fabs(r1-r2); if(d-rz>=eps) //外離 { puts("0.000"); } else if(d-rf<=eps) //內含 { if(r1>r2) printf("%.3f\n", pi*r2*r2); else printf("%.3f\n"
, pi*r1*r1); } else{ double ang1 = acos((r1*r1+d*d-r2*r2)/2./r1/d); double ang2 = acos((r2*r2+d*d-r1*r1)/2./r2/d); double tmp = ang1*r1*r1+ang2*r2*r2-sin(ang1)*d*r1; printf("%.3f\n", tmp); } } return 0; }