# 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:

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);

shading faceted

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

##### 0 Comments

### Answers (2)

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;

end

X = X(idx); Y = Y(idx); Z = Z(idx);

In your case, the intersection of all spheres is empty.

##### 0 Comments

Cameron Henry
on 5 Jul 2012

##### 1 Comment

Richard Brown
on 8 Jul 2012

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!