0% found this document useful (0 votes)
20 views47 pages

CAAD Report

Uploaded by

Nitin Yadav
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)
20 views47 pages

CAAD Report

Uploaded by

Nitin Yadav
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/ 47

MATLAB Tutorial & Assignment

Submitted in fulfillment of the requirements of


S1-22_DEZG622 - Computer Aided Analysis and Design

(Assignment)
By
Name: Manoj Kumar K
(ID NO: 2022HT30024)

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI CAMPUS


First Semester 2022-2023
Tutorial - 1
Write and execute a MATLAB program to generate a parametric ellipse (0≤u≤1) for a given user choice of
inclination angle, major axis and minor axis and center of the ellipse. Also draw a tangent vector at a
specified value of u. Validate the code with the following data.
Data: Inclination angle=30°; Centre= [30,40]; Major axis=150mm; Minor axis=100mm;
u=0.8(tangent vector)

clear all
close
clc
%%defining initial parameters
a = input('Enter the angle of inclination: ');
xc = input('Enter the X-coordinate of centre: ');
yc = input('Enter the Y-coordinate of centre: ');
A = input('Enter the major axis length: ');
B = input('Enter the minor axis length: ');
k = input('Location of tangent [0 -1]: ');
n = 1e6;
u = 0:1/n:1;
Sa = sind(a);
Ca = cosd(a);

%%computing ellipse using parametric form


for i=1:n+1
x(i) = xc + A*cos(2*pi*u(i))*Ca - B*sin(2*pi*u(i))*Sa;
y(i) = yc + A*cos(2*pi*u(i))*Sa + B*sin(2*pi*u(i))*Ca;
end

%%plotting the ellipse


title('Plot of ellipse using parametric equation')
plot(x,y,'b')
xlabel('x-axis')
ylabel('y-axis')
hold on
plot(xc,yc,'*')
hold on

Ck = cos(2*pi*k);
Sk = sin(2*pi*k);
xt = xc + A*Ck*Ca - B*Sk*Sa;
yt = yc + A*Ck*Sa + B*Sk*Ca;
quiver(xt,yt,-A*Sk*Ca - B*Ck*Sa,-A*Sk*Sa + B*Ck*Ca,'r') %plot tangent
axis equal
grid on
hold on
quiver(xc,yc,100*Ca,100*Sa,'k'); %plot major axis
hold on
quiver(xc,yc,100*cosd(90+a),100*sind(90+a),'k'); %plot minor axis
Assignment 1
Write and execute a MATLAB program for geometric modeling of a parametric circle with center at any
point {xc,yc}, radius R and lying in the X-Y plane. Test your program with R=40 mm and center at both the
origin and at {10,10} for estimating the point and tangent vector at any given parameter value 0<=u<=1.
Write the Matlab code for both the original parametric equation and computationally efficient parametric
equation. Compare the computational times.

clear all
close
clc
%% Initializing and Getting Input from the user
Xc = input('X coordinate:');
Yc = input('Y coordinate:');
r = input('Radius:');
n = 20000;
X = [];
Y = [];
% Xn=[];
% Yn=[];
u = input('Location of tangent at any point: ');
k = 0:1/n:1;
%% Circle parametric equation
tic
timerVal1 = tic;
% Orginal Parametric equation
tic0
for i = 1:n+1
x = Xc + r*cos(2*pi*k(i));
y = Yc + r*sin(2*pi*k(i));
% Storing the Data
X = [X;x];
Y = [Y;y];
end
toc
value1 = toc(timerVal1); % Time taken to complete the Program
%% Computationally efficient parametric equation
d =linspace(0,2*pi,n);
delta = d(2)-d(1);
x(1) = Xc+r;
y(1) = Yc+0;
tic
for j = 1:n-1
xn(j) = Xc+(X(j)-Xc)*cos(delta)-(Y(j)-Yc)*sin(delta);
yn(j) = Yc+(Y(j)-Yc)*cos(delta)+(X(j)-Xc)*sin(delta);
% Xn =[Xn;xn];
% Yn =[Yn;yn];
% plot(xn,yn,'r',LineWidth=1);
% hold on;
end
toc
%% Plotting the Figures
plot(X,Y,'-ob');
axis([-80 80 -80 80]);
hold on;
plot(Xc,Yc,'b*');
hold on;
plot(xn,yn,'r',’LineWidth’,1);
hold on;
%% Tangent at any point
xt = Xc + r*cos(2*pi*u);
yt = Yc + r*sin(2*pi*u);
plot(xt,yt);
hold on;
a = -r*sin(2*pi*u);
b = r*cos(2*pi*u);
quiver(xt,yt,a,b,'r',’LineWidth’,1.5); %plot tangent
axis equal
grid on
hold on
%% Circle with new parameters
Xc1 = input('New X coordinate:');
Yc1 = input('New Y coordinate:');
X1 = [];
Y1 = [];
% Xn1=[];
% Yn1=[];
k1 = 0:1/n:1;
%% Circle parametric equation
tic
timerVal3 = tic;
% Orginal Parametric equation
tic
for i = 1:n+1
x1 = Xc1 + r*cos(2*pi*k1(i));
y1 = Yc1 + r*sin(2*pi*k1(i));
% Storing the Data
X1 = [X1;x1];
Y1 = [Y1;y1];
end
toc
value3 = toc(timerVal3); % Time taken to complete the Program
%% Computationally efficient parametric equation
d =linspace(0,2*pi,n);
delta = d(2)-d(1);
x1(1) = Xc1+r;
y1(1) = Yc1+0;
tic
for j = 1:n-1
xn1(j) = Xc1+(X1(j)-Xc1)*cos(delta)-(Y1(j)-Yc1)*sin(delta);
yn1(j) = Yc1+(Y1(j)-Yc1)*cos(delta)+(X1(j)-Xc1)*sin(delta);
% Xn =[Xn;xn];
% Yn =[Yn;yn];
% plot(xn,yn,'r',’LineWidth’,1);
% hold on;
end
toc
%% Plotting the Figures
plot(X1,Y1,'-og');
axis([-60 60 -60 60]);
hold on;
plot(Xc1,Yc1,'g*');
hold on;
plot(xn1,yn1,'b',’LineWidth’,1);
hold on;
%% Tangent at any point
xt1 = Xc1 + r*cos(2*pi*u);
yt1 = Yc1 + r*sin(2*pi*u);
plot(xt1,yt1);
hold on;
a1 = -r*sin(2*pi*u);
b1 = r*cos(2*pi*u);
quiver(xt1,yt1,a1,b1,'r',’LineWidth’,1.5); %plot tangent
axis equal
grid on
hold on
Computation time at (0,0), sec Computation time at (10,10), sec
Original Parametric Equation 0.600904 0.606291
Efficient Parametric Equation 0.018537 0.013410

