Random lines confined in a 3d box
2 views (last 30 days)
Show older comments
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 Comment
Jan
on 5 Oct 2022
"doesn't work" is too vague to understand, what the problem is and what you want to achieve instead.
Accepted Answer
Jan
on 5 Oct 2022
Edited: Jan
on 5 Oct 2022
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
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!