plot many sphere with variant radii

10 ビュー (過去 30 日間)
amir ansari
amir ansari 2020 年 11 月 11 日
回答済み: Aashray 2025 年 4 月 24 日
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 件のコメント
amir ansari
amir ansari 2020 年 11 月 11 日
i forgot to say that the links are not important and just sphere's need

サインインしてコメントする。

回答 (1 件)

Aashray
Aashray 2025 年 4 月 24 日
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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by