Conclusion:-
The computation time for computationally efficient parametric equation is approximately 97% lesser than
original parametric equation
Tutorial 2
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 P1′ = [1 1]and P4′ = [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 P1 ′ = [0 1] and P4′ = [−1 − 1]. Take 50 points on
each segment.

Hermite basis functions or blending functions

clc
clear all
close all

%% Inputs for Hermite curve


P = [0 0;1 1;2 -1;3 0]; %Points given
P1dash = [1 1]; %Tangent vector at first point
P4dash = [1 1]; %Tangent vector at last point
% Blending function matrix
N = [2 -2 1 1;-3 3 -2 -1;0 0 1 0;1 0 0 0];
% Intermediate tangent vector
C = [1 0 0 0;1 4 1 0;0 1 4 1;0 0 0 1];

%% Plot coordinates
plot(P(:,1),P(:,2),'or','LineWidth',2)
hold on

%% Find intermediate tangent vector


P1 = P(1,:);
P2 = P(2,:);
P3 = P(3,:);
P4 = P(4,:);
Pdash = inv(C) * [P1dash; 3*((P3-P2)+(P2-P1)); 3*((P4-P3)+(P3-P2)); P4dash];
%% Create a hermite curve segment
np = 20; %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*','LineWidth',2) %plot various points on cubic spline
hold on
end
end
Code for b)

clc
clear all
close all

%% Inputs for Hermite curve


P = [1 1;2 3;4 -2;5 1]; %Points given
P1dash = [0 1]; %Tangent vector at first point
P4dash = [-1 -1]; %Tangent vector at last point
% Blending function matrix
N = [2 -2 1 1;-3 3 -2 -1;0 0 1 0;1 0 0 0];
% Intermediate tangent vector
C = [1 0 0 0;1 4 1 0;0 1 4 1;0 0 0 1];

%% Plot coordinates
plot(P(:,1),P(:,2),'or','LineWidth',2)
hold on

%% Find intermediate tangent vector


P1 = P(1,:);
P2 = P(2,:);
P3 = P(3,:);
P4 = P(4,:);
Pdash = inv(C) * [P1dash; 3*((P3-P2)+(P2-P1)); 3*((P4-P3)+(P3-P2)); P4dash];

%% Create a hermite curve segment


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*','LineWidth',2) %plot various points on cubic spline
hold on
end
end
Assignment 2
a) Write and execute a MATLAB program to plot a planar parametric curve whose x- and y- axis modeled
using cubic polynomial of the form: X(u) =A*u^3+B*u+C Y(u) =E*u^3+F* u^2+G Where 0 ≤ u ≤ 1 is the
parameter, and A, B, C, E, F, G are the polynomial coefficients. Your program should work for any user
supplied input of these polynomial coefficients.
b) Write Matlab program to generate and plot a the Hermite cubic curve for any set of two control points
and two tangent vectors. Validate your code with sample data given below:
P0 = [1, 1], P’0 = [0.6, 0.8], P1= [8, 2] and P’1 = [-0.4472, -0.8944].
Also include in the program the facility of putting point at the specified u-value and an arrow for the tangent
vector at that point.

MATLAB Code for Q) a:-


