Sunteți pe pagina 1din 16

MASTER OF SCIENCE (ENGINEERING MATHEMATICS)

EQT 535
COMPUTER AIDED GEOMETRIC DESIGN FOR SURFACE MODELLING

(ASSIGNMENT 1)

NURUL EHSAN BIN RAMLI


860216465051
PROBLEM STATEMENT 1

Assume p0, p1, p2, and p3 are the points need to be interpolated using either Aitken or
Lagrange with value t0, t1, t2, and t3 are 0, 1/3, 2/3 and 1 respectively. Explain how we can
generate the same parametric curve using cubic Bezier with control points b0, b1, b2, and b3.
Then, generate both curves in MATLAB with example of your own.

Solution:

By using Lagrange interpolation, the curve is defined as;

𝑥(𝑡)
ℙ(𝑡) = ℙ0 ℒ0𝑛 + ℙ0 ℒ1𝑛 + ⋯ + ℙ0 ℒ𝑛𝑛 𝑤ℎ𝑒𝑟𝑒 ℙ(𝑡) = {𝑦(𝑡) }

Given that; 4 points (p0, p1, p2, p3) ⟶ n=3

So; ℙ(𝑡) = ℙ0 ℒ03 + ℙ1 ℒ13 + ℙ2 ℒ23 + ℙ3 ℒ33

