Sunteți pe pagina 1din 50

Submitted in fulfillment of the requirements of

DEZG622 – Computer Aided Analysis and Design

Boot Camp Lab Session I - Assignment

Submitted by

Name : NILADRI BHATTACHARYYA


ID: 2018HT30159

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI CAMPUS

First Semester 2018-2019


Given Problem
Tutorial 1: Develop a MATLAB code to perform the following
transformations on a rectangle defined by the points (3, 1) (3, 4), (8, 4) and
(8, 1):
a) Only translation of the whole body in x and y directions by (2, 2).
b) Only shear the rectangle with shy = 1.5.
c) Only rotate the rectangle about z-axis by 450
d) Apply all the above transformations in the same order (a) to (c).

GIVEN FIGURE
SCRIPT FILE
clc % To clear the screen
clear all % To clear all the variables from workspace
close all % To close all the figure windows
Ax = 3; % X Coordinate of Point A
Ay = 1; % Y Coordinate of Point A

Bx = 3; % X Coordinate of Point B
By = 4; % Y Coordinate of Point B

Cx = 8; % X Coordinate of Point C
Cy = 4; % Y Coordinate of Point C

Dx = 8; % X Coordinate of Point D
Dy = 1; % Y Coordinate of Point D
plot([Ax,Bx],[Ay,By],'b') % Line between point A and B
%axis([-10 20 -10 20])
hold on % To hold figure handle on same figure
plot([Bx,Cx],[By,Cy],'b') % Line between point B and C
hold on
plot([Cx,Dx],[Cy,Dy],'b') % Line between point C and D
hold on
plot([Dx,Ax],[Dy,Ay],'b') % Line between point D and A
hold on
% Transformation of all points of rectangle

% Tutorial 1(a)
Aold = [Ax;Ay;1];
Bold = [Bx;By;1];
Cold = [Cx;Cy;1];
Dold = [Dx;Dy;1];
OldPos = [Aold,Bold,Cold,Dold];
NewPos= trans2d(3,OldPos);
% OldPos = NewPos;
% NewPos= trans2d(3,OldPos);
% OldPos = NewPos;
% NewPos= trans2d(2,OldPos);
plot([Axnew,Bxnew],[Aynew,Bynew],'r') % Line between point A and B
%axis([-10 20 -10 20])
hold on % To hold figure handle on same figure
plot([Bxnew,Cxnew],[Bynew,Cynew],'r') % Line between point B and C
hold on
plot([Cxnew,Dxnew],[Cynew,Dynew],'r') % Line between point C and D
hold on
plot([Dxnew,Axnew],[Dynew,Aynew],'r') % Line between point D and A

FUNCTION FILE :
%Function file for various 2D transformations
function [newpoint] = trans2d(type,oldpoint)

if type == 1 % Translation
p = 2;
q = 2;
TRANS = [1 0 p;...
0 1 q;...
0 0 1];
MAT = TRANS;

elseif type == 2 % Rotation


theta = 45;
ROT = [cosd(theta) -sind(theta) 0;...
sind(theta) cosd(theta) 0;...
0 0 1];
MAT = ROT;

elseif type == 3 % Shear in y direction


shy = 1.5;
SHEARY = [1 0 0;...
shy 1 0;...
0 0 1];
MAT = SHEARY;
end
newpoint = MAT*oldpoint;
end
SOLUTIONS
Tutorial 1
TRANSLATION IN X AND Y
SHEAR 1.5
Rotate the rectangle about z-axis by 450
A TO C
Problem 1: Develop a MATLAB code to perform the following
transformations on a trapezoid defined by the points (6, 1) (8, 1), (10, 4)
and (3, 4).
a) Translation of all the points by (5, 5) in x and y directions.
b) Rotate the hexagon about z-axis by 900
c) Reflect the whole body about y-axis.
d) Apply all the above transformations in the same order (a) to (c)
SCRIPT

clc % To clear the screen


clear all % To clear all the variables from workspace
close all % To close all the figure windows

Ax = 6; % X Coordinate of Point A
Ay = 1; % Y Coordinate of Point A

Bx = 8; % X Coordinate of Point B
By = 1; % Y Coordinate of Point B

Cx = 10; % X Coordinate of Point C


Cy = 4; % Y Coordinate of Point C

Dx = 3; % X Coordinate of Point D
Dy = 4; % Y Coordinate of Point D

