Drawing 3d objects and filling the sides
Afficher commentaires plus anciens
I created this program to draw a rectangle and fill the sides with color. But now I need to adapt it to accept multiple other shapes (hexagon, pentagon, etc..)
Is there an easier way of drawing these shapes then what I'm doing now? Something that can dynamically look at the points/# of sides and draw it correctly?
This is how it currently draws the object.
The xCalc, yCalc, and zCalc are just lists filled with points.
ex:
xCalc = [0.2000 0.5000 0.5000 0.2000 0.2000 0.5000 0.5000 0.2000] all x-coordinates
etc...
%bottom
fill3(xCalc(1:4), yCalc(1:4), zCalc(1:4), 1);
%up
fill3(xCalc(5:8), yCalc(5:8), zCalc(5:8), 2);
%right
fill3(xCalc([2 6 7 3]), yCalc([2 6 7 3]), zCalc([2 6 7 3]), 3);
%front
fill3(xCalc([2 6 5 1]), yCalc([2 6 5 1]), zCalc([2 6 5 1]), 4);
%back
fill3(xCalc([4 8 7 3]), yCalc([4 8 7 3]), zCalc([4 8 7 3]), 5);
%left
fill3(xCalc([1 5 8 4]), yCalc([1 5 8 4]), zCalc([1 5 8 4]), 6);

Réponses (1)
Star Strider
le 3 Avr 2019
If you simply want different numbers of sides of a regular polygonal solid, this works:
N = 3; % Sides
a = linspace(0, 2*pi, N+1); % Angles
x = [1; 1]*cos(a);
y = [1; 1]*sin(a);
z = [-ones(1,size(x,2)); ones(1,size(x,2))];
figure
surf(x, y, z, 'FaceColor','g')
hold on
patch(x', y', z', 'r')
hold off
axis equal
view(-25, 30)
This draws a prism with green sides and red ends. Change the value of ‘N’ to get different numbers of sides (I experimented with a pentagon and hexagon successfully). If you want different numbers of sides in all axes (for example a pyramid or a duodecahedron), there may be File Exchange routines that will create and plot them.
6 commentaires
Jake Young
le 4 Avr 2019
Star Strider
le 4 Avr 2019
What are your ‘own coordinates’?
Bradley Layton
le 27 Avr 2020
Thanks for the compact and accurate code!

Star Strider
le 27 Avr 2020
Bradley Layton —
My pleasure!
A Vote would be apprecaited!
.
Ang Vas
le 25 Fév 2021
That was very helpful for me as well, but can I separate the two object that I made in order to see as two different objects? Also, how can I change the dimensions of the objects, sorry me for asking but I am not the best at coding if you know what I meen
N1 = 4;
N2 = 5; % Sides
a1 = linspace(0, 2*pi, N1+1);
a2 = linspace(0, 2*pi, N2+1); % Angles
x1 = [1; 1]*cos(a1);
y1 = [1; 1]*sin(a1);
z1 = [-ones(1,size(x1,2)); ones(1,size(x1,2))];
x2 = [1; 1]*cos(a2);
y2 = [1; 1]*sin(a2);
z2 = [-ones(1,size(x2,2)); ones(1,size(x2,2))];
figure
surf(x1, y1, z1, 'FaceColor','g')
surf(x2, y2, z2, 'FaceColor','g')
hold on
patch(x1', y1', z1', 'r')
patch(x2', y2', z2', 'r')
hold off
axis equal
view(-25, 30)
Adding 3 to ‘x2’ and 2 to ‘y2’ here —
N1 = 4;
N2 = 5; % Sides
a1 = linspace(0, 2*pi, N1+1);
a2 = linspace(0, 2*pi, N2+1); % Angles
x1 = [1; 1]*cos(a1);
y1 = [1; 1]*sin(a1);
z1 = [-ones(1,size(x1,2)); ones(1,size(x1,2))];
x2 = [1; 1]*cos(a2);
y2 = [1; 1]*sin(a2);
z2 = [-ones(1,size(x2,2)); ones(1,size(x2,2))];
figure
surf(x1, y1, z1, 'FaceColor','g')
hold on
surf(x2+3, y2+2, z2, 'FaceColor','g')
patch(x1', y1', z1', 'r')
patch(x2'+3, y2'+2, z2', 'r')
hold off
axis equal
view(-25, 30)
My apologies for the delay. .
.
Catégories
En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
