How to draw specific 3D shapes in MATLAB?

13 views (last 30 days)
Veirian
Veirian on 4 Sep 2020
Answered: Star Strider on 4 Sep 2020
I need to extract dimensions of a probe from arrays and then draw a 3D image of the probe. I've managed to do this by essentially using a combination of fill3 (for the 13 bottom rectangles) and plot3 (for the sides) plots to draw each side of the probe, but the resulting code is very long and seems clumsy. I need to specify the location of every corner and then do a plot of it.
I've recently started using MATLAB so I was wondering if there was a more elegant way of drawing these shapes?
I've attached a picture of what I need to draw as well as an extract of my code.
% draw the probe itself:
% Side 1:
x = PROBE.ELEMENT_POSITION(1,1);
y = PROBE.ELEMENT_POSITION(2,1);
z = PROBE.ELEMENT_POSITION(3,1);
PROBE_P1 = [x-PROBE.ELEMENT_MINOR(1)/2 y-PROBE.ELEMENT_MAJOR(2)/2 0];
PROBE_P2 = [x-PROBE.ELEMENT_MINOR(1)/2 y+PROBE.ELEMENT_MAJOR(2)/2 0];
PROBE_P3 = [x-PROBE.ELEMENT_MINOR(1)/2 y+PROBE.ELEMENT_MAJOR(2)/2 PROBE_HEIGHT];
PROBE_P4 = [x-PROBE.ELEMENT_MINOR(1)/2 y-PROBE.ELEMENT_MAJOR(2)/2 PROBE_HEIGHT];
X = [PROBE_P1(1) PROBE_P2(1) PROBE_P3(1) PROBE_P4(1) PROBE_P1(1)];
Y = [PROBE_P1(2) PROBE_P2(2) PROBE_P3(2) PROBE_P4(2) PROBE_P1(2)];
Z = [PROBE_P1(3) PROBE_P2(3) PROBE_P3(3) PROBE_P4(3) PROBE_P1(3)];
plot3(X,Y,Z);

Answers (1)

Star Strider
Star Strider on 4 Sep 2020
I am not certain that this is a significant improvement, however I offer it as an alternative:
boxx = [1 2 2 1 1];
boxy = [1 1 4 4 1];
boxz = [1 1 1 1 1; 6 6 6 6 6];
plotboxx = [1;1]*boxx;
plotboxy = [1;1]*boxy
plotboxz = boxz;
graycolor = ones(size(boxx,1),size(boxx,2),3)*0.8;
figure
surf(plotboxx, plotboxy, plotboxz, graycolor, 'FaceAlpha',0.2)
hold on
patch(plotboxx.', plotboxy.', plotboxz.', [1 1 1]*0.8, 'EdgeColor','k', 'FaceAlpha',0.2)
hold off
grid
set(gcf, 'Color','w')
axis('equal')
view(30,30)
Define the box by changing the values in ‘boxx’ and the rest. It is difficult to explain how it works, other than the surf call constructing the sides of the box and the patch call defining the upper and lower surfaces.
Note how the vectors are constructed. The patch function requires that the vectors defining it create a closed area, here tracing the (x,y) coordinates (boxx(1),boxy(1)) to (boxx(2),boxy(2)) and so for the others. The same vectors work for both surf and patch, however they must be transposed for patch.

Community Treasure Hunt

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

Start Hunting!