Random lines confined in a 3d box
1 次查看(过去 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
2022-10-5
"doesn't work" is too vague to understand, what the problem is and what you want to achieve instead.
采纳的回答
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!