plot([Ax,Bx],[Ay,By],'b') % Line between point A and B


%axis([-10 20 -10 20])
hold on % To hold figure handle on same figure
plot([Bx,Cx],[By,Cy],'b') % Line between point B and C
hold on
plot([Cx,Dx],[Cy,Dy],'b') % Line between point C and D
hold on
plot([Dx,Ax],[Dy,Ay],'b') % Line between point D and A
hold on

% Transformation of all points of rectangle

% Tutorial 1(a)
Aold = [Ax;Ay;1];
Bold = [Bx;By;1];
Cold = [Cx;Cy;1];
Dold = [Dx;Dy;1];
OldPos = [Aold,Bold,Cold,Dold];
NewPos= trans2d(1,OldPos);

OldPos = NewPos;
NewPos= trans2d(2,OldPos);

OldPos = NewPos;
NewPos= trans2d(3,OldPos);

Axnew = NewPos(1,1);
Aynew = NewPos(2,1);
Bxnew = NewPos(1,2);
Bynew = NewPos(2,2);
Cxnew = NewPos(1,3);
Cynew = NewPos(2,3);
Dxnew = NewPos(1,4);
Dynew = NewPos(2,4);

plot([Axnew,Bxnew],[Aynew,Bynew],'r') % Line between point A and B


%axis([-10 20 -10 20])
hold on % To hold figure handle on same figure
plot([Bxnew,Cxnew],[Bynew,Cynew],'r') % Line between point B and C
hold on
plot([Cxnew,Dxnew],[Cynew,Dynew],'r') % Line between point C and D
hold on
plot([Dxnew,Axnew],[Dynew,Aynew],'r') % Line between point D and A

FUNCTION FILE
%Function file for various 2D transformations
function [newpoint] = trans2d(type,oldpoint)

if type == 1 % Translation
p = 5;
q = 5;
TRANS = [1 0 p;...
0 1 q;...
0 0 1];
MAT = TRANS;

elseif type == 2 % Rotation


theta = 90;
ROT = [cosd(theta) -sind(theta) 0;...
sind(theta) cosd(theta) 0;...
0 0 1];
MAT = ROT;

elseif type == 3 % Reflect about Y-axis


REFLECTY = [-1 0 0;...
0 1 0;...
0 0 1];
MAT = REFLECTY;
end
newpoint = MAT*oldpoint;
end
TRANSLATION OF ALL POINTS
ROTATE Z AXIS BY 90 DEGREE
RFLECT WHOLE BODY ABOUT Y AXIS
A TO C
Tutorial 2
Given Problem
Three Dimensional Transformations and Projections Tutorial 2: Develop a
MATLAB code to perform the following transformations on a unit cube
defined by the points A(0, 0, 0), B(0, 1, 0), C(1, 1, 0), D(1, 0, 0), E(0, 0, 1) ,
F(0 ,1 ,1), G(1, 0, 1) and H(1, 1, 1):
a) Only translation of the whole body in x, y and z directions by (1, 1, 1).
b) Only rotate the cube about z-axis by 450 in anti-clockwise direction.
c) Apply transformations in (a) and (b) and then take projection of all the
points of the cube on x-y (z = 0) plane
Script

clc % To clear the screen


clear all % To clear all the variables from workspace
close all % To close all the figure windows

Ax = 0; % X Coordinate of Point A
Ay = 0; % Y Coordinate of Point A
Az = 0; % Z Coordinate of Point A

Bx = 0; % X Coordinate of Point B
By = 1; % Y Coordinate of Point B
Bz = 0; % Z Coordinate of Point B

Cx = 1; % X Coordinate of Point C
Cy = 1; % Y Coordinate of Point C
Cz = 0; % Z Coordinate of Point C

Dx = 1; % X Coordinate of Point D
Dy = 0; % Y Coordinate of Point D
Dz = 0; % Z Coordinate of Point D

Ex = 0; % X Coordinate of Point E
Ey = 0; % Y Coordinate of Point E
Ez = 1; % Z Coordinate of Point E

Fx = 0; % X Coordinate of Point F
Fy = 1; % Y Coordinate of Point F
Fz = 1; % Z Coordinate of Point F

