题目链接:
题意:
在三维坐标系中,有n个球体,每个球的球心为(xi,yi,zi),半径为ri。再给定一个球(球心为(X,Y,Z),半径为R),求该球与其余n个球相交部分体积。(保证一开始的n个球两两之间没有重叠)
思路:
因为n个球两两之间没有重叠,所以考虑其中每个球(称为小球)与最后给定的那个球(称为大球)即可。分3种情况:
设 d 为两球球心之间的距离。
一、d >= R+ri :两球不相交,即相交部分体积=0.
二、d+ri = R :小球在大球里面,即相交部分体积 = 小球体积 = .
三、R-ri < d < R+ri :两球相交,相交部分体积:
设 , .
, .
V = .
证明:
相交部分体积是由2块构成的,分别属于两个球体。其中一块的体积公式为(以大球为例):
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const int MAX = 100 + 10;
const int inf = 1e9 + 7;
typedef struct {
double x, y, z, r;
}Point;
int n;
Point a[MAX];
Point s;
//两点之间距离
double dis(Point p, Point q) {
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));
return ans;
}
int main()
{
int T;
scanf("%d", &T);
int Case = 1;
while (T--)
{
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lf%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z, &a[i].r);
}
scanf("%lf%lf%lf%lf", &s.x, &s.y, &s.z, &s.r);
double ans = 0;
for (int i = 0; i < n; i++) {
double d = dis(s, a[i]);
if (d >= s.r + a[i].r) {
continue;
}
else if (d + a[i].r <= s.r) {
ans += (4.0 / 3)*pi*a[i].r*a[i].r*a[i].r;
}
else {
double co = (s.r*s.r + d * d - a[i].r*a[i].r) / (2.0*d*s.r);
double h = s.r*(1 - co);
ans += (1.0 / 3)*pi*(3.0*s.r - h)*h*h;
co = (a[i].r*a[i].r + d * d - s.r*s.r) / (2.0*d*a[i].r);
h = a[i].r*(1 - co);
ans += (1.0 / 3)*pi*(3.0*a[i].r - h)*h*h;
}
}
printf("Case #%d: %.10lf\n", Case++, ans);
}
return 0;
}