clc
clear all
close all
%% Initializing and getting Inputs from the user
A = input('Value of coeff A:');
B = input('Value of coeff B:');
C = input('Value of coeff C:');
E = input('Value of coeff E:');
F = input('Value of coeff F:');
G = input('Value of coeff G:');
n = 100;
u = 0:1/n:1;
%% Solving the Parametric equation
for i = 1:n+1
x(i) = A*((u(i))^3)+B*(u(i))+C;
y(i) = E*((u(i))^3)+F*(u(i)^2)+G;
end
%% Plotting the cubic curve
plot(x,y,'b',LineWidth=1);
axis equal
hold on;
MATLAB Code for Q) b:-
clc
clear all
close all
%% Input data
P = [1 1;8 2]; % Points Given
P0 = P(1,:);
P1 = P(2,:);
P0dash = [0.6 0.8]; %Tangent Vector at first point
P1dash = [-0.4472 -0.8944]; % Tangent Vector at last point
Hm = [ 2 -2 1 1; -3 3 -2 -1; 0 0 1 0; 1 0 0 0];%Hermite Matrix
Hmt = [0 6 -6 0; 0 -6 6 0; 0 3 -4 1; 0 3 -2 0]; %Tangent matrix
plot(P(:,1),P(:,2),'or','LineWidth',2)
hold on;
np = 1000; % Equidistant points on each segment
u = linspace(0,1,np);
V = [P(1,:);P(2,:);P0dash;P1dash];% V Matrix
%% Solving the cubic curve equation
for j = 1:np
U = [u(j)^3 u(j)^2 u(j) 1];
PS(j,:) = U*Hm*V;% [P] Matrix
plot(PS(j,1),PS(j,2),'r*','LineWidth',2) % Plot various points on Cubic Spline
hold on
end
%% Tangent at any point and its tangent vector
t = input("tanget at any point[0-1]:");
U = [(t)^3 (t)^2 (t) 1];
Pt = U*Hm*V;
x = Pt(1,1);
y = Pt(1,2);
plot(x, y,'bo','LineWidth',2);
hold on;
xt = ((6*t^2)-(6*t))*P0(1,1)+((-6*t^2)+(6*t))*P1(1,1) +((3*t^2)-(4*t)+1)*P0dash(1,1) + ((3*t^2)-
(2*t))*P1dash(1,1);
yt = ((6*t^2)-(6*t))*P0(1,2)+((-6*t^2)+(6*t))*P1(1,2)+((3*t^2)-(4*t)+1)*P0dash(1,2) + ((3*t^2)-
(2*t))*P1dash(1,2);
quiver(x,y,xt,yt,'b');% Ploting Tangent
grid on
hold on
Tutorial 3
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 B0 [1, 1], B1 [2, 3],
B2 [4, 3], B3 [3, 1]. Find 20 equidistant points on the curve.

b) Use the same function to generate a Bezier curve defined by five polygon vertices B0 [1, 1], B1 [2, 4],
B2 [4, 5], B4 [5, 4], B5 [3, 1]. Find 30 equidistant points on the curve.

a) [1,1;2,3;4,3;3,1]
b) [1,1;2,4;4,5;5,4;3,1]

%% Define the control point and number of points on the curve


clc
clear all
close all
B = input('Enter the control points'); %Vertices of polygon
[r,s] = size(B);
n = r-1; % n+1 = Number of polygon vertices
np = 20; % Number of equidistant points on Bezir curve
t = linspace(0,1,np); % Value of parameter 't' for all the points on curve

%% Plot the control point and generate the polygon


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

%% Generate the points on the curve using the basis function defined in the Bezier curve
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
%% Plotting of Bezier curve
for l = 1:np-1
figure(1);plot([Q(l,1),Q(l+1,1)],[Q(l,2),Q(l+1,2)],'--b','LineWidth',2)
hold on
end
Assignment 3
Generate a Matlab program to blend two Bezier curves of given degrees with both C0 and C1 continuities at
the blend point. Verify your program for the sample data given below.
First Bezier curve segment:

1 4  10 8


P0   , P1   , P2   , P3   
1 4  5 1
Second Bezier curve segment:

