There is an equilateral triangle ABC with side length 2*a. The coordinates of the triangle's vertices are A = (-a, 0), B = (a, 0) and C = (0, sqrt(3) * a). Next, there are three circles with centers at the midpoints of sides AB, BC and AC respectively and radii r1, r2 and r3 respectively. Compute the area of the intersection of the triangle and the three circles.
Input
- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains four space-separated integers a, r1, r2, r3.
Output
For each test case, print a single line containing one real number corresponding to the area of the intersection. Your answer will be considered correct if it has an absolute or relative error ≤ 5 * 10-2.
Constraints
- 1 ≤ T ≤ 20
- 1 ≤ a ≤ 50
- 1 ≤ r1, r2, r3 ≤ 200
Example
Input 5 10 5 5 5 10 6 6 7 10 12 13 14 10 15 15 15 10 19 20 18 Output 0.0000000 1.417303000 138.1789 163.712 173.2064190
Explanation
Example case 2: The black colored area shown in the figure is the intersection between the triangle and the three circles.
当时看到这个题,分析了一会,发现情况居多,觉得自己不可能敲完。 看到有人用 积分来做,还是佩服, 只要找到区间,然后直接 求得 差值,就好了, 多学习就好。
一是用来求 积分很困难,还有就是用来求根本不能积分的函数。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
const double Sqrt3=sqrt(3.0);
struct Circle{
double x,y;
double r;
Circle(double _x=0,double _y=0,double _r=0){
x=_x,y=_y,r=_r;
}
}cir[10];
double a,r1,r2,r3;
const double eps=1e-9;
double f(double x){
//写要求辛普森积分的函数,必须是 Y=f(x) 的形式
double low=0,up=Sqrt3*(a-fabs(x));
int cnt=0;
for(int i=1;i<=3;i++){
Circle& c=cir[i];
double d=c.r*c.r-(x-c.x)*(x-c.x);
if(d<-eps)return 0;
d=sqrt(d);
double y1=c.y-d,y2=c.y+d;
low=max(low,y1);
up=min(up,y2);
}
return up-low>=eps?up-low:0;
}
double simpson(double L, double R){
//三点辛普森积分法,要求f(x)是全局函数
double mid = (L + R) / 2.0;
return (f(L) + 4.0 * f(mid) + f(R)) * (R - L) / 6.0;
}
double asr(double L, double R, double eps,double ST){
//自适应辛普森积分递归过程
double mid = (L + R) / 2.0;
double SL = simpson(L, mid), SR = simpson(mid, R);
if(fabs(SL + SR - ST) <= 15.0 * eps) return SL + SR + (SL + SR - ST) / 15.0;//直接返回结果
return asr(L, mid, eps/2.0,SL) +asr(mid, R, eps/2.0,SR);//对半划分区间
}
//调用格式
//asr(l,r,EPS,simpson(l,r))
double query_l(double r){
double l=-a,mid;
while(r-l>=eps){
mid=(l+r)/2;
if(f(mid)>=eps){
r=mid;
}
else{
l=mid+eps;
}
}
return r;
}
double query_r(double l){
double r=a,mid;
while(r-l>=eps){
mid=(l+r)/2;
if(f(mid)>=eps){
l=mid;
}
else{
r=mid-eps;
}
}
return l;
}
/*
5
10 5 5 5
10 6 6 7
10 12 13 14
10 15 15 15
10 19 20 18
*/
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%lf %lf %lf %lf",&a,&r1,&r2,&r3);
cir[1]=Circle(0,0,r1);
cir[2]=Circle(a/2,Sqrt3*a/2,r2);
cir[3]=Circle(-a/2,Sqrt3*a/2,r3);
double l=-a,r=a;
for(l=-a;l<=a;l+=a/100000){//枚举解
if(f(l)>eps){
l=query_l(l);
r=query_r(l);
break;
}
}
//printf("l:%.2f r:%.2f\n",l,r);
printf("%.10f\n",asr(l,r,eps,simpson(l,r)));
}
return 0;
}