Gx = 1; % X Coordinate of Point G
Gy = 1; % Y Coordinate of Point G
Gz = 1; % Z Coordinate of Point G
Hx = 1; % X Coordinate of Point H
Hy = 0; % Y Coordinate of Point H
Hz = 1; % Z Coordinate of Point H

plot3([Ax,Bx],[Ay,By],[Az,Bz],'b') % Line between point A and B


%axis([0 3 0 2 0 2 ])
hold on % To hold figure handle on same figure
plot3([Bx,Cx],[By,Cy],[Bz,Cz],'b') % Line between point B and C
hold on
plot3([Cx,Dx],[Cy,Dy],[Cz,Dz],'b') % Line between point C and D
hold on
plot3([Dx,Ex],[Dy,Ay],[Dz,Az],'b') % Line between point D and A
hold on
plot3([Ex,Fx],[Ey,Fy],[Ez,Fz],'b') % Line between point E and F
hold on
plot3([Fx,Gx],[Fy,Gy],[Fz,Gz],'b') % Line between point F and G
hold on
plot3([Gx,Hx],[Gy,Hy],[Gz,Hz],'b') % Line between point G and H
hold on
plot3([Hx,Ex],[Hy,Ey],[Hz,Ez],'b') % Line between point H and E
hold on
plot3([Ex,Ax],[Ey,Ay],[Ez,Az],'b') % Line between point E and A
hold on
plot3([Fx,Bx],[Fy,By],[Fz,Bz],'b') % Line between point F and B
hold on
plot3([Gx,Cx],[Gy,Cy],[Gz,Cz],'b') % Line between point G and C
hold on
plot3([Hx,Dx],[Hy,Dy],[Hz,Dz],'b') % Line between point H and D
hold on

text(Ax,Ay,Az,'A','fontsize',16)
text(Bx,By,Bz,'B','fontsize',16)
text(Cx,Cy,Cz,'C','fontsize',16)
text(Dx,Dy,Dz,'D','fontsize',16)
text(Ex,Ey,Ez,'E','fontsize',16)
text(Fx,Fy,Fz,'F','fontsize',16)
text(Gx,Gy,Gz,'G','fontsize',16)
text(Hx,Hy,Hz,'H','fontsize',16)

% Transformation of all points of cube


Aold = [Ax;Ay;Az;1];
Bold = [Bx;By;Bz;1];
Cold = [Cx;Cy;Cz;1];
Dold = [Dx;Dy;Dz;1];
Eold = [Ex;Ey;Ez;1];
Fold = [Fx;Fy;Fz;1];
Gold = [Gx;Gy;Gz;1];
Hold = [Hx;Hy;Hz;1];

OldPos = [Aold,Bold,Cold,Dold,Eold,Fold,Gold,Hold];
NewPos= trans3d(1,OldPos);
OldPos = NewPos;
NewPos= trans3d(2,OldPos);
OldPos = NewPos;
NewPos= trans3d(3,OldPos);

Axnew = NewPos(1,1);
Aynew = NewPos(2,1);
Aznew = NewPos(3,1);

Bxnew = NewPos(1,2);
Bynew = NewPos(2,2);
Bznew = NewPos(3,2);

Cxnew = NewPos(1,3);
Cynew = NewPos(2,3);
Cznew = NewPos(3,3);

Dxnew = NewPos(1,4);
Dynew = NewPos(2,4);
Dznew = NewPos(3,4);

Exnew = NewPos(1,5);
Eynew = NewPos(2,5);
Eznew = NewPos(3,5);

Fxnew = NewPos(1,6);
Fynew = NewPos(2,6);
Fznew = NewPos(3,6);

Gxnew = NewPos(1,7);
Gynew = NewPos(2,7);
Gznew = NewPos(3,7);

Hxnew = NewPos(1,8);
Hynew = NewPos(2,8);
Hznew = NewPos(3,8);

plot3([Axnew,Bxnew],[Aynew,Bynew],[Aznew,Bznew],'r') % Line between


