首页 > 代码库 > hdu 5120 (求两圆相交的面积的公式)

hdu 5120 (求两圆相交的面积的公式)

S = A大B大 - A大B小 - A小B大 + A小B小。(A表示A环,大表示大圆,B同)。然后直接套模板,,,,

 

 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <cmath> 5 using namespace std; 6  7 const double eps = 1e-8; 8 const double PI = acos(-1.0); 9 10 int sgn(double x)11 {12     if(fabs(x) < eps) return 0;13     if(x < 0) return - 1;14     else return 1;15 }16 struct Point17 {18     double x, y;19     Point(){}20     Point(double _x, double _y)21     {22         x = _x; y = _y;23     }24     Point operator -( const Point &b) const25     {26         return Point(x - b. x, y - b. y);27     }28 29     double operator ^ (const Point &b) const30     {31         return x*b. y - y*b. x;32     }33 34     double operator * (const Point &b) const35     {36         return x*b. x + y*b. y;37     }38 39     void transXY(double B)40     {41         double tx = x,ty = y;42         x = tx* cos(B) - ty*sin(B);43         y = tx* sin(B) + ty*cos(B);44     }45 };46 47 double dist( Point a, Point b)48 {49     return sqrt((a-b)*(a- b));50 }51 52 double Ac(Point c1, double r1, Point c2, double r2)53 {54     double d = dist(c1,c2);55     if(r1 + r2 < d + eps) return 0;56     if(d < fabs(r1 - r2) + eps)57     {58         double r = min(r1,r2);59         return PI*r*r;60     }61     double x = (d*d + r1*r1 - r2*r2)/(2*d);62     double t1 = acos(x / r1);63     double t2 = acos((d - x)/r2);64     return r1*r1*t1 + r2*r2*t2 - d*r1*sin(t1);65 }66 67 int main() {68     int T; Point c1, c2;69     double ans, r, R, x1, y1, x2, y2;70     scanf("%d", &T);71     for(int cas = 1; cas <= T; ++cas) {72         scanf("%lf%lf%lf%lf%lf%lf", &r, &R, &x1, &y1, &x2, &y2);73         c1.x = x1; c1.y = y1;74         c2.x = x2; c2.y = y2;75         ans = Ac(c1, R, c2, R) - Ac(c1, R, c2, r) - Ac(c1, r, c2, R)76                 + Ac(c1, r, c2, r);77         printf("Case #%d: %.6lf\n", cas, ans);78     }79     return 0;80 }

 

hdu 5120 (求两圆相交的面积的公式)