1, 𝑖𝑓 𝑗 = 1
Where; ℒ𝑖𝑛 (𝑡𝑗 ) = {
0 , 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Thus;

(𝑡 − 𝑡0 )(𝑡 − 𝑡1 ) … (𝑡 − 𝑡𝑖−1 )(𝑡 − 𝑡𝑖+1 ) … (𝑡 − 𝑡𝑛 )


ℒ𝑖𝑛 (𝑡) =
(𝑡𝑖 − 𝑡0 )(𝑡𝑖 − 𝑡1 ) … (𝑡𝑖 − 𝑡𝑖−1 )(𝑡𝑖 − 𝑡𝑖−1 ) … (𝑡𝑖 − 𝑡𝑛 )

𝑛
(𝑡 − 𝑡𝑗 )
ℒ𝑖𝑛 (𝑡) = ∏ , 𝑗≠𝑖
(𝑡𝑖 − 𝑡𝑗 )
𝑗=0

So, the four basis function;

(𝑡 − 𝑡1 )(𝑡 − 𝑡2 )(𝑡 − 𝑡3 )
ℒ03 (𝑡) =
(𝑡0 − 𝑡1 )(𝑡0 − 𝑡2 )(𝑡0 − 𝑡3 )

1 2
(𝑡 − 3) (𝑡 − 3) (𝑡 − 1)
= = −𝟒. 𝟓𝒕𝟑 + 𝟗𝒕𝟐 − 𝟓. 𝟓𝒕 + 𝟏
1 2
(0 − 3) (0 − 3) (0 − 1)
(𝑡 − 𝑡0 )(𝑡 − 𝑡2 )(𝑡 − 𝑡3 )
ℒ13 (𝑡) =
(𝑡1 − 𝑡0 )(𝑡1 − 𝑡2 )(𝑡0 − 𝑡3 )

2
(𝑡 − 0) (𝑡 − ) (𝑡 − 1)
= 3 = 𝟏𝟑. 𝟓𝒕𝟑 − 𝟐𝟐. 𝟓𝒕𝟐 + 𝟗𝒕
1 1 2 1
(3 − 0) (3 − 3) (3 − 1)

(𝑡 − 𝑡0 )(𝑡 − 𝑡1 )(𝑡 − 𝑡3 )
ℒ23 (𝑡) =
(𝑡2 − 𝑡0 )(𝑡2 − 𝑡1 )(𝑡2 − 𝑡3 )

1
(𝑡 − 0) (𝑡 − ) (𝑡 − 1)
= 3 = −𝟏𝟑. 𝟓𝒕𝟑 + 𝟏𝟖𝒕𝟐 − 𝟒. 𝟓𝒕
2 2 1 2
(3 − 0) (3 − 3) (3 − 1)

(𝑡 − 𝑡0 )(𝑡 − 𝑡1 )(𝑡 − 𝑡2 )
ℒ33 (𝑡) =
(𝑡3 − 𝑡0 )(𝑡3 − 𝑡1 )(𝑡3 − 𝑡2 )

1 2
(𝑡 − 0) (𝑡 − ) (𝑡 − )
= 3 3 = 𝟒. 𝟓𝒕𝟑 − 𝟒𝟓𝒕𝟐 + 𝒕
1 2
(1 − 0) (1 − ) (1 − )
3 3

The Lagrange Polynomial is now easy to calculate:

ℙ(𝑡) = (−4.5𝑡 3 + 9𝑡 2 − 5.5𝑡 + 1 )ℙ0 + (13.5𝑡 3 − 22.5𝑡 2 + 9𝑡)ℙ1


+ (−13.5𝑡 3 + 18𝑡 2 − 4.5𝑡)ℙ2 + (4.5𝑡 3 − 45𝑡 2 + 𝑡)ℙ3

The interpolating curve is thus;

𝑥(𝑡)
𝕡(𝑡) = {𝑦(𝑡) } = {𝑥𝑦0} ℒ03 (𝑡) {𝑥𝑦1} ℒ13 (𝑡) {𝑥𝑦2} ℒ23 (𝑡) {𝑥𝑦3} ℒ33 (𝑡)
0 1 2 3
Next, let’s look at how we could solve directly for the Bezier control points of a Bezier
Curve which interpolates those same points at those same parameters values. In this case,
we want to solve for the coefficients of a Bernstein Polynomial.
𝑛

𝕓(𝑡) = ∑ 𝕓𝑖 ℬ𝑛,𝑖 (𝑡) , 0≤𝑡≥1


1=0

𝑥(𝑡) 𝑛!
𝑤ℎ𝑒𝑟𝑒 𝕓(𝑡) = { } 𝑎𝑛𝑑 ℬ𝑛,𝑖 (𝑡) = 𝑡 𝑖 (1 − 𝑡)𝑛−1
𝑦(𝑡) 𝑖! (𝑛 − 𝑖)!

So;
𝕓(𝑡) = 𝕓0 ℬ0 + 𝕓1 ℬ1 + 𝕓2 ℬ2 + 𝕓3 ℬ3
𝕓(𝑡) = (1 − 𝑡)3 𝕓0 + 3𝑡(1 − 𝑡)2 𝕓1 + 3𝑡 2 (1 − 𝑡)𝕓2 + 𝑡 3 𝕓3

Since we want to generate the same parametric curve, we can apply the situation and
condition state below;
 The starting point and ending point for Lagrange Polynomial and Bezier Curve is
actually the same point. So we can say that,
ℙ0 = 𝕓0 𝑎𝑛𝑑 ℙ3 = 𝕓3

Then, we need to find another two points of Bezier Curve which that point is actually the
control point of Bezier curve.
So, we apply all the conditions that we know and all known coefficient to Bernstein
Polynomial, we can find the control point of Bezier Curve.
At t = 1/3 ;
1 1 1 1 1 1
𝑝𝑥,1 = (1 − )3 𝑝𝑥,0 + 3( )(1 − )2 𝔟𝑥,1 + 3( )2 (1 − )𝔟𝑥,2 + ( )3 𝑝𝑥,3
3 3 3 3 3 3
8 4 2 1
𝑝𝑥,1 = ( ) 𝑝𝑥,0 + ( )𝔟𝑥,1 + ( )𝔟𝑥,2 + ( )𝑝𝑥,3
27 9 9 27
27𝑝𝑥,1 = 8 𝑝𝑥,0 + 12𝔟𝑥,1 + 6𝔟𝑥,2 + 𝑝𝑥,3

6(2𝔟𝑥,1 + 𝔟𝑥,2 ) = 27𝑝𝑥,1 − 8𝑝𝑥,0 − 𝑝𝑥,3


27 8 1
2𝔟𝑥,1 + 𝔟𝑥,2 = 𝑝𝑥,1 − 𝑝𝑥,0 − 𝑝𝑥,3
6 6 6
𝟒 𝟏
𝖇𝒙,𝟐 = 𝟒. 𝟓𝒑𝒙,𝟏 − 𝒑𝒙,𝟎 − 𝒑𝒙,𝟑 − 𝟐𝖇𝒙,𝟏 ⟶ (𝒆𝒒𝒏. 𝟏)
𝟑 𝟔
At t = 2/3 ;
2 2 2 2 2 2
𝑝𝑥,2 = (1 − )3 𝑝𝑥,0 + 3( )(1 − )2 𝔟𝑥,1 + 3( )2 (1 − )𝔟𝑥,2 + ( )3 𝑝𝑥,3
3 3 3 3 3 3
1 2 4 8
𝑝𝑥,2 = ( ) 𝑝𝑥,0 + ( )𝔟𝑥,1 + ( )𝔟𝑥,2 + ( )𝑝𝑥,3
27 9 9 27
27𝑝𝑥,2 = 𝑝𝑥,0 + 6𝔟𝑥,1 + 12𝔟𝑥,2 + 8𝑝𝑥,3

6(𝔟𝑥,1 + 2𝔟𝑥,2 ) = 27𝑝𝑥,2 − 𝑝𝑥,0 − 8𝑝𝑥,3


𝟏 𝟒
𝖇𝒙,𝟏 + 𝟐𝖇𝒙,𝟐 = 𝟒. 𝟓𝒑𝒙,𝟐 − 𝒑𝒙,𝟎 − 𝒑𝒙,𝟑 ⟶ (𝒆𝒒𝒏. 𝟐)
𝟔 𝟑
Substitute (eqn. 1) into (eqn. 2)
1 4 4 1
𝔟𝑥,1 = 4.5𝑝𝑥,2 − 𝑝𝑥,0 − 𝑝𝑥,3 − 2(4.5𝑝𝑥,1 − 𝑝𝑥,0 − 𝑝𝑥,3 − 2𝔟𝑥,1 )
6 3 3 6
1 4 8 1
𝔟𝑥,1 = 4.5𝑝𝑥,2 − 𝑝𝑥,0 − 𝑝𝑥,3 − 9𝑝𝑥,1 + 𝑝𝑥,0 + 𝑝𝑥,3 + 4𝔟𝑥,1 )
6 3 3 3
5
3𝔟𝑥,1 = − 𝑝𝑥,0 + 9𝑝𝑥,1 − 4.5𝑝𝑥,2 + 𝑝𝑥,3
2
𝟓 𝟑 𝟏
𝖇𝒙,𝟏 = − 𝒑𝒙,𝟎 + 𝟑𝒑𝒙,𝟏 − 𝒑𝒙,𝟐 + 𝒑𝒙,𝟑 ⟶ (𝒆𝒒𝒏. 𝟑)
𝟔 𝟐 𝟑
Substitute (eqn. 3) into (eqn. 1)
4 1 5 3 1
𝔟𝑥,2 = 4.5𝑝𝑥,1 − 𝑝𝑥,0 − 𝑝𝑥,3 − 2(− 𝑝𝑥,0 + 3𝑝𝑥,1 − 𝑝𝑥,2 + 𝑝𝑥,3 )
3 6 6 2 3
4 1 5 2
𝔟𝑥,2 = 4.5𝑝𝑥,1 − 𝑝𝑥,0 − 𝑝𝑥,3 + 𝑝𝑥,0 − 6𝑝𝑥,1 + 3𝑝𝑥,2 − 𝑝𝑥,3 )
3 6 3 3
𝟏 𝟑 𝟓
𝖇𝒙,𝟐 = 𝒑𝒙,𝟎 − 𝒑𝒙,𝟏 + 𝟑𝒑𝒙,𝟐 − 𝒑𝒙,𝟑
𝟑 𝟐 𝟔
Therefore;
5 3 1
𝔟𝑥,1 = − 𝑝𝑥,0 + 3𝑝𝑥,1 − 𝑝𝑥,2 + 𝑝𝑥,3
6 2 3
5 3 1
𝔟𝑦,1 = − 𝑝𝑦,0 + 3𝑝𝑦,1 − 𝑝𝑦,2 + 𝑝𝑦,3
6 2 3
1 3 5
𝔟𝑥,2 = 𝑝𝑥,0 − 𝑝𝑥,1 + 3𝑝𝑥,2 − 𝑝𝑥,3
3 2 6
1 3 5
𝔟𝑦,2 = 𝑝𝑦,0 − 𝑝𝑦,1 + 3𝑝𝑦,2 − 𝑝𝑦,3
3 2 6
Thus, we can see the relationship between Lagrange Polynomial with Bezier Curve as
shown below (based on the general equation for both Lagrange Polynomial and Bezier
Curve).