2  5 12
P4   , P5   , P6   
clc
clear all
2  5 1
close all
B1=[1 1;4 4; 10 5 ;8 1]; % points on curve
[r,s]=size(B1);
n=r-1;
np=20; %no of equi distance point on curve
t=linspace(0,1,np);
%% boundary plot
for k = 1:n
figure(1); plot([B1(k,1),B1(k+1,1)],[B1(k,2),B1(k+1,2)],'r','LineWidth',2)
hold on % To hold figure handle on same figure
end
%% Generation of points
m=1;
for j=1:np
p=[0,0];
for k=0:n
J(k+1)=(factorial(n)/(factorial(k)*factorial(n-k)))* (t(j)^k*(1-t(j))^(n-k));
p=p+B1(k+1,:)*J(k+1);
end
Q(j,:)=p;
end
%% plotting of bezier curve
for l=1:np-1
plot([Q(l,1),Q(l+1,1)],[Q(l,2),Q(l+1,2)],'--b','Linewidth',2)
hold on
end
%% C0 continuity
% For continuity p4 must concide with p3
%translate the entire control polygon of segment -2
B2=[2 2; 5 5;12 1];
d=B1(4,:)-B2(1,:);
B2new=[B2 ones(3,2)]';
pnew=assign2d(1,B2new);
%% C1 continuity
%p5**=(3/2)*[P3-P2]+P3
p2=[B1(3,:) ones(1,2)]';
pnew(:,2)=(3/2)*(pnew(:,1)-(p2))+ pnew(:,1);
pnew=pnew(1:2,:)';
%% boundary plot
[y, z]=size(pnew);
n=y-1;
for k = 1:n
figure(1); plot([pnew(k,1),pnew(k+1,1)],[pnew(k,2),pnew(k+1,2)],'g','LineWidth',2)
hold on % To hold figure handle on same figure
end
%% Generation of points
m=1;
for j=1:np
p=[0,0];
for k=0:n
J(k+1)=(factorial(n)/(factorial(k)*factorial(n-k)))* (t(j)^k*(1-t(j))^(n-k));
p=p+pnew(k+1,:)*J(k+1);
end
Q(j,:)=p;
end
%% plotting of 2nd bezier curve
for l=1:np-1
plot([Q(l,1),Q(l+1,1)],[Q(l,2),Q(l+1,2)],'--k','Linewidth',2)
hold on
end
%% Function
function [new] = assign2d(typ,input)
if typ ==1 % linear
xd =6;
yd = -1;
zd = 0;
mat = [1 0 0 xd; 0 1 0 yd; 0 0 1 zd; 0 0 0 1];
end
new = mat*input;
end
Tutorial 4
Develop the parametric equation for a quadratic B-spline curve for four control points. Using Matlab, For
the quadratic B-spline curve developed for 4 control points. The control points are given by:

1 4  10 8


P0   , P1   , P2   , P3   
1 4  5 1
clc
clear all
close all

%% B-spline curve
p0 = [1;1];
p1 = [4;4];
p2 = [10;5];
p3 = [8;1];

%% Plotting first segment


u = 0:0.01:1;
P = p0.*((1-u).^2) + p1.*(2*u-(3*u.^2/2)) + p2.*(u.^2/2);
x = P(1,:);
y = P(2,:);
plot(x,y,"k")
axis("equal")
hold on

%% Plotting second segment


u = 1:0.01:2;
P = p1.*((2-u).^2/2) + p2.*(u-(u.^2/2)+(2-u).*(u-1)) + p3.*((u-1).^2);
x = P(1,:);
y = P(2,:);
plot(x,y,"b")

%% Plotting control polygon


plot(1,1,"*g")
plot(4,4,"*g")
plot(10,5,"*g")
plot(8,1,"*g")
plot([4,10],[4,5],"r")
plot([1,4],[1,4],"r")
plot([10,8],[5,1],"r")
hold on
Assignment 4
For the quadratic B-spline curve developed for 5 control points, derive the basis functions, develop the
parametric equation and prove that the curve is tangent to the mid-point of each of the internal control
polygonal segments using both Matlab program using the sample control point data of:

2  5 12 10 14


P0   , P1   , P2   , P3   , P4   
2  5 6 2 3
clc
clear all
close all
%% quardatic B-spline (k=3) n+1=5 (segment =n-k+2=3)
p0=[2;2]; p1=[5;5]; p2=[12;6]; p3=[10;2]; p4=[14;3];
u=0:0.01:1;
P=p0.*((1-u).^2)+ p1.*((u.*(1-u)) + (u.*(2-u))./2)+ p2.*(u.^2./2);
x=P(1,:);
y=P(2,:);
plot(x,y,"b")
axis([0 15 0 8])
hold on
u=1:0.01:2;
P=p1.*(2-u).^2./2 + p2*((u.*(2-u))./2 + (u-1).*(3-u)./2)+ p3*(u-1).^2./2;
x=P(1,:); y=P(2,:);
plot(x,y,"b")
hold on
u=2:0.01:3;
P=p2.*(3-u).^2./2 + p3.*(((u-1).*(3-u))./2 + (u-2).*(3-u))+p4.*((u-2).^2);
x=P(1,:);
y=P(2,:);
plot(x,y,"g")
hold on
plot(2,2,"*g")
plot(5,5,"*g")
plot(12,6,"*g")
plot(10,2,"*g")
plot(14,3,"*g")
plot([2,5],[2,5],"r")
plot([5,12],[5,6],"r")
plot([12,10],[6,2],"r")
plot([10,14],[2,3],"r")
hold on

%% tangent using parametric equation


% P(u)=p0*((1-u)^2) + p1*(2u-(1.5u^2))+ p2*(u^2/2); P'(u)=(2u-2)p0+(-3u+2)p1+up2(0<=u<=1)
% P(u)=p1*((2-u)^2/2) + p2*((-u^2+3u-1.5))+ p3*((u-1)^2/2); P'(u)=(u-2)p1+(-2u+3)p2+(u-)p3(1<=u<=2)
% P(u)=p2*((3-u)^2/2) + p3*((-1.5u^2+7u-7.5))+ p4*((u-2)^2); P'(u)=(u-3)p2+(-3u+7)p3+
2(u-1)p4 (2<=u<=3)

