Upload
sylvia-lawrence
View
212
Download
0
Embed Size (px)
Citation preview
COM335
TOPIC 5
DRAWING CURVES
COM335
Two types of curve :
Regular curves
Which have a simple equation. For example the circle x2 + y2 = r2
The equation is used, usually in parametric form to draw the curve.
General curvesEither have no equation which describes their properties or their
equation if known is too complex to use in practice. For example the
shape of a car body, a ships hull, the profile of a leaf.
COM335
COM335
All curves will be drawn as line segments between nodes
Generally speaking we wish to minimise the number of nodes we store - particularly for general curves
COM335
Regular curves
22 XRY
0 1 2 3 4 5 6 7 8 9 10 X
Y
Cxmy .
tdcy
tbax
.
.
Parametric form of the equation
COM335
(XS,YS)
(XF,YF)
(X,Y)
t = 0
t = 1
a = XS b = (XF – XS)
d = (YF – YS)c = YS
t = 0.3
X = XS + t*(XF – XS)
Y = YS + t*(YF – YS)
COM335
Parametric form of the equation for a circle
x = Rcos()
y = Rsin()
(x,y)
(X,Y)
(X,-Y)
(Y,X)(-Y,X)
(-X,Y)
(-X,-Y)
(-Y,-X) (Y,-X)
Exploiting symmetry in the circle
COM335
Regular curves you might draw
2D the ellipse
Method 2 : Draw a circle of radius 1and then do a 2D scaling transform
by ‘a’ in the x direction and by ‘b’ in the y direction
Method 1 : use the parametric equation of the ellipse :
x = a cos() & y = bsin()
3D the helixUse the parametric equation of the helix :
x = Rcos() & y = Rsin() z = C
COM335
General curves
33
2210
33
2210
..
...
tbtbtbby
tatataax
Parametric cubic equations
P0
P1
P2
P3
Figure 4.4
The Bezier Curve
3
1)(
1..31..2
1.1.1.
13
1)(
0..30..2
0.0.0.
0
23321
32101
01321
32100
xxaaa
dt
dx
aaaax
tAt
xxaaa
dt
dx
aaaax
tAt
Applying the boundary conditions for ‘x’ equation
3
32
21
20
3
33
22
12
03
..13.13.)1(
..13.13.)1(
ytyttyttyty
xtxttxttxtx
Bezier Blending Polynomials
0
0.2
0.4
0.6
0.8
1
1.2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
B(t
)
B(0)
B(1) B(2)
B(3)
COM366
Drawing a Bezier curve segment in MATLABfunction Bezierdemo(N)
%To demonstrate drawing a simple Bezier curve
%N is the 4x2 control node array
%Draw the control nodes and convex hull
plot(N(:,1),N(:,2),'-o')
axis([0,10,0,10]);
hold on
%calculate the Bezier curve positions at 10 points
k = 0;
xx = zeros(1,11); yy = zeros(1,11);
for t = 0:0.1:1
k = k+1;
xx(k) = ((1-t)^3)*N(1,1) +3*t*((1-t)^2)*N(2,1)+3*(t^2)*(1-t)*N(3,1)+(t^3)*N(4,1);
yy(k) = ((1-t)^3)*N(1,2) +3*t*((1-t)^2)*N(2,2)+3*(t^2)*(1-t)*N(3,2)+(t^3)*N(4,2);
end
%Use interpolation to get a smooth curve
xxi = [N(1,1):0.01:N(4,1)];
yyi = interp1(xx,yy,xxi,'spline');
plot(xxi,yyi,'Color','red', 'LineWidth',2)
COM366
>> N = [1 2; 2 5;6 8;8 3]
N =
1 2
2 5
6 8
8 3
>> Bezierdemo(N)
Entering the data and drawing the result
P0
P1
P2
P3
Figure 4.6 : P2, P3 & P4 are co-linear to achieve continuity
P3
P4 P5
P6
Joining Bezier Curves
Ni-1
Ni
Ni+1
Si
Si+1
Figure 4.7 : Spline Segments
33
2210
33
2210
tbtbtbby
tatataax
Fixed gradient. The value of (dy/dx) is set at either end of the curve.
Free end. The gradient is unconstrained at the end (In effect the second derivativeis 0 at either end.
Contour. In which the curved is joined to form a closed loop. (In effect the last nodeis = the first node). The name comes from the use of this condition to representheight contour lines on a topographical map.
Natural Spline End Conditions
COM366
Method Notes
Nearest Selects the nearest node point. Results in a set discontinuous segments
Linear Draws a straight line between each pair of nodes. Results in abrupt gradient changes at the nodes.
Spline Natural cubic spline interpolation. The ‘free end’ end condition is used as a default. In fact interp1 with a spline method calls a more basic ‘spline’ function in which you can define any of the three end conditions.
Pchip, cubic A cubic Hermite polynomial function. You will find this referred to in the graphics literature but the mathematics is beyond this course.
V5cubic Not much use now but is the cubic interpolation scheme used in MATLAB 5
Interpolation methods for the MATLAB interp1 function
COM335
function Interptest
%To test the errors of various interpolation methods
%The test curve is a sine curve between 0 and 180 degrees
x = [0:pi/6:pi];
y = sin(x);
subplot(2,1,1); plot(x,y,'ro')
axis([0 pi -0.05 1.1])
hold on
%Now interpolate with various methods
%Alternatives to try :
% 'nearest'
% 'linear'
% 'spline'
% 'cubic'
xi = [0:pi/60:pi];
yi = interp1(x,y,xi,'spline');
subplot(2,1,1);plot(xi,yi)
hold off
%Now calculate the true values and display errors
yt = sin(xi);
error = yt - yi;
subplot(3,1,3); plot(xi,error)
%To see the errors clearly change the y range as follows
% nearest -0.5 0.5
% linear -0.05 0.05
% 'cubic' -0.02 0.02
% 'spline' -0.002 0.002
axis([0 pi -0.002 0.002])
COM366
Errors with linear interpolation
COM335
Errors with cubic interpolation
COM335
Errors with spline interpolation
COM335