Documente Academic
Documente Profesional
Documente Cultură
Submitted by
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;
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
Dx = 3; % X Coordinate of Point D
Dy = 4; % Y Coordinate of Point D
% 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);
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;
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
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)
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);
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;
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
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
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)
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);
FUNCTION CODE
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;
SCRIPT
clc
clear all
close all
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;
SCRIPT
% Script file for normalized cubic spline curve
clc
clear all
close all
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];
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
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
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
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