Random lines confined in a 3d box

2 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by