Circular Area
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5682 | Accepted: 2225 |
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
///题意:两个圆之间的相交面积 #include<stdio.h> #include<iostream> #include<string.h> #include <stdlib.h> #include<math.h> #include<algorithm> using namespace std; const int N = 2; const double pi = atan(1.0)*4; struct Circle{ double x,y,r; }c[N]; typedef Circle Point; double dis(Point a, Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } ///两圆相交面积模板 double Two_Circle_Area(Circle a,Circle b){ double d = dis(a,b); if(a.r+b.r<d){ ///相离 return 0; } if(fabs(a.r-b.r)>=d){ ///内含 double r = min(a.r,b.r); return pi*r*r; } double angleA = acos((a.r*a.r+d*d-b.r*b.r)/2/a.r/d); double angleB = acos((b.r*b.r+d*d-a.r*a.r)/2/b.r/d); double area1 = a.r*a.r*angleA; ///扇形面积公式 S = r*r*圆心角 / 2; double area2 = b.r*b.r*angleB; return area1+area2-a.r*d*sin(angleA); } int main(){ while(scanf("%lf%lf%lf%lf%lf%lf",&c[0].x,&c[0].y,&c[0].r,&c[1].x,&c[1].y,&c[1].r)!=EOF){ printf("%.3lf ",Two_Circle_Area(c[0],c[1])); } return 0; }