Randomly generated cubes in cylindrical volumes in MATLAB

5 ビュー (過去 30 日間)
hongtao xu
hongtao xu 2023 年 9 月 23 日
コメント済み: KSSV 2023 年 10 月 9 日
Hey
I want to generate n cubes of a given side length at any position within a given cylinder volume. Following the condition of not intersecting each other.
Can someone help me with this.
Regards
Om
  5 件のコメント
hongtao xu
hongtao xu 2023 年 9 月 25 日
@Image AnalystI need to output a 3D stereogram because I need to import into comsol.just randomly place them one-by-one at a random location wherever they might fit,Because the volume of these cubes doesn't need to fill the entire cylinder,the cube I set is about 85 percent volume,and the rest is air.
hongtao xu
hongtao xu 2023 年 9 月 26 日
Just like this one.

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

採用された回答

KSSV
KSSV 2023 年 9 月 26 日
% Draw cyliner
Radius = 1. ; % Radius of the cylindrical shell
Height = 2. ; % Height of the Cylinder
%
NH = 7 ; % Number of Elements on the Height
NT = 10 ; % Number of Angular Dicretisation
% Discretizing the Height and Angle of the cylinder
nH = linspace(0,Height,10) ;
nT = linspace(0,2*pi,100) ;
[H, T] = meshgrid(nH,nT) ;
% Convert grid to cylindrical coordintes
X = Radius*cos(T);
Y = Radius*sin(T);
Z = H ;
% DRaw randoom cubes
% First make random points so that cubes donot intersect
L = 0.2 ; % L, B, H of cube
dL = L+0.1 ;
x = -(Radius-L):dL:(Radius-L) ;
y = x ;
z = L:dL:Height-L ;
%
[xx,yy,zz] = meshgrid(x,y,z) ;
xx = xx(:) ; yy = yy(:) ; zz = zz(:) ;
% Remove points lying outside the cylinder
idx = inpolygon(xx,yy,(Radius-L)*cos(nT),(Radius-L)*sin(nT)) ;
xx = xx(idx) ; yy = yy(idx) ; zz = zz(idx) ;
% randomly select few points
N = 100 ;
idx = randsample(1:length(xx),N) ;
xc = xx(idx) ; yc = yy(idx) ; zc = zz(idx) ;
figure
hold on
plot3(X',Y',Z,'k')
plot3(X,Y,Z,'k')
axis equal
for i = 1:N
plotcube([L L L],[xc(i) yc(i) zc(i)]);
drawnow
end
You may download the function plot cube.
  2 件のコメント
hongtao xu
hongtao xu 2023 年 10 月 9 日
Thanks,it looks perfect.
KSSV
KSSV 2023 年 10 月 9 日
Thanks is accepting/ voting the answer. :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by