u=0.5;
for i=1:3
if (u>=0 && u<=1)
P=p0.*((1-u).^2)+ p1.*((u.*(1-u)) + (u.*(2-u))./2)+ p2.*(u.^2./2);
P1=(2.*(u-1)).*p0+((1-2.*u)+(2-2.*u))/2.*p1 + u.*p2;
quiver(P(1,1),P(2,1),P1(1,1),P1(2,1),'k')
elseif (u>=1 && u<=2)
P=p1.*(2-u).^2./2 + p2*((u.*(2-u))./2 + (u-1).*(3-u)./2)+ p3*(u-1).^2./2;
P2=(u-2).*p1+0.5*((2-2.*u)+(1-u+3-u)).*p2+(u-1).*p3;
quiver(P(1,1),P(2,1),P2(1,1),P2(2,1),'b')
elseif (u>=2 && u<=3)
P=p2.*(3-u).^2./2 + p3.*(((u-1).*(3-u))./2 + (u-2).*(3-u))+p4.*((u-2).^2);
P3=(u-3).*p2+(0.5*(4-2*u)+(2-u+3-u)).*p3+(2*(u-2)).*p4;
quiver(P(1,1),P(2,1),P3(1,1),P3(2,1),'g')
end
u=1+u;
end
%% segment mid point
mid1=[(p0+p1)/2,(p1+p2)/2,(p2+p3)/2,(p3+p4)/2]';
plot(mid1(:,1),mid1(:,2),'--m');
Tutorial 5
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 45o


d) Apply all the above transformations in the same order (a) to (c).

function [newpoint] = trans2d(type,oldpoint)

if type == 1
p = 2;
q = 2;
TRANS = [1 0 p;0 1 q;0 0 1]
MAT = TRANS;
elseif type == 2
theta = 45;
ROT = [cos(theta) -sin(theta) 0;sin(theta) cos(theta) 0;0 0 1]
MAT = ROT;
elseif type == 3
shy = 1.5;
SHEARY = [1 0 0;shy 1 0;0 0 1]
MAT = SHEARY;
end
newpoint = MAT*oldpoint;
end

clc
clear all
close all
% Initial position of rectangle
Ax = 3;
Ay = 1;
Bx = 3;
By = 4;
Cx = 8;
Cy = 4;
Dx = 8;
Dy = 1;
plot([Ax,Bx],[Ay,By],'b') % Line between point A & B
axis([-10 20 -10 20])
hold on
plot([Bx,Cx],[By,Cy],'b') % Line between point B & C
hold on
plot([Cx,Dx],[Cy,Dy],'b') % Line between point C & D
hold on
plot([Dx,Ax],[Dy,Ay],'b') % Line between point D & A
hold on

%% Transformation of all points of rectangle


% Tutorial (5a)
%% Master matrix 1
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);

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

% Plotting new position of rectangle


plot([Axnew,Bxnew],[Aynew,Bynew],'r')
%axis([-10 20 -10 20]
hold on
plot([Bxnew,Cxnew],[Bynew,Cynew],'r')
plot([Cxnew,Dxnew],[Cynew,Dynew],'r')
plot([Dxnew,Axnew],[Dynew,Aynew],'r')

%% Master matrix 2
Aold = [Axnew;Aynew;1];
Bold = [Bxnew;Bynew;1];
Cold = [Cxnew;Cynew;1];
Dold = [Dxnew;Dynew;1];
OldPos = [Aold,Bold,Cold,Dold];

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],'g')
hold on
plot([Bxnew,Cxnew],[Bynew,Cynew],'g')
plot([Cxnew,Dxnew],[Cynew,Dynew],'g')
plot([Dxnew,Axnew],[Dynew,Aynew],'g')

%% Master matrix 3
Aold = [Axnew;Aynew;1];
Bold = [Bxnew;Bynew;1];
Cold = [Cxnew;Cynew;1];
Dold = [Dxnew;Dynew;1];
OldPos = [Aold,Bold,Cold,Dold];

NewPos = trans2d(2,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],'k')
hold on
plot([Bxnew,Cxnew],[Bynew,Cynew],'k')
plot([Cxnew,Dxnew],[Cynew,Dynew],'k')
plot([Dxnew,Axnew],[Dynew,Aynew],'k')
Assignment 5
Using MATLAB, generate an ellipse with a = 4 and b = 1 inclined at 30 deg to x-axis 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:

