POJ2546 Circular Area(计算几何)

                                                                            Circular Area
                                                       Time Limit: 1000MS  Memory Limit: 65536K
 

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

Source

Northeastern Europe 2000, Far-Eastern Subregion
 
 
这道题直接上模板就可以了~
 
模板:
 
struct Circle{
    double x, y, r;
};
//圆的圆心坐标,半径

double dis(Circle a, Circle b){
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
//两圆圆心的距离

double solve(Circle a, Circle b){
    double d = dis(a, b);
    if (d >= a.r + b.r) return 0;
    if (d <= fabs(a.r - b.r)){
        double r = a.r < b.r ? a.r : b.r;
        return pi * r * r;
    }

    double ang1 = acos((a.r * a.r + d * d - b.r * b.r) / 2.00 / a.r / d);
    double ang2 = acos((b.r * b.r + d * d - a.r * a.r) / 2.00 / b.r / d);
    double ret = ang1 * a.r * a.r + ang2 * b.r * b.r - d * a.r * sin(ang1);
    return ret;
}
//返回值即为两圆公共部分的面积
 
 
POJ2546:
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define pi    3.1415926535897932384626
 6 
 7 struct Circle{
 8     double x, y, r;
 9 } r[31];
10 
11 double dis(Circle a, Circle b){
12     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
13 }
14 
15 double solve(Circle a, Circle b){
16     double d = dis(a, b);
17     if (d >= a.r + b.r) return 0;
18     if (d <= fabs(a.r - b.r)){
19         double r = a.r < b.r ? a.r : b.r;
20         return pi * r * r;
21     }
22 
23     double ang1 = acos((a.r * a.r + d * d - b.r * b.r) / 2.00 / a.r / d);
24     double ang2 = acos((b.r * b.r + d * d - a.r * a.r) / 2.00 / b.r / d);
25     double ret = ang1 * a.r * a.r + ang2 * b.r * b.r - d * a.r * sin(ang1);
26     return ret;
27 }
28 
29 int main(){
30 
31     while (~scanf("%lf%lf%lf%lf%lf%lf", &r[0].x, &r[0].y, &r[0].r, &r[1].x, &r[1].y, &r[1].r))
32         printf("%.3f
", solve(r[0], r[1]));
33         
34     return 0;
35 
36 }
原文地址:https://www.cnblogs.com/cxhscst2/p/6392555.html