point A and B
%axis([0 3 0 2 0 2 ])
hold on % To hold figure handle on same figure
plot3([Bxnew,Cxnew],[Bynew,Cynew],[Bznew,Cznew],'r') % Line between
point B and C
hold on
plot3([Cxnew,Dxnew],[Cynew,Dynew],[Cznew,Dznew],'r') % Line between
point C and D
hold on
plot3([Dxnew,Exnew],[Dynew,Aynew],[Dznew,Aznew],'r') % Line between
point D and A
hold on
plot3([Exnew,Fxnew],[Eynew,Fynew],[Eznew,Fznew],'r') % Line between
point E and F
hold on
plot3([Fxnew,Gxnew],[Fynew,Gynew],[Fznew,Gznew],'r') % Line between
point F and G
hold on
plot3([Gxnew,Hxnew],[Gynew,Hynew],[Gznew,Hznew],'r') % Line between
point G and H
hold on
plot3([Hxnew,Exnew],[Hynew,Eynew],[Hznew,Eznew],'r') % Line between
point H and E
hold on
plot3([Exnew,Axnew],[Eynew,Aynew],[Eznew,Aznew],'r') % Line between
point E and A
hold on
plot3([Fxnew,Bxnew],[Fynew,Bynew],[Fznew,Bznew],'r') % Line between
point F and B
hold on
plot3([Gxnew,Cxnew],[Gynew,Cynew],[Gznew,Cznew],'r') % Line between
point G and C
hold on
plot3([Hxnew,Dxnew],[Hynew,Dynew],[Hznew,Dznew],'r') % Line between
point H and D

hold on
text(Axnew,Aynew,Aznew,'A','fontsize',16)
text(Bxnew,Bynew,Bznew,'B','fontsize',16)
text(Cxnew,Cynew,Cznew,'C','fontsize',16)
text(Dxnew,Dynew,Dznew,'D','fontsize',16)
text(Exnew,Eynew,Eznew,'E','fontsize',16)
text(Fxnew,Fynew,Fznew,'F','fontsize',16)
text(Gxnew,Gynew,Gznew,'G','fontsize',16)
text(Hxnew,Hynew,Hznew,'H','fontsize',16)

FUNCTION CODE
%Define the transformation matrices here
function [newpoint] = trans3d(type,oldpoint)

if type == 1 % Translation
p = 1;
q = 1;
r = 1;
TRANS = [1 0 0 p;...
0 1 0 q;...
0 0 1 r;...
0 0 0 1];
MAT = TRANS;

elseif type == 2 % Rotation


theta = 45;
ROT = [cosd(theta) -sind(theta) 0 0;...
sind(theta) cosd(theta) 0 0;...
0 0 1 0;...
0 0 0 1];
MAT = ROT;

elseif type == 3 % Projection on xy plane


PROJXY = [1 0 0 0;...
0 1 0 0;...
0 0 0 0;...
0 0 0 1];
MAT = PROJXY;
end
newpoint = MAT*oldpoint;
end
Only translation of the whole body in x, y and z directions by
(1, 1, 1)
ROTATE the cube about z-axis by 450 in anti-clockwise
direction.
Apply transformations in (a) and (b) and then take projection
of all the points of the cube on x-y (z = 0) plane.
Problem 2

Problem 2: The vertices of a block with one corner removed are given
as A( 2, 1, 1), B (3, 1, 2), C (3, 1.5, 2), D (2.5, 2, 2), E (2, 2, 2), F (2,
1, 1), G (3, 1, 1), H (3, 2, 1), I (2, 1, 1), J (3, 2, 1.5). Perform the
following transformations on the body shown in the figure below.
a) Rotate the body by 900 about z-axis.
b) Orthographic projection of the body on x-y plane (z = 0).
c) Orthographic projection of the body on y-z plane (x = 0)
SCRIPT
clc % To clear the screen
clear all % To clear all the variables from workspace
close all % To close all the figure windows

Ax = 2; % X Coordinate of Point A
Ay = 1; % Y Coordinate of Point A
Az = 2; % Z Coordinate of Point A

Bx = 3; % X Coordinate of Point B
By = 1; % Y Coordinate of Point B
Bz = 2; % Z Coordinate of Point B

Cx = 3; % X Coordinate of Point C
Cy = 1.5; % Y Coordinate of Point C
Cz = 2; % Z Coordinate of Point C

Dx = 2.5; % X Coordinate of Point D


Dy = 2; % Y Coordinate of Point D
Dz = 2; % Z Coordinate of Point D

Ex = 2; % X Coordinate of Point E
Ey = 2; % Y Coordinate of Point E
Ez = 2; % Z Coordinate of Point E

Fx = 2; % X Coordinate of Point F
Fy = 1; % Y Coordinate of Point F
Fz = 1; % Z Coordinate of Point F

