Description
Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of three digits after decimal point.
Input
In the single line of input file there are space-separated real numbers x1 y1 r1 x2 y2 r2. They represent center coordinates and radii of two circles.
Output
The output file must contain single real number - the area.
Sample Input
20.0 30.0 15.0 40.0 30.0 30.0
Sample Output
608.366 求两园相交面积
1 #include <iostream> 2 #include <math.h> 3 #include <stdio.h> 4 using namespace std; 5 double dis(double x1, double y1, double x2, double y2) { 6 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); 7 } 8 int main() { 9 double pi = acos(-1); 10 double gx, gy, gr, wx, wy, wr; 11 while(scanf("%lf %lf %lf %lf %lf %lf", &gx, &gy, &gr, &wx, &wy, &wr) != EOF) { 12 double dd = dis(gx, gy, wx, wy); 13 if(dd >= (wr + gr) || wr == 0 || gr == 0) printf("0.000\n"); 14 else if(dd <= fabs(wr - gr)) { 15 double rr = min(wr, gr); 16 printf("%.3f\n",rr*rr*pi); 17 } else { 18 double a1 = acos((gr*gr + dd*dd - wr*wr) / (2*gr*dd)); 19 double a2 = acos((wr*wr + dd*dd - gr*gr) / (2*wr*dd)); 20 double area1 = (sin(a1*2)*gr*gr+sin(a2*2)*wr*wr)/2; 21 double area2 = gr*gr*a1 + wr*wr*a2; 22 printf("%.3f\n",(area2-area1)); 23 } 24 } 25 return 0; 26 }
时间: 2024-10-12 19:24:07