hdu 1798 Tell me the area geometry

Problem Description
    There are two circles in the plane (shown in the below picture), there is a common area between the two circles. The problem is easy that you just tell me the common area.
hdu <wbr>1798 <wbr>Tell <wbr>me <wbr>the <wbr>area <wbr>几何
Input
There are many cases. In each case, there are two lines. Each line has three numbers: the coordinates (X and Y) of the centre of a circle, and the radius of the circle.
Output
For each case, you just print the common area which is rounded to three digits after the decimal point. For more details, just look at the sample.
Sample Input
0 0 2
2 2 1
Sample Output

0.108
This problem is to find the area of ​​the intersection of two circles. Three situations can be discussed:
hdu <wbr>1798 <wbr>Tell <wbr>me <wbr>the <wbr>area <wbr>几何
 

                  Figure 1 Figure 2

One, two circles are separated, circumscribed, or at least one of the circles has a radius of 0: the required area is 0.
2. Two circles are inscribed and included: the area sought is the area of ​​the small circle.
3. The intersection of two circles: This situation is divided into two small cases: 1. The centers of the two circles are on the opposite side of the common chord, as shown in Figure 1; 2. The centers of the two circles are on the same side of the common chord, as shown in Figure 2. Looking at Figure 1 first, the shaded part can be divided into two bows by the common chord AB, and the areas of the two bows can be added together, that is, S (shadow) = S (sector O1AB) - S (triangle O1AB) + S (sector O2AB) -S (triangle O2AB) = S (sector O1AB) + S (sector O2AB) - S (quadrangle O1AO2B), that is, the difference between the area of ​​the two sectors and the area of ​​the four sides. Looking at Figure 2 again, the area required at this time is: S (sector O1AB) - S (triangle O1AB) + S (sector O2AB <the sector here is a sector with a central angle of 2*y>) + S (triangle O2AB) = S (Sector O1AB) + S (Sector O2AB) - S (Quadrangle O1AO2B), is also the difference between the area of ​​the two sectors and the area of ​​the four sides. Therefore these two minor cases do not need to be discussed separately. (in the figure, a is the center distance, c is the radius of the circle O1, z is the radius of the circle O2, b is the size of the angle AO1O2, y is the size of the angle AO2O1, A and B are the two ends of the common chord, and O1 and O2 are the two The center of the circle)
The following is the implementation code:
 
#include<stdio.h>
#include<math.h>
int main()
{
    double q,w,m,n,a,b,c,x,y,z,PI;
    PI=2*asin(1.0);
    while(~scanf("%lf%lf%lf",&a,&b,&c)){
        scanf("%lf%lf%lf",&x,&y,&z);
        a =sqrt((ax)*(ax)+(by)*(by)); // Calculate the distance between the center of the circle
         // If the two circles are separated, circumscribed or at least one circle radius is 0, then the sought area is 0 
        if (a>=c+z||!c||!z)x= 0 ;
         // If the two are inscribed or included, then the area sought is the small circle area 
        else  if (a<=fabs(z- c)){
             if (z>c)z= c;
            x = z * z * PI;
        }
        // If the two circles intersect, the area is solved as follows 
        else {
             // Find the half of the central angle corresponding to the common chord in the circle o1 by the law of cosines 
            b=acos((a*a+c*cz*z)/( 2 * a* c));
             // Find the half of the central angle corresponding to the common chord in the circle o2 by the cosine theorem 
            y=acos((a*a+z*zc*c)/( 2 *a* z));
             // Calculate the sector area in circle o1 
            m=b*c* ​​c;
             // Calculate the sector area in circle o2 
            n=y*z* z;
             // Calculate the triangle area 
            q=c*c* corresponding to the sector in circle o1 sin(b)* cos(b);
             // Calculate the area of ​​the triangle corresponding to the sector in circle o2 
            w=z*z*sin(y)* cos(y);
             //q+w is the area of ​​the quadrilateral in the figure, and the difference between the sum of the areas of the two sectors and the area of ​​the quadrilateral is
             // the area required. In Figure 2, y is an obtuse angle, and the calculated area w is a negative value. At this time, q+w
             // represents the difference between the areas of the two triangles, which is just the area of ​​a quadrilateral. Therefore, it is not necessary to discuss 
            x for Figures 1 and
             2 . =m+n-(q+ w);
        }
        printf("%.3f\n",x);
    }
    return 0;
}

Honestly speaking: copied from the big guy's blog >> http://blog.sina.com.cn/s/blog_69c3f0410100rh9f.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324940915&siteId=291194637