Drawing 3d objects and filling the sides

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);

Answers (1)

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 Comments

Is there anyway I can use this code to work with my own coordinates though?
What are your ‘own coordinates’?
Thanks for the compact and accurate code!
Bradley Layton —
My pleasure!
A Vote would be apprecaited!
.
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)
@Ang Vas — To separate them, add appropriate offsets to the ‘x’ and ‘y’ coordinates.
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. .
.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 3 Apr 2019

Commented:

on 22 Feb 2024

Community Treasure Hunt

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

Start Hunting!