0% found this document useful (0 votes)
11 views9 pages

MATLAB SHEET 6 by NV

The document contains MATLAB code for visualizing a 3D box through various projections including side, front, top, trimetric, diametric, isometric, cavalier, and cabinet views. It defines functions for calculating box vertices, performing perspective projections, and drawing the box in 3D space. The code is organized into multiple sections, each addressing different projection techniques and visualizations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

MATLAB SHEET 6 by NV

The document contains MATLAB code for visualizing a 3D box through various projections including side, front, top, trimetric, diametric, isometric, cavalier, and cabinet views. It defines functions for calculating box vertices, performing perspective projections, and drawing the box in 3D space. The code is organized into multiple sections, each addressing different projection techniques and visualizations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MATLAB SHEET-6

Question:

Code:
clc;
l = 2;
w = 3;
h = 4;
%corners of the original box
original_box = [...
0 0 0;
l 0 0;
l w 0;
0 w 0;
0 0 h;
l 0 h;
l w h;
0 w h];
% edges of the box
edges = [1 2; 2 3; 3 4; 4 1;
5 6; 6 7; 7 8; 8 5;
1 5; 2 6; 3 7; 4 8];

pX=[0 0 0 ;
0 1 0;
0 0 1]
pA_box=original_box*pX;

pY=[1 0 0 ;
0 0 0;
0 0 1]
pB_box=original_box*pY;

pZ=[1 0 0 ;
0 1 0;
0 0 0];
pC_box=original_box*pZ;

plot_box = @(box, color) ...


arrayfun(@(i) plot3(box(edges(i,:),1), box(edges(i,:),2),
box(edges(i,:),3),...
'Color', color, 'LineWidth', 2), 1:size(edges,1));

subplot(2,2,1)
hold on;
plot_box(pB_box, 'r');
view(0,0);
axis equal;grid on;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Side view');
axis([0 4 0 4])

subplot(2,2,2)
hold on;
plot_box(pA_box, 'r');
view(90,0);
axis equal;grid on;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Front view');
axis([0 4 0 4])

subplot(2,2,3)
hold on;
plot_box(original_box, 'g');
view(3);
axis equal;grid on;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Original box');
axis([0 4 0 4])

subplot(2,2,4)
hold on;
plot_box(pC_box, 'r');
view(0,90);
grid on;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Top view');
axis([0 4 0 4])

Output:
Question:2

Code:
clc;
l = 2;
w = 3;
h = 4;
%corners of the original box
original_box = [...
0 0 0;
l 0 0;
l w 0;
0 w 0;
0 0 h;
l 0 h;
l w h;
0 w h];
% edges of the box
edges = [1 2; 2 3; 3 4; 4 1; % bottom face
5 6; 6 7; 7 8; 8 5; % top face
1 5; 2 6; 3 7; 4 8]; % vertical edges

A=45*pi/180;
B=45*pi/180;
t_diametric=[cos(B) sin(A)*sin(B) 0 0 ;
0 cos(A) 0 0;
sin(A) -sin(A)*cos(B) 0 0];
box_trimetric=original_box*t_diametric;
fz=0.5;

A= asin(fz/(2)^(1/2)) ;
B=asin(fz/(2-fz^2)^(1/2));
t_diametric=[cos(B) sin(A)*sin(B) 0 0 ;
0 cos(A) 0 0;
sin(A) -sin(A)*cos(B) 0 0];
box_diametric=original_box*t_diametric;

A=35.26*pi/180;
B=45*pi/180;
t_isometric=[cos(B) sin(A)*sin(B) 0 0 ;
0 cos(A) 0 0;
sin(A) -sin(A)*cos(B) 0 0];
box_isometric=original_box*t_isometric;

% plotting the boxes


plot_box = @(box, color) ...
arrayfun(@(i) plot3(box(edges(i,:),1), box(edges(i,:),2),
box(edges(i,:),3),...
'Color', color, 'LineWidth', 2), 1:size(edges,1));

subplot(2,2,1)
hold on;
plot_box(original_box, 'g');
view(3);
axis equal;grid on;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Original box');

subplot(2,2,2)
hold on;
plot_box(box_trimetric, 'r');
view(3);
axis equal;grid on;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Trimetric view');

subplot(2,2,3)
hold on;
plot_box(box_diametric, 'r');
view(3);
axis equal;grid on;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Diametric view');

subplot(2,2,4)
hold on;
plot_box(box_isometric, 'r');
view(3);
axis equal;grid on;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Isometric view');

Output:
Question:3