Procedure:
To generate the ellipse, perform the following transformations:
a) First rotate the ellipse with centre at (0, 0) about z-axis by 30 deg
b) After rotation, translate the centre to (2, 2).
clear all
close
clc
A = input('Enter the angle of inclination: ');
xc = input('Enter the X-coordinate of centre: ');
yc = input('Enter the Y-coordinate of centre: ');
a = input('Enter the major axis length: ');
b = input('Enter the minor axis lenght: ');
Sa = sind(A);
Ca = cosd(A);
X = xc+ a*cosd(A);
Y = yc+ b*sind(A);
plot(X,Y,'b*','LineWidth',0.5)
n= input("Number of points:");
u = 0:1/n:1;
for i = 1:n+1
Dt = (2*pi)/(u(i)-1);
x1(i) = xc + ((X-xc)*cos(2*pi*(Dt))) - ((Y-yc)*sin(2*pi*(Dt)));
y1(i) = yc + ((Y-yc)*cos(2*pi*(Dt))) - ((X-xc)*sin(2*pi*(Dt)));
%plotting of ellipse at centre(0,0)
plot(x1,y1,'*b','LineWidth',0.1)
xlabel('x - axis')
ylabel('y - axis')
hold on
plot(xc,yc,'*')
hold on
quiver(xc,yc,5*Ca,5*Sa,'k'); %plot major axis
hold on
quiver(xc,yc,5*cosd(90+A),5*sind(90+A),'k'); %plot minor axis
hold on
end
%% Translating the center of the ellipse
xc1 = input("New X translation coordinate:");
yc1 = input("New Y translation coordinate:");
Sa1 = sind(A);
Ca1 = cosd(A);
Xt = xc1+ a*cosd(A);
Yt = yc1+ b*sind(A);
plot(Xt,Yt,'*b','LineWidth',0.5)
for i = 1:n+1
Dt = (2*pi)/(u(i)-1);
xt(i) = xc1 + ((Xt-xc1)*cos(2*pi*(Dt))) - ((Yt-yc1)*sin(2*pi*(Dt)));
yt(i) = yc1 + ((Yt-yc1)*cos(2*pi*(Dt))) - ((Xt-xc1)*sin(2*pi*(Dt)));
%plotting of ellipse at centre(2,2)
pp = plot(xt,yt,'*b','LineWidth',1);
pp.Color = "red";
xlabel('x - axis')
ylabel('y - axis')
hold on
plot(xc1,yc1,'*')
hold on
quiver(xc1,yc1,5*Ca1,5*Sa1,'k'); %plot major axis
hold on
quiver(xc1,yc1,5*cosd(90+A),5*sind(90+A),'k'); %plot minor axis
hold on
end
Tutorial 6
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 45o 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.

clc
clear all
close all
%% Coordinates of cube
P = [0 0 0; 0 1 0; 1 1 0; 1 0 0; 0 0 0; 0 0 1; 0 1 1; 1 1 1; 1 0 1; 1 0 0; 1 1 0; 1 1 1; 0 1 1; 0 1 0; 0 0 0; 0 0 1; 1
0 1];
x = P(:,1);
y = P(:,2);
z = P(:,3);
plot3(x,y,z,'b');
xlabel('x - axis')
ylabel('y - axis')
zlabel('z - axis')
hold on
%% Translation
xt = x+1;
yt = y+1;
zt = z+1;
pp = plot3(xt,yt,zt);
pp.Color = 'green';
hold on
%% Rotation
u = input("Angle of rotation:");
xr = x*cosd(u) - y*sind(u);
yr = x*sind(u) + y*cosd(u);
zr = z;
plot3(xr,yr,zr);
hold on
%% Projection
xy = [0 0 0; 0 1 0; 1 1 0; 1 0 0; 0 0 0]; % projection along x-y plane
yz = [0 0 0; 0 0 1; 0 1 1; 0 1 0; 0 0 0]; % projection along y-z plane
xz = [0 0 0; 0 0 1; 1 0 1; 1 0 0; 0 0 0]; % projection along x-z plane
j = input('Projection along the plane:');
if j == xy
x1 = j(:,1);
y1 = j(:,2);
z1 = j(:,3);
plot3(x1,y1,z1,"r");
hold on;
elseif j == yz
x1 = j(:,1);
y1 = j(:,2);
z1 = j(:,3);
plot3(x1,y1,z1,"r");
hold on;
elseif j == xz
x1 = j(:,1);
y1 = j(:,2);
z1 = j(:,3);
plot3(x1,y1,z1,"r");
hold on;
end
Assignment 6
The vertices of a block with one corner removed are given as A( 2, 1, 2), 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, 2, 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)

clc
clear all
close all
%% Coordinates of cube
P = [2 1 2; 3 1 2; 3 1 1; 2 1 1;2 2 1; 3 2 1; 3 1 1; 3 1 2; 3 1.5 2; 2.5 2 2; 3 2 1.5; 3 1.5 2; 2.5 2 2; 2 2 2; 2 2 1;
3 2 1; 3 2 1.5; 2.5 2 2; 2 2 2; 2 1 2; 2 1 1] ;
x = P(:,1);
y = P(:,2);
z = P(:,3);
plot3(x,y,z,'b');
xlabel('x - axis')
ylabel('y - axis')
zlabel('z - axis')
hold on
%% ROTATION
u = input("Angle of rotation:");
xr = x*cosd(u) - y*sind(u);
yr = x*sind(u) + y*cosd(u);
zr = z;
plot3(xr,yr,zr);
hold on
%% PROJECTION
xy = [3 1.5 2; 3 1 2; 2 1 2; 2 2 2; 2.5 2 2; 2.5 2 2; 3 1.5 2; 2.5 2 2]; %% PROJECTION ALONG XY
PLANE
x1 = xy(:,1);
y1 = xy(:,2);
z1 = xy(:,3);
plot3(x1,y1,z1,"r");
hold on;
yz = [2.5 2 2; 3 2 1.5; 3 2 1; 3 1 1; 3 1 2; 3 1.5 2; 3 2 1.5; 2.5 2 2; 3 1.5 2]; %% PROJECTION ALONG YZ
PLANE
x1 = yz(:,1);
y1 = yz(:,2);
z1 = yz(:,3);
plot3(x1,y1,z1,"g");
hold on;
Tutorial 7
Write a MATLAB code to develop a surface of revolution by revolving 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
about z-axis P1’ = [1 1] and P4’ =[1 1] about z-axis. Take 20 points on each segment.