Gx = 3; % X Coordinate of Point G
Gy = 1; % Y Coordinate of Point G
Gz = 1; % Z Coordinate of Point G

Hx = 3; % X Coordinate of Point H
Hy = 2; % Y Coordinate of Point H
Hz = 1; % Z Coordinate of Point H

Ix = 2; % X Coordinate of Point I
Iy = 2; % Y Coordinate of Point I
Iz = 1; % Z Coordinate of Point I

Jx = 3; % X Coordinate of Point J
Jy = 2; % Y Coordinate of Point J
Jz = 1.5; % Z Coordinate of Point J

plot3([Ax,Bx],[Ay,By],[Az,Bz],'b') % Line between point A and B


%axis([0 3 0 2 0 2 ])
hold on % To hold figure handle on same figure
plot3([Bx,Cx],[By,Cy],[Bz,Cz],'b') % Line between point B and C
hold on
plot3([Cx,Dx],[Cy,Dy],[Cz,Dz],'b') % Line between point C and D
hold on
plot3([Cx,Jx],[Cy,Jy],[Cz,Jz],'b') % Line between point C and J
hold on
plot3([Jx,Dx],[Jy,Dy],[Jz,Dz],'b') % Line between point J and D
hold on
plot3([Dx,Ex],[Dy,Ey],[Dz,Ez],'b') % Line between point D and E
hold on
plot3([Ex,Ax],[Ey,Ay],[Ez,Az],'b') % Line between point E and A
hold on
plot3([Ax,Fx],[Ay,Fy],[Az,Fz],'b') % Line between point A and F
hold on
plot3([Fx,Gx],[Fy,Gy],[Fz,Gz],'b') % Line between point F and G
hold on
plot3([Gx,Hx],[Gy,Hy],[Gz,Hz],'b') % Line between point G and H
hold on
plot3([Hx,Ix],[Hy,Iy],[Hz,Iz],'b') % Line between point H and I
hold on
plot3([Ix,Fx],[Iy,Fy],[Iz,Fz],'b') % Line between point I and F
hold on
plot3([Bx,Gx],[By,Gy],[Bz,Gz],'b') % Line between point B and G
hold on
plot3([Ex,Ix],[Ey,Iy],[Ez,Iz],'b') % Line between point E and I
hold on
plot3([Hx,Jx],[Hy,Jy],[Hz,Jz],'b') % Line between point H and J
hold on

text(Ax,Ay,Az,'A','fontsize',16)
text(Bx,By,Bz,'B','fontsize',16)
text(Cx,Cy,Cz,'C','fontsize',16)
text(Dx,Dy,Dz,'D','fontsize',16)
text(Ex,Ey,Ez,'E','fontsize',16)
text(Fx,Fy,Fz,'F','fontsize',16)
text(Gx,Gy,Gz,'G','fontsize',16)
text(Hx,Hy,Hz,'H','fontsize',16)
text(Ix,Iy,Iz,'I','fontsize',16)
text(Jx,Jy,Jz,'J','fontsize',16)

% Transformation of all points of cube

Aold = [Ax;Ay;Az;1];
Bold = [Bx;By;Bz;1];
Cold = [Cx;Cy;Cz;1];
Dold = [Dx;Dy;Dz;1];
Eold = [Ex;Ey;Ez;1];
Fold = [Fx;Fy;Fz;1];
Gold = [Gx;Gy;Gz;1];
Hold = [Hx;Hy;Hz;1];
Iold = [Ix;Iy;Iz;1];
Jold = [Jx;Jy;Jz;1];

OldPos = [Aold,Bold,Cold,Dold,Eold,Fold,Gold,Hold,Iold,Jold];
NewPos= trans3d(3,OldPos);
% OldPos = NewPos;
% NewPos= trans3d(2,OldPos);
% OldPos = NewPos;
% NewPos= trans3d(3,OldPos);

Axnew = NewPos(1,1);
Aynew = NewPos(2,1);
Aznew = NewPos(3,1);

Bxnew = NewPos(1,2);
Bynew = NewPos(2,2);
Bznew = NewPos(3,2);
Cxnew = NewPos(1,3);
Cynew = NewPos(2,3);
Cznew = NewPos(3,3);

Dxnew = NewPos(1,4);
Dynew = NewPos(2,4);
Dznew = NewPos(3,4);

