Matlab Introductory Exercise 1
1. Create and visualize a cube
3
2
1
0
-1
-2
-3
2
3
2
0
1
0
-1
-2
-2
-3
Wire frame model view
Solid model view
------------------------------------------------------------------------------------------------% The code creates matrices to store the geometric data of a simple
% cube (Vertices, Edges, Faces, Face colors)
% generates a wireframe and a solid view of the cube
%
%
%
%
%
%
Things you will learn
Matrices, vectors
Element, row, column access of matrices
size()
for loops
Basic visualization- scatter3(), line(), patch()
%% house keeping
clear;
% clears all variables in the MatLab workspace
clc;
% clears the command window
close all;
% closes all figure windows
%% The B-rep
rep (Boundry representation) data of the cub
cube
e (i.e. Faces, Vertices, Edges)
%Cube parameters
l=2;
%length of an edge of the cube
%Vertex
%
%
V=[ 0 0
0 l
l l
l 0
0 0
0 l
l l
l 0
%Edge
%
E=[ 1
2
3
4
5
6
list 0;
0;
0;
0;
l;
l;
l;
l;];
Each row corresponds to a vertex
The columns corresponds to the x,y,z cordinates of the
corresponding vertex
% v1 = 0 0 0 <- x,y,z cordinates of v1
% v2 = 0 l 0 ...
list - Each row corresponds to an edge
columns are the start and end vertices of the edge (i.e indices of the vertices)
2;
% e1 = v1 v2 <- start and end vertices of edge e1
3;
% e2 = v2 v3 ...
4;
1;
6;
7;
7
8
1
2
3
4
8;
5;
5;
6;
7;
8];
%Face list - Each row corresponds to a face
%
columns are the vertices of the polygon describing the face (i.e indices of the
vertices)
F=[ 1 2 3 4; % f1 = v1 v2 v3 v4 <- the vertices of the polygon which bounds the planar surface
5 6 7 8; % f2 = v5 v6 v7 v8 ...
1 2 6 5;
2 3 7 6;
3 4 8 7;
1 4 8 5];
%Color list - Each row corresponds to a face
%
columns are the 8bit r g b values to color the face
C=[255 0 0;
% color of f1 = 255 0 0 = red
0 0 255;
% color of f2 = blue
0 255 0;
% color of f3 = green
128 128 128; %gray
255 255 255; %white
0 0 0;];
%black
%% Visualization
% Visualizing a wire frame model
figure()
%Draw vertices
scatter3(V(:,1),V(:,2),V(:,3));
hold on;
it
%creates a new figure window
%creates a 3D scatter plot of all Vertices
.
%Reads as Scatter3(Vertex
X cordinates,
%
Vertex Y cordinates,
%
Vertex Z cordinates)
% holds whats drawn on figure to draw more things on top of
%Draw edges
for i=1:size(E,1)
V1=V(E(i,1),:);
% The for loop iterates over the edge list
% E(i,1) is the index of the starting vertex of the i`th edge
% So V(E(i,1),:) are the cordinate of the starting vetex
V2=V(E(i,2),:);
% The ending vetex (V2) of the i'th edge
line([V1(1) V2(1)],[V1(2) V2(2)],[V1(3) V2(3)]); %creates a 3D line
% Reads drawline([x cord of start, x cord of end],
%
[y cord of start, y cord of end],
%
[z cord of start, z cord of end])
end
axis([-3 3 -3 3 -3 3])
% set the axis limits of the figure
% Visualizing a solid model
figure()
%creates a new figure window
patch('faces', F, 'vertices' ,V, 'facec', 'flat', 'FaceVertexCData', C, 'EdgeColor','black',
'LineWidth',2);
%the command takes the form patch(property, value , property, value....)
%Therefore the command reads as:
%create a patch such that
faces are F, vertices are V
%
face colors are flat, face color values are C
%
Edge colors are black, edges have a linewidth of 2
axis([-3 3 -3 3 -3 3])
% set the axis limits of the figure
© Copyright 2026 Paperzz