plot many sphere with variant radii

8 visualizaciones (últimos 30 días)
amir ansari
amir ansari el 11 de Nov. de 2020
Respondida: Aashray el 24 de Abr. de 2025
hi to all
i have spheres centers and radii and i just want to plot n spheres with variant radii in a cube ,like below figure.how can i do that?
  1 comentario
amir ansari
amir ansari el 11 de Nov. de 2020
i forgot to say that the links are not important and just sphere's need

Iniciar sesión para comentar.

Respuestas (1)

Aashray
Aashray el 24 de Abr. de 2025
Hello Amir
MATLAB’s built-in surf function can be used to plot surfaces in three dimensions. I have included an example code below for better understanding:
n = 35; % Number of spheres
cubeSize = 10; % Cube dimensions
% Using placeholder values for radii and centers
radii = 0.5 + rand(n, 1)*0.4;
centers = radii + (cubeSize - 2 * radii) .* rand(n, 3);
% These values can be replaced by actual values as below:
% radii = [1.2; 0.8; 1.5; ...];
% centers = [2, 3, 4;
% 5, 5, 5;
% 1, 1, 1;
% ...];
% Plot setup
figure;
hold on;
axis equal;
xlim([0 cubeSize]);
ylim([0 cubeSize]);
zlim([0 cubeSize]);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Spheres with Given Centers and Radii');
% Plot each sphere using the given center and radius
for i = 1:n
r = radii(i);
center = centers(i, :);
[X, Y, Z] = sphere(20); % Resolution of sphere
X = r * X + center(1);
Y = r * Y + center(2);
Z = r * Z + center(3);
% Plot the sphere
surf(X, Y, Z, 'EdgeColor', 'none', 'FaceAlpha', 0.9);
end
view(3);
grid on;
You can also adjust the transparency of the spheres by modifying the FaceAlpha parameter, set it from 0 (fully transparent) to 1 (fully opaque).
Below are some example images of the plot generated using the above code:
For more details on the functions used, you can refer to the MATLAB documentation:
  1. https://www.mathworks.com/help/matlab/ref/figure.html
  2. https://www.mathworks.com/help/matlab/ref/sphere.html
  3. https://www.mathworks.com/help/matlab/ref/surf.html

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by