Exnew = NewPos(1,5);
Eynew = NewPos(2,5);
Eznew = NewPos(3,5);

Fxnew = NewPos(1,6);
Fynew = NewPos(2,6);
Fznew = NewPos(3,6);

Gxnew = NewPos(1,7);
Gynew = NewPos(2,7);
Gznew = NewPos(3,7);

Hxnew = NewPos(1,8);
Hynew = NewPos(2,8);
Hznew = NewPos(3,8);

Ixnew = NewPos(1,9);
Iynew = NewPos(2,9);
Iznew = NewPos(3,9);

Jxnew = NewPos(1,10);
Jynew = NewPos(2,10);
Jznew = NewPos(3,10);

plot3([Axnew,Bxnew],[Aynew,Bynew],[Aznew,Bznew],'r') % Line between


point A and B
%axis([0 3 0 2 0 2 ])
hold on % To hold figure handle on same figure
plot3([Bxnew,Cxnew],[Bynew,Cynew],[Bznew,Cznew],'r') % Line between
point B and C
hold on
plot3([Cxnew,Dxnew],[Cynew,Dynew],[Cznew,Dznew],'r') % Line between
point C and D
hold on
plot3([Cxnew,Jxnew],[Cynew,Jynew],[Cznew,Jznew],'r') % Line between
point C and J
hold on
plot3([Jxnew,Dxnew],[Jynew,Dynew],[Jznew,Dznew],'r') % Line between
point J and D
hold on
plot3([Dxnew,Exnew],[Dynew,Eynew],[Dznew,Eznew],'r') % Line between
point D and E
hold on
plot3([Exnew,Axnew],[Eynew,Aynew],[Eznew,Aznew],'r') % Line between
point E and A
hold on
plot3([Axnew,Fxnew],[Aynew,Fynew],[Aznew,Fznew],'r') % Line between
point A and F
hold on
plot3([Fxnew,Gxnew],[Fynew,Gynew],[Fznew,Gznew],'r') % Line between
point F and G
hold on
plot3([Gxnew,Hxnew],[Gynew,Hynew],[Gznew,Hznew],'r') % Line between
point G and H
hold on
plot3([Hxnew,Ixnew],[Hynew,Iynew],[Hznew,Iznew],'r') % Line between
point H and I
hold on
plot3([Ixnew,Fxnew],[Iynew,Fynew],[Iznew,Fznew],'r') % Line between
point I and F
hold on
plot3([Bxnew,Gxnew],[Bynew,Gynew],[Bznew,Gznew],'r') % Line between
point B and G
hold on
plot3([Exnew,Ixnew],[Eynew,Iynew],[Eznew,Iznew],'r') % Line between
point E and I
hold on
plot3([Hxnew,Jxnew],[Hynew,Jynew],[Hznew,Jznew],'r') % Line between
point H and J
hold on
text(Axnew,Aynew,Aznew,'A','fontsize',16)
text(Bxnew,Bynew,Bznew,'B','fontsize',16)
text(Cxnew,Cynew,Cznew,'C','fontsize',16)
text(Dxnew,Dynew,Dznew,'D','fontsize',16)
text(Exnew,Eynew,Eznew,'E','fontsize',16)
text(Fxnew,Fynew,Fznew,'F','fontsize',16)
text(Gxnew,Gynew,Gznew,'G','fontsize',16)
text(Hxnew,Hynew,Hznew,'H','fontsize',16)
text(Ixnew,Iynew,Iznew,'I','fontsize',16)
text(Jxnew,Jynew,Jznew,'J','fontsize',16)

FUNCTION CODE

%Define the transformation matrices here


function [newpoint] = trans3d(type,oldpoint)

if type == 1 % Rotation
theta = 90;
ROT = [cosd(theta) -sind(theta) 0 0;...
sind(theta) cosd(theta) 0 0;...
0 0 1 0;...
0 0 0 1];
MAT = ROT;

elseif type == 2 % Projection on xy plane


PROJXY = [1 0 0 0;...
0 1 0 0;...
0 0 0 0;...
0 0 0 1];
MAT = PROJXY;

elseif type == 3 % Projection on yz plane


PROJYZ = [0 0 0 0;...
0 1 0 0;...
0 0 1 0;...
0 0 0 1];
MAT = PROJYZ;
end
newpoint = MAT*oldpoint;
end

a) Rotate the body by 90o about z-axis


b) Orthographic projection of the body on x-y plane (z =
0).
c) Orthographic projection of the body on y-z plane (x =
0)
Tutorial 3
Using MATLAB, generate an ellipse with a = 4 and b = 1 inclined at 300 to
xaxis with centre at (2, 2) assuming ‘n’ number of uniformly spaced points
on circumference. The parametric equation for an ellipse with centre at (0
,0) is given by: 𝑥𝑖+1 = 𝑎 𝑐𝑜𝑠(𝜃𝑖 + 𝛿𝜃) 𝑦𝑖+1 = 𝑏 𝑠𝑖𝑛(𝜃𝑖 + 𝛿𝜃), 𝛿𝜃 = 2𝜋 𝑛−1 . To
generate the ellipse, perform the following transformations:
a) First rotate the ellipse with centre at (0, 0) about z-axis by 300
b) After rotation, translate the centre to (2, 2).

SCRIPT
clc
clear all
close all

np = 50; % Number of point on ellipse


deltheta = 360/(np-1); % Increment in Angle
thetai = 0; % Initial angle
a = 4; % Semi Major Axis
b = 1; % Semi Minor Axis
xc(1) = 4;
yc(1) = 0;
for k = 2:np
xi = a*cosd(thetai);
yi = b*sind(thetai);
xc(k) = xi*cosd(deltheta) - a/b*yi*sind(deltheta);
yc(k) = b/a*xi*sind(deltheta) + yi*cosd(deltheta);
thetai = thetai + deltheta;
end

figure(1); plot(xc,yc,'r.','Linewidth',1.5);
hold on

xp = [xc;yc;ones(1,length(xc))];
newcorA = trans2d(2,xp);
newx = newcorA(1,:);
newy = newcorA(2,:);
figure(1); plot(newx,newy,'b.','Linewidth',1.5);
hold on

newcorB = trans2d(1,newcorA);
newx = newcorB(1,:);
newy = newcorB(2,:);
figure(1); plot(newx,newy,'g.','Linewidth',1.5);

FUNCTION FILE
%Function file for various 2D transformations
function [newpoint] = trans2d(type,oldpoint)

if type == 1 % Translation
p = 2;
q = 2;
TRANS = [1 0 p;...
0 1 q;...
0 0 1];
MAT = TRANS;

elseif type == 2 % Rotation


theta = 30;
ROT = [cosd(theta) -sind(theta) 0;...
sind(theta) cosd(theta) 0;...
0 0 1];
MAT = ROT;

elseif type == 3 % Shear in y direction


shy = 1.5;
SHEARY = [1 0 0;...
shy 1 0;...
0 0 1];
MAT = SHEARY;
end
newpoint = MAT*oldpoint;
en

ROTATE ELLIPSE AT CENTRE (0,0)


TRANSLATE THE CENTRE (2,2)
Problem 3: a) Generate a normalized piecewise cubic spline curve through
the four points with position vectors P1 [0, 0 ], P2 [1, 1], P3 [2, -1], P4 [3, 0 ]
with tangent vectors 𝐏1 ′ = [1 1]and 𝐏𝟒 ′ = [1 1]. Take 20 points on each
segment.
b) Generate a normalized piecewise cubic spline curve through the four
points with position vectors P1 [1, 1], P2 [2, 3], P3 [4, -2], P4 [5, 1 ] with
tangent vectors 𝐏1 ′ = [0 1] and 𝐏𝟒 ′ = [−1 − 1]. Take 50 points on each
segment.

SCRIPT
% Script file for normalized cubic spline curve
clc
clear all
close all

P = [1 1 ;... % Points Given


2 3;...
4 -2;...
5 1];...

P1dash = [0 1]; %Tangent Vector at first point


P4dash = [-1 -1]; % Tangent Vector at last point

N=[2 -2 1 1;...
-3 3 -2 -1;...
0 0 1 0;...
1 0 0 0];

C = [ 1 0 0 0;...
1 4 1 0;...
0 1 4 1;...
0 0 0 1];

P1 = P(1,:);
P2 = P(2,:);
P3 = P(3,:);
P4 = P(4,:);

plot(P(:,1),P(:,2),'or')
hold on