For Lagrange Polynomial;

𝒑𝒙,𝟎 𝟑 𝒑𝒙,𝟏 𝟑 𝒑𝒙,𝟐 𝟑 𝒑𝒙,𝟑 𝟑


𝕡(𝒕) = { } 𝓛𝟎 (𝒕) + { } 𝓛𝟏 (𝒕) + { } 𝓛𝟐 (𝒕) + { } 𝓛 (𝒕)
𝒑𝒚,𝟎 𝒑𝒚,𝟏 𝒑𝒚,𝟐 𝒑𝒚,𝟑 𝟑

For Bernstein Polynomial


5 3 1
𝒑𝒙,𝟎 (− 6 𝑝𝑥,0 + 3𝑝𝑥,1 − 2 𝑝𝑥,2 + 3 𝑝𝑥,3 )
𝕓(𝒕) = { } 𝓑 (𝒕) + { 5 } 𝓑𝟏 (𝒕)
𝒑𝒚,𝟎 𝟎 3 1
(− 6 𝑝𝑦,0 + 3𝑝𝑦,1 − 2 𝑝𝑦,2 + 3 𝑝𝑦,3 )
1 3 5
(3 𝑝𝑥,0 − 2 𝑝𝑥,1 + 3𝑝𝑥,2 − 6 𝑝𝑥,3 ) 𝒑𝒙,𝟑
+ { 1 3 5 } 𝓑𝟐 (𝒕) + { } 𝓑𝟑 (𝒕)
(3 𝑝𝑦,0 − 2 𝑝𝑦,1 + 3𝑝𝑦,2 − 6 𝑝𝑦,3 ) 𝒑 𝒚,𝟑
Matlab Code
%% MSc. Engineering Mathematics (EQT535 CAGD)
% Universiti Malaysia Perlis
% Assignment1:Problem 1
% Relation Between Lagrange Polynomial with Bezier Curve
% by Nurul Ehsan Ramli
% date 21 February 2014

