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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by