Code:
clc;
l = 2;
w = 3;
h = 4;
original_box = [...
0 0 0;
l 0 0;
l w 0;
0 w 0;
0 0 h;
l 0 h;
l w h;
0 w h];
% faces
faces=[ 1 2 3 4; 5 6 7 8; 1 2 6 5; 3 4 8 7; 4 1 5 8];

% Cavalier
f=1;
A=45*pi/180;

a=f*cos(A);
b=f*sin(A);
t_oblique=[1 0 0 ;
0 1 0 ;
a b 0 ];

box_cavalier=original_box*t_oblique;

% Cabinet
f=0.5;
A=45*pi/180;

a=f*cos(A);
b=f*sin(A);
t_oblique=[1 0 0 ;
0 1 0 ;
a b 0 ];

box_cabinet=original_box*t_oblique;

% plotting
figure;
subplot(2,2,[1,2]);
patch('Vertices',original_box, 'Faces',faces, 'FaceColor','g','FaceAlpha',0.3);
title('Original Box');
axis equal;
view(3);
subplot(2,2,3);
patch('Vertices',box_cavalier, 'Faces',faces, 'FaceColor','b','FaceAlpha',0.3);
title('Cavalier projection');
axis equal;
view(3);
subplot(2,2,4);
patch('Vertices',box_cabinet, 'Faces',faces, 'FaceColor','b','FaceAlpha',0.3);
title(' Cabinet projection');
axis equal;
view(3);

Output:
Question:4

Code:
% Define box dimensions
l = 1; % Length
w = 2; % Width
h = 3; % Height

% Define the box vertices function


function vertices = box_vertices(l, w, h, origin)
vertices = [
origin(1), origin(2), origin(3); % Vertex 1
origin(1) + l, origin(2), origin(3); % Vertex 2
origin(1) + l, origin(2) + w, origin(3); % Vertex 3
origin(1), origin(2) + w, origin(3); % Vertex 4
origin(1), origin(2), origin(3) + h; % Vertex 5
origin(1) + l, origin(2), origin(3) + h; % Vertex 6
origin(1) + l, origin(2) + w, origin(3) + h; % Vertex 7
origin(1), origin(2) + w, origin(3) + h % Vertex 8
]';
end

% Define the perspective projection function


function projection = perspective_projection(vertices, cop)

for i = 1:size(vertices, 2)
X = vertices(1, i);
Y = vertices(2, i);
Z = vertices(3, i);

Xp = (cop(3) * X) / (Z - cop(3));
Yp = (cop(3) * Y) / (Z - cop(3));

projection(:, i) = [Xp; Yp]


end
end

% Define the draw box function


function draw_box(vertices, subplot_num, title_str)
subplot(2, 2, subplot_num);

% Draw the box edges


edges = [
1 2; 2 3; 3 4; 4 1;
5 6; 6 7; 7 8; 8 5;
1 5; 2 6; 3 7; 4 8
];

for i = 1:size(edges, 1)
plot3(vertices(1, edges(i, :)), vertices(2, edges(i, :)), vertices(3,
edges(i, :)), 'k-');
hold on;
end

title(title_str);
xlabel('X'); ylabel('Y'); zlabel('Z');
grid on;
axis equal;
end

%One vertex at the origin


origin_a = [0, 0, 0];
vertices_a = box_vertices(l, w, h, origin_a);
cop_a = [2, 2, -5];
projection_a = perspective_projection(vertices_a, cop_a);

%Centroid at the origin


origin_b = [-l/2, -w/2, -h/2];
vertices_b = box_vertices(l, w, h, origin_b);
cop_b = [2, 2, -5];
projection_b = perspective_projection(vertices_b, cop_b);

%Plotting
figure;

% Plot original boxes


draw_box(vertices_a, 1, 'Original Box (Vertex at Origin)');
draw_box(vertices_b, 2, 'Original Box (Centroid at Origin)');

% Plot projections
subplot(2, 2, 3);
edges = [
1 2; 2 3; 3 4; 4 1;
5 6; 6 7; 7 8; 8 5;
1 5; 2 6; 3 7; 4 8
];
for i = 1:size(edges, 1)
plot(projection_a(1, edges(i, :)), projection_a(2, edges(i, :)), 'r-');
hold on;
end
title('Perspective Projection (Vertex at Origin)');
xlabel('Xp'); ylabel('Yp');
grid on;
axis equal;

subplot(2, 2, 4);
for i = 1:size(edges, 1)
plot(projection_b(1, edges(i, :)), projection_b(2, edges(i, :)), 'b-');
hold on;
end
title('Perspective Projection (Centroid at Origin)');
xlabel('Xp'); ylabel('Yp');
grid on;
axis equal;
Output:

You might also like