clear
clc

%% Prompt for points to interpolate


point1 = input('Please enter first point: ');
point2 = input('Please enter second point: ');
point3 = input('Please enter third point: ');
point4 = input('Please enter forth point: ');

%% Calculate t
t0=0;
t1=1/3;
t2=2/3;
t3=1;
t=t0:0.01:t3;

%% Calculate L0, L1, L2 and L3


L0=((t-t1).*(t-t2).*(t-t3))/((t0-t1).*(t0-t2).*(t0-t3));
L1=((t-t0).*(t-t2).*(t-t3))/((t1-t0).*(t1-t2).*(t1-t3));
L2=((t-t0).*(t-t1).*(t-t3))/((t2-t0).*(t2-t1).*(t2-t3));
L3=((t-t0).*(t-t1).*(t-t2))/((t3-t0).*(t3-t1).*(t3-t2));

%% Calculate Lagrange Polynomial


LPx=point1(1)*L0 + point2(1)*L1 + point3(1)*L2 + point4(1)*L3;
LPy=point1(2)*L0 + point2(2)*L1 + point3(2)*L2 + point4(2)*L3;

%% Plot points & curve


plot(LPx,LPy,'k-')
hold on

%% Calculate Bezier control point


bpoint1 = point1;
bpoint4 = point4;

bpoint2(1)= -(5/6).*point1(1) + 3.*point2(1) - (3/2).*point3(1) ...


