Circular Area
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5272 | Accepted: 2100 |
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
套模板求解两圆相交的面积
code:<span style="font-size:18px;">#include<iostream>
#include<stdio.h>
#include<cmath>
#include<cstring>
using namespace std;
const double pi = 3.141592653;
int flag;
double solve( double r, double r0, double dis )
{
double ang = 2*acos( (r*r + dis*dis - r0*r0) / (2*r*dis ) );//公式
double s2 = 0.5*r*r*sin(ang);
double s1 = 0.5*r*r*ang;
return s1 - s2;
}
int main( )
{
double x,y,r,x0,y0,r0,dis,ans;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while( scanf( "%lf%lf%lf%lf%lf%lf",&x,&y,&r,&x0,&y0,&r0 ) != EOF )
{
flag=0;
dis = sqrt( ( x - x0 )*( x - x0 ) + ( y - y0 )*( y - y0 ) );
if( r < r0 )
swap(r,r0);
if(dis>=(r+r0))
printf("0.000\n");
else if(dis+r0<=r)
printf("%.3lf\n",pi*r0*r0);
else
printf("%.3lf\n",solve(r,r0,dis)+solve(r0,r,dis));
}
return 0;
}</span>