How do I find common volume of spheres?
5 views (last 30 days)
Show older comments
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:
h=surf(x+inx, y+iny, z+inz); % generates first sphere
b=surf(x+inx, y+iny, z+inz); %generates second sphere
c=surf(x+inx, y+iny, z+inz);
e=surf(x+inx, y+iny, z+inz);
f=surf(x+inx, y+iny, z+inz);
set(h,'FaceAlpha',0.5) %makes them transparent
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
idx = true(size(X)); % All points assumed in to start with
for i = 1:numel(inx)
idx = idx & (X - inx(i)).^2 + (Y - iny(i)).^2 + (Z - inz(i)).^2 <= r^2;
X = X(idx); Y = Y(idx); Z = Z(idx);
In your case, the intersection of all spheres is empty.
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.
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!