# How do I find common volume of spheres?

5 views (last 30 days)
Cameron Henry on 5 Jul 2012
I have plotted some spheres using surf and want to find where they all meet. i.e. co-ordinates that are inside all of the spheres.
the code I used to generate the spheres is:
r=15;
phi=linspace(0,pi,30);
theta=linspace(0,2*pi,40);
[phi,theta]=meshgrid(phi,theta);
x=r*sin(phi).*cos(theta);
y=r*sin(phi).*sin(theta);
z=r*cos(phi);
clf
hold on
inx=3363.5;
iny=1195.5;
inz=21.5;
h=surf(x+inx, y+iny, z+inz); % generates first sphere
inx=3363.5;
iny=1169.5;
inz=32.5;
b=surf(x+inx, y+iny, z+inz); %generates second sphere
inx=3346.5;
iny=1177.5;
inz=33.5;
c=surf(x+inx, y+iny, z+inz);
inx=3366.5;
iny=1182.5;
inz=12.5;
e=surf(x+inx, y+iny, z+inz);
inx=3357.5;
iny=1182.5;
inz=12.5;
f=surf(x+inx, y+iny, z+inz);
set(h,'FaceAlpha',0.5) %makes them transparent
set(b,'FaceAlpha',0.5)
set(c,'FaceAlpha',0.5)
set(e,'FaceAlpha',0.5)
set(f,'FaceAlpha',0.5)
I have tried simply comparing the spheres but I am finding it difficlut to see where they meet since there is soo many.
How can I make them easier to compare?
P.S. I am using r2011a

Richard Brown on 5 Jul 2012
Here's how I'd do it (if I was in a hurry). First pretend all the spheres are cubes, and find their intersection.
inx = [3363.5, 3363.5, 3346.5, 3366.5, 3357.5];
iny = [1195.5, 1169.5, 1177.5, 1182.5, 1182.5];
inz = [21.5, 32.5, 33.5, 12.5, 12.5];
% Find bounding box for region
xlim = [max(inx - r), min(inx + r)];
ylim = [max(iny - r), min(iny + r)];
zlim = [max(inz - r), min(inz + r)];
Then, generate a large number ( ngrid^3 ) of points inside that box
ngrid = 100;
[X, Y, Z] = ndgrid(linspace(xlim(1), xlim(2), ngrid), ...
linspace(ylim(1), ylim(2), ngrid), ...
linspace(zlim(1), zlim(2), ngrid));
X = X(:); Y = Y(:); Z = Z(:);
Finally, rule them out by checking every point against every sphere. If you're lucky there should be some left.
% Now rule points out
for i = 1:numel(inx)
idx = idx & (X - inx(i)).^2 + (Y - iny(i)).^2 + (Z - inz(i)).^2 <= r^2;
end
X = X(idx); Y = Y(idx); Z = Z(idx);
In your case, the intersection of all spheres is empty.

Cameron Henry on 5 Jul 2012
thanks for the quick response!
so there is no common volume? :(
what I needed to find was the maximum number of spheres that you could be in at the same time and where the co-ordinates of that is. I thought that these ones were the maximum amount, but obvisuly not.
so how would I find the common volume that includes as many spheres as possible?
There is 9 spheres, the coordinates of the spheres are the 5 from before as well as 4 more
x=[3358.5,3364.5,3353.5,3361.5]
y=[1168.5,1201.5,1190.5,1179.5]
z=[8.5,12.5,17.5,23.5]
I excluded these before because, the first two are further away so I think they won't work, and the other 2 are in close so I think that they will always be part of the common volume
Richard Brown on 8 Jul 2012
If you have only 9 spheres, then there are only 81 possible ways of choosing between 1 and 9 different spheres. I suggest you use my approach above on each of these combinations, and see what the largest number of spheres you can find that has a nonempty intersection is.