Pdash = inv(C)*[P1dash;...
3*((P3-P2)+(P2-P1));... % Derivatives at all points
3*((P4-P3)+(P3-P2));...
P4dash];

np = 50; % Equidistant points on each segment


t = linspace(0,1,np);
for i = 1:3;
for j = 1:np;
T = [t(j)^3 t(j)^2 t(j) 1];
G = [P(i,:);P(i+1,:);Pdash(i,:);Pdash(i+1,:)];
PS(j,:) = T*N*G;
plot(PS(j,1),PS(j,2),'b.') % Plot various points on Cubic Spline
hold on
end

end
THROUGH THE GIVEN FOUR POINTS WITH 20 POINTS EACH
THROUGH THE GIVEN FOUR POINTS WITH 50 POINTS EACH
Tutorial 4
Tutorial 4: Write a MATLAB function to generate nth degree Bezier curve
Bezier curve which takes polygon vertices as inputs.
a) Use the same function to generate a Bezier curve defined by four
polygon vertices 𝐵0 [1, 1], 𝐵1 [2, 3], 𝐵2 [4, 3], 𝐵3 [3, 1]. Find 20 equidistant
points on the curve.

SCRIPT
clc
clear all
close all

B = [1,1;2,3;4,3;3,1]; % Vertices of Polygon


[r,s] = size(B);
n = r-1; % n + 1 = Number of Polygon Vertices
np = 20; %Number of equidistant points on Bezier Curve
t = linspace(0,1,np); %Value of parameter 't' for all the points on Curve

for k = 1:n
figure(1); plot([B(k,1),B(k+1,1)],[B(k,2),B(k+1,2)],'r')
hold on % To hold figure handle on same figure
end

for j = 1:np
P =[0,0];
for i = 0:n

J(i+1) = factorial(n)/(factorial(i)*(factorial(n-i)))*t(j)^i*(1-t(j))^(n-i);
P = P + B(i+1,:)*J(i+1);
end
Q(j,:) = P;
end

for l = 1:np-1
figure(1); plot([Q(l,1),Q(l+1,1)],[Q(l,2),Q(l+1,2)],'ob')
hold on % To hold figure handle on same figure
end

BEZIER CURVE
b) Use the same function to generate a Bezier curve defined by five
polygon vertices 𝐵0 [1, 1], 𝐵1 [2, 4], 𝐵2 [4, 5], 𝐵4 [5, 4], 𝐵5 [3, 1]. Find 30
equidistant points on the curve.

SCRIPT
clc
clear all
close all

B = [1,1;2,4;4,5;5,4;3,1]; % Vertices of Polygon


[r,s] = size(B);
n = r-1; % n + 1 = Number of Polygon Vertices
np = 30; %Number of equidistant points on Bezier Curve
t = linspace(0,1,np); %Value of parameter 't' for all the points on Curve

for k = 1:n
figure(1); plot([B(k,1),B(k+1,1)],[B(k,2),B(k+1,2)],'r')
hold on % To hold figure handle on same figure
end

for j = 1:np
P =[0,0];
for i = 0:n

J(i+1) = factorial(n)/(factorial(i)*(factorial(n-i)))*t(j)^i*(1-t(j))^(n-i);
P = P + B(i+1,:)*J(i+1);
end
Q(j,:) = P;
end

for l = 1:np-1
figure(1); plot([Q(l,1),Q(l+1,1)],[Q(l,2),Q(l+1,2)],'ob')
hold on % To hold figure handle on same figure
end
BEZIER CURVE
Problem 4: Write a script file in MATLAB to generate an open
uniform knot vector for a kth order B-spline curve with defining
polygon having n + 1 number of polygon vertices. Include two
examples in the output for (a) n = 4, k = 2 (b) n = 3, k = 4;

SCRIPT

clc% Clearing the screen


clear all %Clearing the workspace

n = input('Kindly enter the value of n ') %entering the value of n


k = input('Kindly enter the value of k ') %entering the value of k
z = n+k+1;
Kvector = zeros(1,z);

for i = 1:k
Kvector(i)=0;
end
for i = k+1:(n+1)
Kvector(i)= Kvector(i-1)+1;
end
Kvector(n+2)= Kvector(n+1)+1;
for i = n+3:n+k+1
Kvector(i) = Kvector(i-1);
end

Kvector %Final Knot Vector

S-ar putea să vă placă și