How do I plot a 3D Box containing points?

조회 수: 13 (최근 30일)
Lamin Rehan
Lamin Rehan 2014년 9월 21일
댓글: carlotta barbierato 2020년 2월 19일
I want to plot a 3D box, suppose [0,0,0] to [1,1,1] box boundary along X,Y,Z. Inside the box, I want to plot some number of points, say 10. these points must be inside the box. How can I do it in MATLAB? Please assist me. Thanks.

답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 9월 22일
Lamin - consider using plot3 to draw your box (and later, the 10 points). Draw the box in "parts" by defining the xy coordinates for the corners of the box, drawing a line from the origin (0,0) to (1,0) to (1,1) to (0,1) and back to origin. The X, Y, and Z vectors become
X = [0;1;1;0;0];
Y = [0;0;1;1;0];
Z = [0;0;0;0;0];
figure;
hold on;
plot3(X,Y,Z); % draw a square in the xy plane with z = 0
plot3(X,Y,Z+1); % draw a square in the xy plane with z = 1
set(gca,'View',[-28,35]); % set the azimuth and elevation of the plot
Now connect the two squares
for k=1:length(X)-1
plot3([X(k);X(k)],[Y(k);Y(k)],[0;1]);
end
Now use rand and scatter3 to generate 10 random points within the box (note that rand returns a pseudorandom scalar drawn from the standard uniform distribution on the open interval (0,1)).
scatter3(rand(10,1),rand(10,1),rand(10,1),'g');
There are probably other ways to generate the same result with fewer lines of code.
  댓글 수: 4
Geoff Hayes
Geoff Hayes 2020년 2월 19일
carlotta - if you know the positions for each of the points you want to draw within box, you should be able to use the above code and just replace the calls to rand with the known x, y, and z coordinates.
carlotta barbierato
carlotta barbierato 2020년 2월 19일
ok, thank you!

댓글을 달려면 로그인하십시오.

카테고리

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