+ (1/3).*point4(1);
bpoint2(2)= -(5/6).*point1(2) + 3.*point2(2) - (3/2).*point3(2) ...
+ (1/3).*point4(2);

bpoint3(1)= (1/3).*point1(1) - (3/2).*point2(1) + 3.*point3(1) ...


- (5/6).*point4(1);
bpoint3(2)= (1/3).*point1(2) - (3/2).*point2(2) + 3.*point3(2) ...
- (5/6).*point4(2);
%% Calculate B03, B13, B23 and B33
B03 = (1-t).^3;
B13 = 3.*t.*((1-t).^2);
B23 = 3.*(t.^2).*(1-t);
B33 = t.^3;

%% Plot control points and control polygon


bx=[bpoint1(1),bpoint2(1),bpoint3(1), bpoint4(1)];
by=[bpoint1(2),bpoint2(2),bpoint3(2), bpoint4(2)];
plot(bx,by,'ro')
hold on
plot(bx,by, 'g-')

%% Plot Bezier Curve


BPx = bx(1)*B03 + bx(2)*B13 + bx(3)*B23 + bx(4)*B33;
BPy = by(1)*B03 + by(2)*B13 + by(3)*B23 + by(4)*B33;
plot(BPx,BPy)

Example:
Input: Please enter first point: [0 0]
Please enter second point: [2 2]
Please enter third point: [0 3]
Please enter forth point: [2 4]
Output:
PROBLEM STATEMENT 2

Design any meaningful sketches at least 10 pieces of curves. Use any curves that we have
learnt in class. Explanation of the curve used should be explain in the report.

Solution:

Matlab code
%% MSc. Engineering Mathematics (EQT535 CAGD)
% Universiti Malaysia Perlis
% Assignment1:Problem 2
% Picture: Batman
% by Nurul Ehsan Ramli
% date 21 February 2014
clear
clc

%% curve 1 : Aitken
%% Control points & plot
cpx=[0 1.5 2]; cpy=[-2 4 3];
hold on
%% Calculate t0, t1 and t2
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% 1st level to calculate P01 and P11
P01x=((t1-t)/(t1-t0))*cpx(1)+((t-t0)/(t1-t0))*cpx(2);
P01y=((t1-t)/(t1-t0))*cpy(1)+((t-t0)/(t1-t0))*cpy(2);
P11x=((t2-t)/(t2-t1))*cpx(2)+((t-t1)/(t2-t1))*cpx(3);
P11y=((t2-t)/(t2-t1))*cpy(2)+((t-t1)/(t2-t1))*cpy(3);
%% 2nd level to calculate P02
P02x=((t2-t)/(t2-t0)).*P01x+((t-t0)/(t2-t0)).*P11x;
P02y=((t2-t)/(t2-t0)).*P01y+((t-t0)/(t2-t0)).*P11y;
%% Plot interpolation
plot(P02x,P02y,'k')

%% curve 2 : Aitken
%% Control points & plot
cpx=[2 3.5 3.5]; cpy=[3 4 -0.75];
hold on
%% Calculate t0, t1 and t2
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% 1st level to calculate P01 and P11
P01x=((t1-t)/(t1-t0))*cpx(1)+((t-t0)/(t1-t0))*cpx(2);
P01y=((t1-t)/(t1-t0))*cpy(1)+((t-t0)/(t1-t0))*cpy(2);
P11x=((t2-t)/(t2-t1))*cpx(2)+((t-t1)/(t2-t1))*cpx(3);
P11y=((t2-t)/(t2-t1))*cpy(2)+((t-t1)/(t2-t1))*cpy(3);
%% 2nd level to calculate P02
P02x=((t2-t)/(t2-t0)).*P01x+((t-t0)/(t2-t0)).*P11x;
P02y=((t2-t)/(t2-t0)).*P01y+((t-t0)/(t2-t0)).*P11y;
%% Plot interpolation
plot(P02x,P02y,'k')

