MATLAB Answers

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

4 ビュー (過去 30 日間)
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;
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');

  0 件のコメント



Image Analyst
Image Analyst 2020 年 8 月 25 日
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);

  3 件のコメント

Nnamdi Chukwunenye
Nnamdi Chukwunenye 2020 年 8 月 25 日
Hi, thanks for your help. I would like the points anywhere inside the shell as long as they don't touch the shell or the xy-plane below. How would I go about getting them to plot randomly in within the hemisphere? I was thinking of doing something like this
r = 76;
t = 2*pi*rand(n,1);
r = r*sqrt(rand(n,1));
x = r.*cos(t); % x coordinates
y = r.*sin(t); % y coordinates
But with spherical coordinates, but I'm not sure how to prevent negative values in the z direction. Would you recommend scatter3 over the above method? In regards to the distances between the center sphere and the random ones will that solution only work with catersian coordinates ?
Nnamdi Chukwunenye
Nnamdi Chukwunenye 2020 年 8 月 27 日
Hi, I adapted the FAQ soln into my code and was succesfully able to generate the random points inside of the hemisphere. Thank you for your help with that, but the second part of my problem is calculating the distance between the surface of the sphere are the center of the domain and scattered points around it. I want eliminate the points that lie within a certain distance (d=16) from the surface of hemisphere located at the center of the domain.
% Create Set of Random Points to be Plotted inside Hemisphere
n = 50;
A = zeros(n,3);
radius = 76;
% Generate random points
r = randn(n,3);
r = bsxfun(@rdivide, r, sqrt(sum(r.^2,1)));
r = radius*r;
% Extract the x, y, and z coordinates from the array.
x = r(:,1); % Extract x from column #1.
y = r(:,2); % Extract y from column #2.
z = abs(r(:,3)); % Extract z from column #3.
A = [x, y, z]; % stores random points generated
BioM_3Dgraph1 = scatter3(x, y, z, 'filled', 'r');


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by