题目链接: 传送门
Circular Area
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5999 | Accepted: 2336 |
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 <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 2*1e6+100;
const int INF = 0x3fffffff;
const int mod = 1e9+7;
const double eps = 1e-8;
const double Pi = acos(-1.0);
struct Circle{
double x,y,r;
};
double insectArea(Circle a,Circle b)
{
double dis = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
double r = min(a.r,b.r);
if(a.r+b.r <= dis)
return 0.0; //相离
else if(fabs(a.r-b.r) >= dis)
return Pi*r*r; //内含
else{
double ang1 = acos((a.r*a.r+dis*dis-b.r*b.r)/2.0/a.r/dis);
double ang2 = acos((b.r*b.r+dis*dis-a.r*a.r)/2.0/b.r/dis);
double ret = ang1*a.r*a.r+ang2*b.r*b.r-dis*a.r*sin(ang1);
return ret; //相交
}
}
int main()
{
Circle c1,c2;
while(~scanf("%lf%lf%lf%lf%lf%lf",&c1.x,&c1.y,&c1.r,&c2.x,&c2.y,&c2.r)){
printf("%.3lf\n",insectArea(c1,c2));
}
return 0;
}
一直不知道下面的code为啥错了,望大佬指出:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 2*1e6+100;
const int INF = 0x3fffffff;
const int mod = 1e9+7;
const double Pi = acos(-1.0);
struct Circle{
double x,y,r;
};
double insectArea(Circle a,Circle b)
{
double dis = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
double r = min(a.r,b.r);
if(a.r+b.r <= dis)
return 0.0; //相离
else if(fabs(a.r-b.r) >= dis)
return Pi*r*r; //内含
else{
double o1 = 2.0*acos((b.r*b.r+dis*dis-a.r*a.r)/(2.0*b.r*dis));
double s1 = o1*b.r*b.r/2.0;
s1 = s1-b.r*b.r*sin(o1)/2.0;
double o2 = 2.0*acos((a.r*a.r+dis*dis-b.r*b.r)/(2.0*a.r*dis));
double s2 = o2*a.r*a.r/2.0;
s2 = s2-a.r*a.r*sin(o2)/2.0;
return s1+s2; //相交
}
}
int main()
{
Circle c1,c2;
while(~scanf("%lf%lf%lf%lf%lf%lf",&c1.x,&c1.y,&c1.r,&c2.x,&c2.y,&c2.r)){
printf("%.3lf\n",insectArea(c1,c2));
}
return 0;
}