%% curve 3 : Aitken
%% Control points & plot
cpx=[3.5 8 3.5]; cpy=[-0.75 8 16];
hold on
%% Calculate t0, t1 and t2
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% 1st level to calculate P01 and P11
P01x=((t1-t)/(t1-t0))*cpx(1)+((t-t0)/(t1-t0))*cpx(2);
P01y=((t1-t)/(t1-t0))*cpy(1)+((t-t0)/(t1-t0))*cpy(2);
P11x=((t2-t)/(t2-t1))*cpx(2)+((t-t1)/(t2-t1))*cpx(3);
P11y=((t2-t)/(t2-t1))*cpy(2)+((t-t1)/(t2-t1))*cpy(3);
%% 2nd level to calculate P02
P02x=((t2-t)/(t2-t0)).*P01x+((t-t0)/(t2-t0)).*P11x;
P02y=((t2-t)/(t2-t0)).*P01y+((t-t0)/(t2-t0)).*P11y;
%% Plot interpolation
plot(P02x,P02y,'k')

%% curve 4 : Aitken
%% Control points & plot
cpx=[3.5 2.5 1.5]; cpy=[16 13 17];
hold on
%% Calculate t0, t1 and t2
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% 1st level to calculate P01 and P11
P01x=((t1-t)/(t1-t0))*cpx(1)+((t-t0)/(t1-t0))*cpx(2);
P01y=((t1-t)/(t1-t0))*cpy(1)+((t-t0)/(t1-t0))*cpy(2);
P11x=((t2-t)/(t2-t1))*cpx(2)+((t-t1)/(t2-t1))*cpx(3);
P11y=((t2-t)/(t2-t1))*cpy(2)+((t-t1)/(t2-t1))*cpy(3);
%% 2nd level to calculate P02
P02x=((t2-t)/(t2-t0)).*P01x+((t-t0)/(t2-t0)).*P11x;
P02y=((t2-t)/(t2-t0)).*P01y+((t-t0)/(t2-t0)).*P11y;
%% Plot interpolation
plot(P02x,P02y,'k')

%% curve 5 Lagrange
%% Control Point
cpx=[0 -1.5 -2]; cpy=[-2 4 3];
%% Calculate t
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% Calculate L0, L1 and L2
L0=((t-t1).*(t-t2))/((t0-t1).*(t0-t2));
L1=((t-t0).*(t-t2))/((t1-t0).*(t1-t2));
L2=((t-t0).*(t-t1))/((t2-t0).*(t2-t1));
%% Calculate Polynomial
Px=cpx(1)*L0+cpx(2)*L1+cpx(3)*L2;
Py=cpy(1)*L0+cpy(2)*L1+cpy(3)*L2;
%% Plot curve & points
plot(Px,Py)
hold on

%% curve 6 Lagrange
%% Control Point
cpx=[-2 -3.5 -3.5]; cpy=[3 4 -0.75];
%% Calculate t
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% Calculate L0, L1 and L2
L0=((t-t1).*(t-t2))/((t0-t1).*(t0-t2));
L1=((t-t0).*(t-t2))/((t1-t0).*(t1-t2));
L2=((t-t0).*(t-t1))/((t2-t0).*(t2-t1));
%% Calculate Polynomial
Px=cpx(1)*L0+cpx(2)*L1+cpx(3)*L2;
Py=cpy(1)*L0+cpy(2)*L1+cpy(3)*L2;
%% Plot curve & points
plot(Px,Py)
hold on

%% curve 7 Lagrange
%% Control Point
cpx=[-3.5 -8 -3.5]; cpy=[-0.75 8 16];
%% Calculate t
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% Calculate L0, L1 and L2
L0=((t-t1).*(t-t2))/((t0-t1).*(t0-t2));
L1=((t-t0).*(t-t2))/((t1-t0).*(t1-t2));
L2=((t-t0).*(t-t1))/((t2-t0).*(t2-t1));
%% Calculate Polynomial
Px=cpx(1)*L0+cpx(2)*L1+cpx(3)*L2;
Py=cpy(1)*L0+cpy(2)*L1+cpy(3)*L2;
%% Plot curve & points
plot(Px,Py)
hold on

