其他分享
首页 > 其他分享> > 【两球体积并】

【两球体积并】

作者:互联网

#include <iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
using namespace std;
#define inf 1e18
typedef long long ll;
typedef long double ld;
const ll maxn = 1e5+100;
const ll mod = 1e9+7;
const ld pi = acos(-1.0);

ld xa,ya,za,ra,xb,yb,zb,rb;
ld d,va,vb,ans,ha,hb;

int main() {
	ios::sync_with_stdio(false);

	cin >> xa >> ya >> za >> ra >> xb >> yb >> zb >> rb;

	d = sqrt( (xa-xb)*(xa-xb) + (ya-yb)*(ya-yb) + (za-zb)*(za-zb) ); //圆心距离
	va = (4.0/3.0)*pi*(ra*ra*ra);   // a球体积
	vb = (4.0/3.0)*pi*(rb*rb*rb);   // b球体积

	if(d >= ra+rb) {     //判断相离或相切
		ans = va+vb;
	} else if( d+min(ra,rb) <= max(ra,rb) ) { // 包含
		ans = max(va,vb);
	} else {   //相切
		ha = ra - ( ( (ra*ra) - (rb*rb) + (d*d) ) / (2*d) ); //球冠的高
		hb = rb - ( ( (rb*rb) - (ra*ra) + (d*d) ) / (2*d) );

		ld vra = ( pi*ha*ha*( 3*ra-ha  ) ) / 3.0;     //球冠体积
		ld vrb = ( pi*hb*hb*( 3*rb-hb  ) ) / 3.0;

		ans = va + vb - vra - vrb;
	}

	cout << fixed << setprecision(7) << ans << endl;

	return 0;
}

标签:xb,xa,两球,za,体积,ra,rb,include
来源: https://www.cnblogs.com/Tecode/p/15032183.html