POJ——2546

  题目链接:http://poj.org/problem?id=2546

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

  大意是给出两圆心,及圆的半径,求两个圆相交部分的面积,求解关键是对分好case进行处理

Accepted:

 1 #include <iostream>
 2 #include <math.h>
 3 #include <iomanip>
 4 #define Pi 3.1415926
 5 
 6 using namespace std;
 7 typedef struct Point
 8 {
 9     float x, y;
10 }Point;
11 typedef struct Circle
12 {
13     Point p;
14     float r;
15 }Circle;
16 double Dis(Point p1, Point p2);
17 double CArea( Circle c1, Circle c2);
18 int main()
19 {
20     float x1, y1, r1, x2, y2, r2;
21     cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;
22     double s = 0;
23     Circle c1, c2;
24     Point p1, p2;
25     if(r1 <= r2)
26     {
27         p1.x = x1, p1.y = y1, c1.p = p1, c1.r = r1;
28         p2.x = x2, p2.y = y2, c2.p = p2, c2.r = r2;
29     }
30     else
31     {
32         p1.x = x2, p1.y = y2, c1.p = p1, c1.r = r2;
33         p2.x = x1, p2.y = y1, c2.p = p2, c2.r = r1;
34     }
35 
36     s = CArea(c1, c2);
37     cout << setiosflags(ios::fixed)<<setprecision(3)<< s << endl;
38     return 0;
39 }
40 double CArea( Circle c1, Circle c2)
41 {
42     double s = 0;
43     double x = 0, y = 0, h = 0;
44     double alpha = 0, beta = 0;
45     double d = Dis(c1.p, c2.p);
46     if(d >= (c1.r + c2.r))
47     {
48         s = 0;
49         //cout << "case 1" << endl;
50     }
51     else if(c2.r >= (c1.r + d))
52     {
53         s = Pi*c1.r*c1.r;
54         //cout << "case 2" << endl;
55     }
56     else if(d >= sqrt(pow(c2.r, 2) - pow(c1.r, 2)) && d < c2.r + c1.r)
57     {
58         x = (pow(c2.r, 2) - pow(c1.r, 2) + pow(d, 2)) / (2*d);
59         h = sqrt(pow(c2.r, 2) - pow(x, 2));
60         alpha = asin(h/c1.r);
61         beta = asin(h/c2.r);
62         s = alpha*pow(c1.r, 2) + beta*pow(c2.r, 2) - d*h;
63         //cout << "case 3" << endl;
64     }
65     else if(d <= sqrt(pow(c2.r, 2) - pow(c1.r, 2)) && d >= c2.r - c1.r)
66     {
67 
68         y = (pow(c2.r, 2) - pow(c1.r, 2) - pow(d, 2)) /(2*d);
69         h = sqrt(pow(c1.r, 2) - pow(y, 2));
70         alpha = asin(h/c1.r);
71         beta = asin(h/c2.r);
72         s = (Pi - alpha)*pow(c1.r, 2) + h*y + beta*pow(c2.r, 2) - h*(d+y);
73         //cout << "case 4" << endl;
74     }
75     return s;
76 }
77 double Dis(Point p1, Point p2)
78 {
79     return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
80 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值