【模板】 两球的体积并 || 体积交 球

 

 


#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;
}

void SphereInterVS(sphere a, sphere b,double &v,double &s) {
    double d = dist(a.centre, b.centre);//球心距
    double t = (d*d + a.r*a.r - b.r*b.r) / (2.0 * d);//
    double h = sqrt((a.r*a.r) - (t*t)) * 2;//h1=h2,球冠的高
    double angle_a = 2 * acos((a.r*a.r + d*d - b.r*b.r) / (2.0 * a.r*d));  //余弦公式计算r1对应圆心角,弧度
    double angle_b = 2 * acos((b.r*b.r + d*d - a.r*a.r) / (2.0 * b.r*d));  //余弦公式计算r2对应圆心角,弧度
    double l1 = ((a.r*a.r - b.r*b.r) / d + d) / 2;
    double l2 = d - l1;
    double x1 = a.r - l1, x2 = b.r - l2;//分别为两个球缺的高度
    double v1 = PI*x1*x1*(a.r - x1 / 3);//相交部分r1圆所对应的球缺部分体积
    double v2 = PI*x2*x2*(b.r - x2 / 3);//相交部分r2圆所对应的球缺部分体积
     v = v1 + v2;//相交部分体积
    double s1 = PI*a.r*x1;  //r1对应球冠表面积
    double s2 = PI*a.r*x2;  //r2对应球冠表面积
     s = 4 * PI*(a.r*a.r + b.r*b.r) - s1 - s2;//剩余部分表面积

}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值