[ ][ ]
2 −2 11 1 0 00
H= −3 3 − 2− 1 B= 1 4 1 0
0 0 10 0 1 41
10 0 0 0001

%% Step 1 :- Define the input parameter specified in problem and plot the initial control point
% Script file for normalized cubic spline curve
clc
clear all
close all
P = [0 0;1 1;2 -1;3 0];
P1dash = [1 1];
P4dash = [1 1];
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','LineWidth',2)
hold on
%% Step 2 :- Generate the intermediate tangent vector using the given condition
Pdash = inv(C)*[P1dash;3*((P3-P2)+(P2-P1));3*((P4-P3)+(P3-P2));P4dash]; % Derivatives at all points
%% Step 3 :- Generation of interpolated cubic spline
np = 20;
t = linspace(0,1,np);
k = 1;
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;
pxa(k) = PS(j,1);
pya(k) = PS(j,2);
k = k+1;
figure(1);plot(PS(j,1),PS(j,2),'b.')
hold on
end
end
%% Step 4 :- Revolve the curve generated in Step 3
n = 3*np;
phip = linspace(0,2*pi,n);
for i = 1:n
phi = phip(i);
qxa(i,:) = pxa;
qya(i,:) = pya*cos(phi);
qza(i,:) = pya*sin(phi);
figure(2);plot3(qxa(i,:),qya(i,:),qza(i,:))
xlabel('X-axis')
ylabel('Y-axis')
zlabel('Z-axis')
hold on
drawnow
end
% grid on
hold on
xlabel('Xaxis')
ylabel('Yaxis')
zlabel('Zaxis')
figure(3)
surf(qxa,qya,qza)
Assignment 7
Write a MATLAB code to develop a surface of revolution by revolving a Bezier curve defined by four
polygon vertices B0[1,1], B1[2,3], B2[4,3], B3[3,1]. Find 20 equidistant points on the curve.

clc
clear all
close all
B = [1,1;2,3;4,3;3,1]; % Vertices of Polygon
[r,c] = size(B); % Control points
n = r-1; % n + 1 = Number of degree
np = input('Number of equidistant points on Bezier Curve:');
u = linspace(0,1,np); %Value of parameter 't' for all the points on Curve
P1 =[];
%% Plotting the polygon
plot(B(:,1),B(:,2),'r','LineWidth',2);
hold on
%% Generating points on the curve
for j = 1:np
P =[0,0];
for i = 0:n
J(i+1) = factorial(n)/(factorial(i)*(factorial(n-i)))*u(j)^i*(1-u(j))^(n-i);
P = P + B(i+1,:)*J(i+1);
end
P1 = [P1;P];
end
%% Plotting the Bezier curve
plot(P1(:,1), P1(:,2),'--b','LineWidth',2);
%Surface of Revolution about x-axis
pxa = P1(:,1);
pya = P1(:,2);
n = 150; % Total number of points on curve
phip = linspace(0,2*pi,n);
for i = 1:n
phi = phip(i);
qxa(i,:) = pxa;
qya(i,:) = pya*cos(phi);
qza(i,:) = pya*sin(phi);
figure(2); plot3(qxa(i,:), qya(i,:), qza(i,:))
hold on
drawnow
end
%grid on
hold on
xlabel('Xaxis')
ylabel('Yaxis')
zlabel('Zaxis')
Tutorial 8
Develop the ruled surface with edge curves defined by:
Z=0, y=0.25 cos(2πx) 0 ≤ x ≤ 1

Z=1, y=0.5 cos(2πx) 0 ≤ x ≤ 1


%% Step 1 :- Generation of coordinates of points on two edge curves
% Clearing the workspace
clc
clear all
close all
npa = 50; % Number of points on first edge curve
npb = 50; % Number of points on second edge curve
%% Coordinates of points on two edge curves
x1 = linspace(0,1,npa);
x2 = linspace(0,1,npb);
y1 = 0.25*cos(2*pi*x1);
y2 = 0.5*sin(2*pi*x2);
z1 = zeros(1,npa);
z2 = ones(1,npb);
Pu0 = y1;
Pu1 = y2;
%% Step 2 :- Development of Ruled surface
npw = 50;
w = linspace(0,1,npw);
% Ruled surface
Qx = zeros(npw,npa);
Qy = zeros(npw,npa);
Qz = zeros(npw,npa);
for i = 1:npw
Qx(i,:) = (1-w(i))*x1 + w(i)*x2;
Qy(i,:) = (1-w(i))*y1 + w(i)*y2;
Qz(i,:) = (1-w(i))*z1 + w(i)*z2;
figure(1)
plot3(Qx(i,:),Qy(i,:),Qz(i,:));
hold all
end
figure(2)
surf(Qx,Qy,Qz)
Assignment 8

