自适应辛普森积分
以其中一圆柱的中轴线为
x
轴,另一圆柱的中轴线为
那么,以
t
自变量,计算平面
ans=∫min(r1,r2)0S(t)=∫min(r1,r2)0r12−t2−−−−−−−√∗r22−t2−−−−−−−√
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
const double eps = 1e-8;
double r1, r2;
double func(double x)
{
return sqrt((r1*r1-x*x)*(r2*r2-x*x));
}
double simp(double l, double r)
{
return (func(l)+func(r)+func((l+r)/2)*4)*(r-l)/6;
}
double simpson(double l, double r)
{
double mid = (l + r)/2, fw = simp(l, r);
double fl = simp(l, mid), fr = simp(mid, r);
if(fabs(fw - fl - fr) < eps) return fw;
else return simpson(l, mid) + simpson(mid, r);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("sgu217.in","r",stdin);
freopen("sgu217.out","w",stdout);
#endif
std::cin >> r1 >> r2;
printf("%lf",simpson(0, std::min(r1,r2))*8);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}