ZJU-ICPC Summer 2020 Contest 9 B

B. Ball
In Geometry, a spherical cap is a portion of a sphere cut off by a plane. If the radius of the sphere is \(r\) and the height of the spherical cap is \(h\), the volume of the spherical cap is \(πh^{2}(r−\frac{1}{3}h)\).

There are two spheres in the three-dimensional space whose radius are \(r_1\) and \(r_2\), center coordinates are \((x1,y1,z1)\) and \((x2,y2,z2)\). Your task is to calculate the volume of their combination.

Input
The first line contains four integers, denoting \(x1, y1, z1, r1(0 \leq x1,y1,z1,r1\leq 100)\).

The second line contains four integers, denoting \(x2, y2, z2, r2(0 \leq x2,y2,z2,r2 \leq 100)\).

Output
Output one line with one real number, the volume of their combination.

Your answer will be consider correct if its absolute or relative error will not exceed \(10^{−6}\).

Examples

inputCopy
0 0 0 4
3 4 0 3
outputCopy
361.911473693544167

inputCopy
0 0 0 5
3 0 0 3
outputCopy
538.521340702850352

求两个球体并起来的体积。

首先分三种情况:包含,分离,相交。

前两种利用球体公式,后一种用题目给的球冠公式做。


#include<bits/stdc++.h>
using namespace std;

const long double pi = acos(-1);

long double getans(long double r,long double h){
    return pi * h * h * (3 * r - h);
}
long double get(long double r){
    return 4 * pi * r * r * r;
}

long double ax,ay,az,bx,by,bz,ar,br;

int main(){
    cin >> ax >> ay >> az >> ar;
    cin >> bx >> by >> bz >> br;
    
    long double dis = (ax - bx) * (ax - bx) + (ay - by) * (ay - by) + (az - bz) * (az - bz);
    
    if(dis >= (ar + br) * (ar + br)){
        printf("%.15Lf\n",(get(ar) + get(br)) / 3.0);
        return 0;
    }
    
    dis = sqrt(dis);
    
    if(dis + ar <= br || dis + br <= ar){
        printf("%.15Lf\n",get(max(ar,br))/3.0);
        return 0;
    }
    
    long double x = ar * ar - br * br + dis * dis; x = x / (2 * dis); 
    long double y = dis - x;
    
    printf("%.15Lf\n",(getans(ar,ar + x) + getans(br,br + y)) / 3.0);
    return 0;
}


posted @ 2020-07-27 16:01  zhuzihan  阅读(120)  评论(0编辑  收藏  举报