%% curve 8 Lagrange
%% Control Point
cpx=[-3.5 -2.5 -1.5]; cpy=[16 13 17];
%% Calculate t
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% Calculate L0, L1 and L2
L0=((t-t1).*(t-t2))/((t0-t1).*(t0-t2));
L1=((t-t0).*(t-t2))/((t1-t0).*(t1-t2));
L2=((t-t0).*(t-t1))/((t2-t0).*(t2-t1));
%% Calculate Polynomial
Px=cpx(1)*L0+cpx(2)*L1+cpx(3)*L2;
Py=cpy(1)*L0+cpy(2)*L1+cpy(3)*L2;
%% Plot curve & points
plot(Px,Py)
hold on

%% Curve 9 : Bezier Curve


%% Value t
t=0:0.01:1;
%% Calculate Bezier control point
bpoint1 = [-1.5 17];
bpoint2 = [-1 14];
bpoint3 = [1 14];
bpoint4 = [1.5 17];
%% Calculate B03, B13, B23 and B33
B03 = (1-t).^3;
B13 = 3.*t.*((1-t).^2);
B23 = 3.*(t.^2).*(1-t);
B33 = t.^3;
%% Plot control points and control polygon
bx=[bpoint1(1),bpoint2(1),bpoint3(1), bpoint4(1)];
by=[bpoint1(2),bpoint2(2),bpoint3(2), bpoint4(2)];
hold on
%% Plot Bezier Curve
BPx = bx(1)*B03 + bx(2)*B13 + bx(3)*B23 + bx(4)*B33;
BPy = by(1)*B03 + by(2)*B13 + by(3)*B23 + by(4)*B33;
plot(BPx,BPy, 'r')

%% Curve 10 : Bezier Curve


%% Value t
t=0:0.01:1;
%% Calculate Bezier control point
bpoint1 = [-3 13];
bpoint2 = [-1 8];
bpoint3 = [1 8];
bpoint4 = [3 13];
%% Calculate B03, B13, B23 and B33
B03 = (1-t).^3;
B13 = 3.*t.*((1-t).^2);
B23 = 3.*(t.^2).*(1-t);
B33 = t.^3;
%% Plot control points and control polygon
bx=[bpoint1(1),bpoint2(1),bpoint3(1), bpoint4(1)];
by=[bpoint1(2),bpoint2(2),bpoint3(2), bpoint4(2)];
hold on
%% Plot Bezier Curve
BPx = bx(1)*B03 + bx(2)*B13 + bx(3)*B23 + bx(4)*B33;
BPy = by(1)*B03 + by(2)*B13 + by(3)*B23 + by(4)*B33;
plot(BPx,BPy, 'r')

%% curve 11 : Aitken
%% Control points & plot
cpx=[3 6 3.5]; cpy=[13 8 -0.75];
hold on
%% Calculate t0, t1 and t2
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% 1st level to calculate P01 and P11
P01x=((t1-t)/(t1-t0))*cpx(1)+((t-t0)/(t1-t0))*cpx(2);
P01y=((t1-t)/(t1-t0))*cpy(1)+((t-t0)/(t1-t0))*cpy(2);
P11x=((t2-t)/(t2-t1))*cpx(2)+((t-t1)/(t2-t1))*cpx(3);
P11y=((t2-t)/(t2-t1))*cpy(2)+((t-t1)/(t2-t1))*cpy(3);
%% 2nd level to calculate P02
P02x=((t2-t)/(t2-t0)).*P01x+((t-t0)/(t2-t0)).*P11x;
P02y=((t2-t)/(t2-t0)).*P01y+((t-t0)/(t2-t0)).*P11y;
%% Plot interpolation
plot(P02x,P02y,'k')

%% curve 12 Lagrange
%% Control Point
cpx=[-3 -6 -3.5]; cpy=[13 8 -0.75];
%% Calculate t
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% Calculate L0, L1 and L2
L0=((t-t1).*(t-t2))/((t0-t1).*(t0-t2));
L1=((t-t0).*(t-t2))/((t1-t0).*(t1-t2));
L2=((t-t0).*(t-t1))/((t2-t0).*(t2-t1));
%% Calculate Polynomial
Px=cpx(1)*L0+cpx(2)*L1+cpx(3)*L2;
Py=cpy(1)*L0+cpy(2)*L1+cpy(3)*L2;
%% Plot curve & points
plot(Px,Py)
hold on

