CCPC-Wannafly Winter Camp Day2 H Cosmic Cleaner(两个球相交部分体积计算)

题目链接:

Cosmic Cleaner

 

题意:

在三维坐标系中,有n个球体,每个球的球心为(xi,yi,zi),半径为ri。再给定一个球(球心为(X,Y,Z),半径为R),求该球与其余n个球相交部分体积。(保证一开始的n个球两两之间没有重叠)

 

思路:

因为n个球两两之间没有重叠,所以考虑其中每个球(称为小球)与最后给定的那个球(称为大球)即可。分3种情况:

设 d 为两球球心之间的距离。

一、d >= R+ri :两球不相交,即相交部分体积=0.

二、d+ri = R :小球在大球里面,即相交部分体积 = 小球体积 =  \frac{4}{3}*\pi* (ri)^{3}.

三、R-ri < d < R+ri :两球相交,相交部分体积:

设 cos\alpha = \frac{R^{2}+d^{2}-(ri)^{2}}{2*R*d} ,h1 = R*(1-cos\alpha ) .

cos\beta = \frac{(ri)^{2}+d^{2}-R^{2}}{2*ri*d}h2 = ri*(1-cos\beta ) .

V = \frac{\pi }{3}*(3*R-h1)*h1^{2}+\frac{\pi }{3}*(3*ri-h2)*h2^{2} .

CCPC-Wannafly Winter Camp Day2 H Cosmic Cleaner(两个球相交部分体积计算)_第1张图片

证明:

相交部分体积是由2块构成的,分别属于两个球体。其中一块的体积公式为(以大球为例):

V=\int_{0}^{h}\pi (R^{2}-(Rcos\alpha +x)^{2})dx

=\int_{0}^{h}\pi (R^{2}-R^{2}cos^{2}\alpha -x^{2}-2Rcos\alpha x)dx

=\int_{0}^{h}\pi (R^{2}sin^{2}\alpha -x^{2}-2Rcos\alpha x)dx

= \pi (- \frac{1}{3}h^{3}-Rcos\alpha h^{2}+R^{2}sin^{2}\alpha h)

= \pi (Rh(Rsin^{2}\alpha -hcos\alpha )- \frac{1}{3}h^{3})

= \pi (Rh(R(1-cos^{2}\alpha ) -hcos\alpha )- \frac{1}{3}h^{3})

= \pi (Rh(R-Rcos^{2}\alpha -hcos\alpha )- \frac{1}{3}h^{3})

= \pi (Rh(R-cos\alpha (Rcos\alpha +h) )- \frac{1}{3}h^{3})

= \pi (Rh(R-Rcos\alpha )- \frac{1}{3}h^{3})

=\pi (Rh^{2}- \frac{1}{3}h^{3})

 

Code:

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

 

你可能感兴趣的:(ACM-计算几何)