Circular Area
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5716 | Accepted: 2239 |
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
题意:给第一个圆的圆心坐标和半径,再给第二个圆的圆心坐标和半径,求两个圆的相交面积。
几何题,刚开始接触,照着别人代码理解完成自己的代码。
两种情况一次分析就好,情况相同~
附上代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #define ll long long 6 #define M 100005 7 using namespace std; 8 9 const double pi = acos(-1.0); 10 11 struct Round 12 { 13 double x,y; 14 double r; 15 } rr[2]; 16 17 double dis(Round a, Round b) 18 { 19 return sqrt((a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y)); ///求两点之间的距离 20 } 21 22 double solve(Round a, Round b) 23 { 24 double d = dis(a, b); 25 if(d >= a.r + b.r) ///相离的情况 26 return 0; 27 else if(d <= fabs(a.r - b.r)) ///内含的情况 28 { 29 double r = a.r < b.r?a.r : b.r; 30 return pi*r*r; 31 } 32 double ang1 = acos((a.r * a.r + d * d - b.r * b.r) / 2.0 / a.r / d); 33 ///公式: cos(A)=(b^2+c^2-a^2)/2bc acos(cos(A))求出角A的弧度 34 double ang2 = acos((b.r * b.r + d * d - a.r * a.r) / 2.0 / b.r / d); 35 double ret = ang1 * a.r * a.r + ang2 * b.r * b.r - d * a.r * sin(ang1); 36 ///扇形面积s=弧度*r^2/2 三角形面积=a*b*sin(A)/2 37 return ret; 38 } 39 40 int main() 41 { 42 while(~scanf("%lf%lf%lf%lf%lf%lf",&rr[0].x,&rr[0].y,&rr[0].r,&rr[1].x,&rr[1].y,&rr[1].r)) 43 { 44 printf("%.3lf ",solve(rr[0], rr[1])); 45 } 46 return 0; 47 }