%% curve 13 : Aitken
%% Control points & plot
cpx=[0.25 1 1.75]; cpy=[13 14 13];
hold on
%% Calculate t0, t1 and t2
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% 1st level to calculate P01 and P11
P01x=((t1-t)/(t1-t0))*cpx(1)+((t-t0)/(t1-t0))*cpx(2);
P01y=((t1-t)/(t1-t0))*cpy(1)+((t-t0)/(t1-t0))*cpy(2);
P11x=((t2-t)/(t2-t1))*cpx(2)+((t-t1)/(t2-t1))*cpx(3);
P11y=((t2-t)/(t2-t1))*cpy(2)+((t-t1)/(t2-t1))*cpy(3);
%% 2nd level to calculate P02
P02x=((t2-t)/(t2-t0)).*P01x+((t-t0)/(t2-t0)).*P11x;
P02y=((t2-t)/(t2-t0)).*P01y+((t-t0)/(t2-t0)).*P11y;
%% Plot interpolation
plot(P02x,P02y,'k')

%% curve 14 : Aitken
%% Control points & plot
cpx=[0.25 1 1.75]; cpy=[13 12 13];
hold on
%% Calculate t0, t1 and t2
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% 1st level to calculate P01 and P11
P01x=((t1-t)/(t1-t0))*cpx(1)+((t-t0)/(t1-t0))*cpx(2);
P01y=((t1-t)/(t1-t0))*cpy(1)+((t-t0)/(t1-t0))*cpy(2);
P11x=((t2-t)/(t2-t1))*cpx(2)+((t-t1)/(t2-t1))*cpx(3);
P11y=((t2-t)/(t2-t1))*cpy(2)+((t-t1)/(t2-t1))*cpy(3);
%% 2nd level to calculate P02
P02x=((t2-t)/(t2-t0)).*P01x+((t-t0)/(t2-t0)).*P11x;
P02y=((t2-t)/(t2-t0)).*P01y+((t-t0)/(t2-t0)).*P11y;
%% Plot interpolation
plot(P02x,P02y,'k')

%% curve 15 Lagrange
%% Control Point
cpx=[-0.25 -1 -1.75]; cpy=[13 14 13];
%% Calculate t
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% Calculate L0, L1 and L2
L0=((t-t1).*(t-t2))/((t0-t1).*(t0-t2));
L1=((t-t0).*(t-t2))/((t1-t0).*(t1-t2));
L2=((t-t0).*(t-t1))/((t2-t0).*(t2-t1));
%% Calculate Polynomial
Px=cpx(1)*L0+cpx(2)*L1+cpx(3)*L2;
Py=cpy(1)*L0+cpy(2)*L1+cpy(3)*L2;
%% Plot curve & points
plot(Px,Py)
hold on

%% curve 16 Lagrange
%% Control Point
cpx=[-0.25 -1 -1.75]; cpy=[13 12 13];
%% Calculate t
t0=0;
t1=t0+sqrt((cpy(2)-cpy(1))^2+(cpx(2)-cpx(1))^2);
t2=t1+sqrt((cpy(3)-cpy(2))^2+(cpx(3)-cpx(2))^2);
t=t0:0.01:t2;
%% Calculate L0, L1 and L2
L0=((t-t1).*(t-t2))/((t0-t1).*(t0-t2));
L1=((t-t0).*(t-t2))/((t1-t0).*(t1-t2));
L2=((t-t0).*(t-t1))/((t2-t0).*(t2-t1));
%% Calculate Polynomial
Px=cpx(1)*L0+cpx(2)*L1+cpx(3)*L2;
Py=cpy(1)*L0+cpy(2)*L1+cpy(3)*L2;
%% Plot curve & points
plot(Px,Py)
hold on
Explanations

 Blue line is drawn by using Lagrange Polynomial (3 Points)


 Black line is drawn by using Aitken Interpolation (3 Points)
 Red line is drawn by using Bezier Curve (4 Control Points)

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