How to remove or exclude the intersecting part from two intersecting or overlaping spheres using Matlab?

Hi everyone,
i have two spheres of radii R1 and R2 with centers C1 and C2 intersecting each others. i want to exclude the intersecting part. How to remove it to get new object like peanut. i used following code to get interseting spheres.

3 Comments

For just a few seconds I wanted to answer your question by using a polyshape for the task.
A polyshpe would be perfect, if only this were a 2-d problem, and you wanted to solve it for circles. Or, if polyshapes worked in 3-d. Maybe next release, but sadly, things get significantly more complex when you work in 3-d. Having written code for the intersection, union, etc., of volumes in 3-d and above, I will attest to that.
Thanks Mr. John for your kind feedback and guidance. Warm regards!

Sign in to comment.

 Accepted Answer

Here is what i invented
ix1 = (xc1-x2).^2+(yc1-y2).^2+(zc1-z2).^2 < R2^2;
ix2 = (xc2-x1).^2+(yc2-y1).^2+(zc2-z1).^2 < R1^2;
scatter3(xc1(ix1),yc1(ix1),zc1(ix1),20,'r','fill')
scatter3(xc2(ix2),yc2(ix2),zc2(ix2),20,'r','fill')
Of course i prepared explanations and result picture

10 Comments

Thanks for guidance. Could you please explain about xc1, yc1 and zc1.
i am not so expert in matlab. where can i fix this code.
ix1 = (xc1-x2).^2+(yc1-y2).^2+(zc1-z2).^2 < R2^2;
ix2 = (xc2-x1).^2+(yc2-y1).^2+(zc2-z1).^2 < R1^2;
scatter3(xc1(ix1),yc1(ix1),zc1(ix1),20,'r','fill')
scatter3(xc2(ix2),yc2(ix2),zc2(ix2),20,'r','fill')
Regards in advance.
% Hi Mr. Darova, i have followed your procedure but i cannot get the same figure as you
% generated. Please can you check, where i am making error. My figure is attached.
% Thanks for all your cooperation.
R1 = 10; R2 = 14; C1=[10 12 14]; C2=[16 18 20];
%************** *************************** Point cloud of Sphere-1 *******************************
% This function handle was used to create the sphere of 10,000 random
% points.
numPoints = 10000;
r = randn(3, numPoints);
r = bsxfun(@rdivide, r, sqrt(sum(r.^2,1)));
r = R1 * r;
% Extract the Xs1, Ys1, and Zs1 coordinates from the array for sphere S1
x1=C1(1,1);
y1=C1(1,2);
z1=C1(1,3);
x2 = C2(1,1);
y2 = C2(1,2);
z2 = C2(1,3);
Xs2 = r(1,:) + x2; % Extract Xs2 from row #1.
Ys2 = r(2,:) + y2; % Extract Ys2 from row #2.
Zs2 = r(3,:) + z2; % Extrac
% Xs1, Ys1, Zs1 represent coordiantes for sphere S1
Xs1 = r(1,:) + x1 ; % Extract Xs1 from row #1.
Ys1 = r(2,:) + y1; % Extract Ys1 from row #2.
Zs1 = r(3,:) + z1; % Extract Zs1 from row #3.
% Display the shell of points
figure(1)
ix2 = (Xs2-x1).^2+(Ys2-y1).^2+(Zs2-z1).^2 < R1^2;
scatter3(Xs2(ix2),Ys2(ix2),Zs2(ix2),20,'r','fill', 'MarkerFaceColor',[1 0 1])
hold on
scatter3(Xs1, Ys1, Zs1,'b')
% ix1 = (Xs1-x2).^2+(Ys1-y2).^2+(Zs1-z2).^2 < R2^2;
% scatter3(Xs1(ix1),Ys1(ix1),Zs1(ix1),20,'r','fill')
axis square;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
zlabel('Z', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%
%msgbox('Now use the circular arrow icon on the toolbar to rotate the sphere.');
%% ********************** Generation of sphere-2 of same 10,000 point cloud **************************************************************
numPoints = 10000;
% Get a 3-by-numPoints list of (Xs2, Ys2, zs2) coordinates for sphere S2.
r = randn(3, numPoints);
r = bsxfun(@rdivide, r, sqrt(sum(r.^2,1)));
r = R2 * r;
%C2= input('Centre of sphere 2 = ')
x2 = C2(1,1);
y2 = C2(1,2);
z2 = C2(1,3);
% Xs2, Ys2, Zs2 represent coordiantes for sphere S2
Xs2 = r(1,:) + x2; % Extract Xs2 from row #1.
Ys2 = r(2,:) + y2; % Extract Ys2 from row #2.
Zs2 = r(3,:) + z2; % Extract Zs2 from row #3.
% Display the shell of points
ix1 = (Xs1-x2).^2+(Ys1-y2).^2+(Zs1-z2).^2 < R2^2;
scatter3(Xs1(ix1),Ys1(ix1),Zs1(ix1),20,'r','fill', 'MarkerFaceColor',[1 0 1])
hold on
scatter3(Xs2, Ys2, Zs2,'g');
axis square;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
zlabel('Z', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
Thanks Mr. Darova for you guidance. i got this picture after following your guidelines. is it ok.
Now how can we exclude this intersected part from the two spheres.
Can we use NAN to negate this interseted area.
  • Can we use NAN to negate this interseted area.
Yes, we can or you can
Could you please guide regarding NAN by giving any hints. is there any other technique possible to remove this intersected part from total object. Thanks and very very warm regards four kind help and guidance. God bless you!
If ix1 and ix2 are indices or inside parts, then
  • You can draw only outside parts
scatter3(Xs1(~ix1),Ys1(~ix1),Zs1(~ix1)
scatter3(Xs2(~ix2),Ys2(~ix2),Zs2(~ix2)
  • You can remove inside parts
Xs1(ix1) = [];
Ys1(ix1) = [];
% and so on ...
scatter3(Xs1,Ys1,Zs1) % plot outside parts without using indices
  • You can replace inside parts with NaN or inf
Xs1(ix1) = nan; % or inf
Ys1(ix1) = nan; % or inf
% and so on ...
scatter3(Xs1,Ys1,Zs1) % plot outside parts without using indices
I like the first part. No more operations needed
Sir, these are three methods, either can be applied. you are geneous. can i have your email address plz.
Thanks sir. if you dont mind, i will keep in touch to learn from you Matlab skills. Regards and obliged for your kind guidance.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 1 Apr 2020

Edited:

on 4 Apr 2020

Community Treasure Hunt

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

Start Hunting!