MATLAB Answers

How to adjust centers and radii of three intersected spheres to get the desired shape using matlab

2 views (last 30 days)
M.S. Khan
M.S. Khan on 10 May 2020
Commented: M.S. Khan on 10 May 2020
Hi Matlab Community,
Thanks and regards for all support from distinguished community members.
I have three sphere they intersect each other for inputed Radii and Centers. My code (below )is giving me the intersected sphere shown in figure 1.
Demo values give me figure 1.
R1 =10, R2 =12, R3 =14, C1 = [10 12 14], C2 = [16 18 20], C3 = [ 22 24 26]
i want the intersected to intersect each other shown in figure2 ( Note: figure 2 are spheres not circles but i gave just for clearance my view to experts. )
Thanks in advance for all cooperation
% Figure 1 Figure 2
R1 = input('Radius1 = ') % we input radii i.e. R1, R2 and R3 of three spheres of our choice.
R2 = input('Radius2= ')
R3 = input('Radius3= ')
% *********************** TO INPUT CENTERS OF THE SPHERES ********************************************
% we input the centers i.e. C1, C2 and C3 of the three spheres like S1, S2 and S3 respectively.
C1 = input('Centre of the sphere1= ')
C2= input('Centre of sphere2 = ')
C3 = input('Centre of sphere3 = ')
% ***************************************** 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 x, y, and z coordinates from the array.
x1=C1(1,1);
y1=C1(1,2);
z1=C1(1,3);
x = r(1,:) + x1 ; % Extract x from row #1.
y = r(2,:) + y1; % Extract y from row #2.
z = r(3,:) + z1; % Extract z from row #3.
% Display the shell of points
figure(1)
scatter3(x, y, z);
axis square;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
zlabel('Z', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [1 1 1 1]);
%
%% ********************** Generation of sphere-2 of same 10,000 point cloud **************************************************************
numPoints = 10000;
% Get a 3-by-numPoints list of (x, y, z) coordinates.
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);
x = r(1,:) + x2; % Extract x from row #1.
y = r(2,:) + y2; % Extract y from row #2.
z = r(3,:) + z2; % Extract z from row #3.
% Display the shell of points
hold on
scatter3(x, y, z);
axis square;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
zlabel('Z', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [1 1 1 1]);
%hold off
%% ************************ Generation of 3rd sphere S3 for 10,000 random points ************************
numPoints = 10000;
% Get a 3-by-numPoints list of (x, y, z) coordinates.
r = randn(3, numPoints);
r = bsxfun(@rdivide, r, sqrt(sum(r.^2,1)));
r = R3 * r;
%C2= input('Centre of sphere 2 = ')
x3 = C3(1,1);
y3 = C3(1,2);
z3 = C3(1,3);
x = r(1,:) + x3; % Extract x from row #1.
y = r(2,:) + y3; % Extract y from row #2.
z = r(3,:) + z3; % Extract z from row #3.
% Display the shell of points
hold on
scatter3(x, y, z);
axis square;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
zlabel('Z', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [1 1 1 1]);
hold off

  4 Comments

Show 1 older comment

Sign in to comment.

Answers (1)

KSSV
KSSV on 10 May 2020
clc; clear all ;
r = [1 1. 1.] ; % radii of three spheres
P = [1. 1. 0; -1 0. 0.; 1. -1. 0. ] ; % centers of sphere
m = 20;
n = 10;
theta = linspace(0,2*pi,m) ;
phi = linspace(-pi/2,pi/2,n) ;
X = zeros(n,m,3) ;
Y = zeros(n,m,3) ;
Z = zeros(n,m,3) ;
[T,P] = meshgrid(theta,phi) ;
for i = 1:3
X(:,:,i) = P(i,1) + r(i) *cos(P).* cos(T);
Y(:,:,i) = P(i,2) + r(i) *cos(P).* sin(T);
Z(:,:,i) = P(i,3) + r(i) *sin(P) ;
end
figure
hold on
C = {'.r', '.g', '.b'} ;
for i = 1:3
plot3(X(:,:,i),Y(:,:,i),Z(:,:,i),C{i},"MarkerSize",10)
end

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!