題目鏈接:https://www.zhixincode.com/contest/10/problem/H?problem_id=153
題意:在三維坐標系中,有n個球體,每個球的球心為(xi,yi,zi),半徑為ri。再給定一個球(球心為(X,Y,Z),半徑為R),求該球與其余n個球相交部分體積。(保證一開始的n個球兩兩之間沒有重疊)
先介紹球缺的概念:
球缺是指球體被平面截去一部分后剩余的部分。截面稱為球缺的底面,垂直於截面的直徑被此截面截得的線段長稱為球缺的高。
球缺的表面積:(R是球體的半徑,h是球缺的高,r是球缺的底面半徑)
球缺的體積: 或
(R是球體的半徑,h是球缺的高,r是底面半徑)
與球冠區別:球缺是體,而球冠是面,故球冠只能計算表面積
后面附有大牛對球缺體積公式的證明
思路:
因為n個球兩兩之間沒有重疊,所以考慮其中每個球(稱為小球)與最后給定的那個球(稱為大球)即可。分3種情況:
設 d 為兩球球心之間的距離。
一、d >= R+ri :兩球不相交,即相交部分體積=0.
二、d+ri = R :小球在大球里面,即相交部分體積 = 小球體積 = .
三、R-ri < d < R+ri :兩球相交,相交部分體積:
設 , .
,
.
V = .
證明:https://blog.csdn.net/luyehao1/article/details/86583384
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 6 const double pi = acos(-1); 7 8 const int MAX = 100 + 10; 9 const int inf = 1e9 + 7; 10 11 typedef struct { 12 double x, y, z, r; 13 }Point; 14 15 int n; 16 Point a[MAX]; 17 Point s; 18 19 //兩點之間距離 20 double dis(Point p, Point q) { 21 double ans = sqrt((p.x - q.x)*(p.x - q.x) + (p.y - q.y)*(p.y - q.y) + (p.z - q.z)*(p.z - q.z)); 22 return ans; 23 } 24 25 int main() 26 { 27 int T; 28 scanf("%d", &T); 29 int Case = 1; 30 while (T--) 31 { 32 scanf("%d", &n); 33 for (int i = 0; i < n; i++) { 34 scanf("%lf%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z, &a[i].r); 35 } 36 scanf("%lf%lf%lf%lf", &s.x, &s.y, &s.z, &s.r); 37 double ans = 0; 38 for (int i = 0; i < n; i++) { 39 double d = dis(s, a[i]); 40 if (d >= s.r + a[i].r) { 41 continue; 42 } 43 else if (d + a[i].r <= s.r) { 44 ans += (4.0 / 3)*pi*a[i].r*a[i].r*a[i].r; 45 } 46 else { 47 double co = (s.r*s.r + d * d - a[i].r*a[i].r) / (2.0*d*s.r); 48 double h = s.r*(1 - co); 49 ans += (1.0 / 3)*pi*(3.0*s.r - h)*h*h; 50 co = (a[i].r*a[i].r + d * d - s.r*s.r) / (2.0*d*a[i].r); 51 h = a[i].r*(1 - co); 52 ans += (1.0 / 3)*pi*(3.0*a[i].r - h)*h*h; 53 } 54 } 55 printf("Case #%d: %.10lf\n", Case++, ans); 56 } 57 return 0; 58 }