clc;
clear all;
close all;
A1 = [0,0,0];
A2 = [0.2357,0.2357,0.3333];
A3 = [1.1785,0.2357,0.3333];
A4 = [1.4142,0,0];
B1 = [1.4142,0,0];
B2 = [1.1785,0.2357,0.3333];
B3 = [1.1785,1.1785,0.3333];
B4 = [1.4142,1.4142,0];
C1 = [1.4142,1.4142,0];
C2 = [1.1785,1.1785,0.3333];
C3 = [0.2357,1.1785,0.3333];
C4 = [0,1.4142,0];
D1 = [0,1.4142,0];
D2 = [0.2357,1.1785,0.3333];
D3 = [0.2357,0.2357,0.3333];
D4 = [0,0,0];
P00 = [A1;A2;A3;A4];
P01 = [B1;B2;B3;B4];
P10 = [C4;C3;C2;C1];
P11 = [D4;D3;D2;D1];
[r,~] = size(P00);
n = r-1;
np = 80;
t = linspace(0,1,np);
hold all
figure(1)
for i = 1:n
plot3([P00(i,1),P00(i+1,1)], [P00(i,2),P00(i+1,2)], [P00(i,3),P00(i+1,3)], 'r');
plot3([P01(i,1),P01(i+1,1)], [P01(i,2),P01(i+1,2)], [P01(i,3),P01(i+1,3)], 'r');
plot3([P10(i,1),P10(i+1,1)], [P10(i,2),P10(i+1,2)], [P10(i,3),P10(i+1,3)], 'r');
plot3([P11(i,1),P11(i+1,1)], [P11(i,2),P11(i+1,2)], [P11(i,3),P11(i+1,3)], 'r');
end
for j = 1:np
p1 = [0,0,0];
p2 = [0,0,0];
p3 = [0,0,0];
p4 = [0,0,0];
for i = 0:n
bc(i+1) = (factorial(n)/(factorial(i)*factorial(n-i)))*((t(j))^i)*((1-t(j))^(n-i));
p1 = p1 + P00(i+1,:).*bc(i+1);
p2 = p2 + P01(i+1,:).*bc(i+1);
p3 = p3 + P10(i+1,:).*bc(i+1);
p4 = p4 + P11(i+1,:).*bc(i+1);
end
P1(j,:) = p1;
P2(j,:) = p2;
P3(j,:) = p3;
P4(j,:) = p4;
end
for i = 1:np-1
plot3([P1(i,1),P1(i+1,1)],[P1(i,2),P1(i+1,2)], [P1(i,3),P1(i+1,3)], '.b', 'LineWidth',3);
plot3([P2(i,1),P2(i+1,1)],[P2(i,2),P2(i+1,2)], [P2(i,3),P2(i+1,3)], 'k.', 'LineWidth',3);
plot3([P3(i,1),P3(i+1,1)],[P3(i,2),P3(i+1,2)], [P3(i,3),P3(i+1,3)], '.m', 'LineWidth',3);
plot3([P4(i,1),P4(i+1,1)],[P4(i,2),P4(i+1,2)], [P4(i,3),P4(i+1,3)], 'r.', 'LineWidth',3);
end
%%Plot surface
u = linspace(0,1,np);
w = linspace(0,1,np);
for i = 1:np
for j = 1:np
Px(i,j) = P1(i,1)*(1-w(j)) + P2(j,1)*(u(i))+ P3(i,1)*w(j)+P4(j,1)*(1-u(i))- P1(1,1)*(1-u(i))*(1-w(j))-
P1(end,1)*(u(i))*(1-w(j))-P3(1,1)*(1-u(i))*(w(j))- P3(end,1)*u(i)*w(j);
Py(i,j) = P1(i,2)*(1-w(j)) + P2(j,2)*(u(i))+ P3(i,2)*w(j)+P4(j,2)*(1-u(i))- P1(1,2)*(1-u(i))*(1-w(j))-
P1(end,2)*(u(i))*(1-w(j))-P3(1,2)*(1-u(i))*(w(j))- P3(end,2)*u(i)*w(j);
Pz(i,j) = P1(i,3)*(1-w(j)) + P2(j,3)*(u(i))+ P3(i,3)*w(j)+P4(j,3)*(1-u(i))- P1(1,3)*(1-u(i))*(1-w(j))-
P1(end,3)*(u(i))*(1-w(j))-P3(1,3)*(1-u(i))*(w(j))- P3(end,3)*u(i)*w(j);
end
end
surf(Px,Py,Pz)
view(45,45)

You might also like