简体   繁体   中英

How to draw a cube (3D box) around a point in MATLAB?

How can I draw a 3D bounding box around a 3D point in Matlab? eg plotting a cube (15x15x15) around a 3D point (20,3,10); the point should be in inside and center of the cube.

Do you want the six sides of the box to be semi-transparent? If so, you could use the following:

% Center point is at coordinate [ax ay az].
ax = 20;  ay = 3;  az = 10;

% Full-width of each side of cube.
w = 15;

% For readability.
h = w/2;

patch_args = { 'FaceColor', 'b', 'FaceAlpha', 0.3 };

% Side #1 of 6.
patch( 'XData', ax+[-h -h  h  h], 'YData', ay+[-h  h  h -h], 'ZData', az+[-h -h -h -h], patch_args{:} )
daspect( [1 1 1] )  % 1:1:1 aspect ratio.
hold on
% Side #2 of 6.
patch( 'XData', ax+[-h -h  h  h], 'YData', ay+[-h  h  h -h], 'ZData', az+[ h  h  h  h], patch_args{:} )
% Side #3 of 6.
patch( 'XData', ax+[-h -h  h  h], 'YData', ay+[ h  h  h  h], 'ZData', az+[-h  h  h -h], patch_args{:} )
% Side #4 of 6.
patch( 'XData', ax+[-h -h  h  h], 'YData', ay+[-h -h -h -h], 'ZData', az+[-h  h  h -h], patch_args{:} )
% Side #5 of 6.
patch( 'XData', ax+[ h  h  h  h], 'YData', ay+[-h -h  h  h], 'ZData', az+[-h  h  h -h], patch_args{:} )
% Side #6 of 6.
patch( 'XData', ax+[-h -h -h -h], 'YData', ay+[-h -h  h  h], 'ZData', az+[-h  h  h -h], patch_args{:} )

% Red dot in middle.
scatter3( ax, ay, az, 'or', 'filled', 'SizeData', 150 )

hold off

If instead you want the six sides to be completely transparent, you could repeat the code above but set FaceAlpha to 0.0 instead of 0.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM