Random lines confined in a 3d box

1 回表示 (過去 30 日間)
george korris
george korris 2022 年 10 月 5 日
コメント済み: george korris 2022 年 10 月 5 日
Hi everyone I am trying to write a code that makes random lines confined in a 3d box but i cant figure it out.
I changed a code i found in the forum for the 2d case but it doesnt work with my changes.
figure;
axes('XLim', [-0.1, 1.1], 'YLim', [-0.1, 1.1], 'ZLim', [-0.1, 1.1]);
nLine = 100;
Coor = [0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0];
for k = 1:nLine
edge1 = randi(4);
edge2 = randi(4);
edge3 = randi(4);
P1 = Coor(:, edge1) + rand * (Coor(:, edge1 + 1) - Coor(:, edge1));
P2 = Coor(:, edge2) + rand * (Coor(:, edge2 + 1) - Coor(:, edge2));
P3 = Coor(:, edge3) + rand * (Coor(:, edge3 + 1) - Coor(:, edge3));
line([P1(1), P2(1), P3(1)], [P1(2), P2(2), P3(2)], [P1(3), P2(3), P3(3)]);
end
  1 件のコメント
Jan
Jan 2022 年 10 月 5 日
"doesn't work" is too vague to understand, what the problem is and what you want to achieve instead.

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

採用された回答

Jan
Jan 2022 年 10 月 5 日
編集済み: Jan 2022 年 10 月 5 日
figure;
axes('XLim', [-0.1, 1.1], 'YLim', [-0.1, 1.1], 'ZLim', [-0.1, 1.1]);
hold('on');
view(3)
nLine = 100;
% A cube has 8 corners, but maybe you want 12 edges?
Coor = [0, 0, 0, 0, 1, 1, 1, 1; ...
0, 0, 1, 1, 0, 0, 1, 1; ...
0, 1, 0, 1, 0, 1, 0, 1];
for k = 1:nLine
edge1 = randi(7);
edge2 = randi(7);
edge3 = randi(7);
P1 = Coor(:, edge1) + rand * (Coor(:, edge1 + 1) - Coor(:, edge1));
P2 = Coor(:, edge2) + rand * (Coor(:, edge2 + 1) - Coor(:, edge2));
P3 = Coor(:, edge3) + rand * (Coor(:, edge3 + 1) - Coor(:, edge3));
line([P1(1), P2(1), P3(1)], [P1(2), P2(2), P3(2)], [P1(3), P2(3), P3(3)]);
end
  1 件のコメント
george korris
george korris 2022 年 10 月 5 日
@Jan thank you!!!

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by