CAAD Report
CAAD Report
(Assignment)
By
Name: Manoj Kumar K
(ID NO: 2022HT30024)
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);
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.
clc
clear all
close all
%% Plot coordinates
plot(P(:,1),P(:,2),'or','LineWidth',2)
hold on
clc
clear all
close all
%% Plot coordinates
plot(P(:,1),P(:,2),'or','LineWidth',2)
hold on
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]
%% 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:
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:
%% B-spline curve
p0 = [1;1];
p1 = [4;4];
p2 = [10;5];
p3 = [8;1];
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.
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
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);
%% 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).
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
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)