MATLAB: Generate randomly placed spheres within a hemisphere domain and calculate the distance between the center of the hemisphere and the random spheres

3d plotsmatrix manipulationplotspheresurfaces

Hello, I am trying to generate randomly placed spheres inside of a defined hemisphere domain. I would like the spheres to be generated in a spherical pattern. Would I need to use spherical coordinates converted to euclidean coordinates with a scatter3 'filled' plot for this part? Next, I want to calculate the distance between a sphere placed at the origin of the domain and the new spheres. From my understanding that would require me to calculate the distance between 2 surfaces or a surface and multiple points, but from my research I have not seen any ways to do this in the documentation or answers pages. I have attached my code below that defines the domain and sets the origin hemisphere. Any help is greatly appreciated.
% Create Hemisphere Domain
[x_dom,y_dom,z_dom] = sphere(80); %Create Sphere
x_dom = x_dom(41:end,:); % Keep top 41 x points
y_dom = y_dom(41:end,:); % Keep top 41 y points
z_dom = z_dom(41:end,:); % Keep top 41 z points
hemisphere_radius = 80;
figure();
Hemi_sf = surf(hemisphere_radius.*x_dom,hemisphere_radius.*y_dom,hemisphere_radius.*z_dom, 'FaceColor','#4DBEEE','EdgeColor', 'none');
alpha 0.2
x_ax_lab = xlabel('x axis', 'Color', '#4DBEEE');
y_ax_lab = ylabel('y axis', 'Color', '#4DBEEE');
z_ax_lab = zlabel('z axis', 'Color', '#4DBEEE');
% Plot Hemisphere Lower Boundary Circular Plane
x_c = 0;
y_c = 0;
z_c = 0;
radii_plane = 80;
radii_vein = 4;
radii_node = 4;
center_plane = [x_c, y_c]; % center point of circular plane
viscircles(center_plane, radii_plane, 'color', '#77AC30');
hold on
%%
% Place Center Sphere
[x_c,y_c,z_c] = sphere();
x_c = x_c(11:end,:); % Keep top 11 x points
y_c = y_c(11:end,:); % Keep top 11 y points
z_c = z_c(11:end,:); % Keep top 11 z points
center_root = surf(radii_vein.*x_c,radii_vein.*y_c,radii_vein.*z_c, 'FaceColor', 'k');

Best Answer

Do you want the points to be on the outer shell of the hemisphere, or anywhere inside it?
If you have coordinates x, y, z for each point then you can find the distances of the entire group from the origin like this:
distances = sqrt(x(:) .^ 2 + y(:) .^ 2 + z(:) .^2);