codeforces D. Area of Two Circles' Intersection 计算几何
D. Area of Two Circles' Intersection
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou are given two circles. Find the area of their intersection.
Input
The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle.
The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.
Output
Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed10 - 6.
Sample test(s)
input
0 0 4
6 0 4
output
7.25298806364175601379
input
0 0 5
11 0 5
output
0.00000000000000000000
题意:求两个圆相交面积,主要问题就是精度的问题;
解决思路:在两个圆心相距很远的时候用两个圆心和一个交点算角度的精度可能不够,可以用两个交点和一个圆心来算比较好;
AC代码:
#include <bits/stdc++.h>
#define pi acos(-1.0)
using namespace std;
long double x1,x2,y2,r1,r2,y;
long double area(){
long double d=sqrt((x1-x2)*(x1-x2)+(y-y2)*(y-y2));
if(r1>r2){
long double temp=r1;
r1=r2;
r2=temp;
}
if(r1+r2<=d) return 0;
else if(r2-r1>=d) return pi*r1*r1;
else {
long double a1=acos((r1*r1+d*d-r2*r2)/(2.0*r1*d));
long double a2=acos((r2*r2+d*d-r1*r1)/(2.0*r2*d));
a1*=2;
a2*=2;
return (a1*r1*r1+a2*r2*r2-r1*r1*sin(a1)-r2*r2*sin(a2))/2.0;
}
}
int main()
{