27
COM335 TOPIC 5 DRAWING CURVES

COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

Embed Size (px)

Citation preview

Page 1: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM335

TOPIC 5

DRAWING CURVES

Page 2: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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.

Page 3: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM335

Page 4: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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

Page 5: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM335

Regular curves

Page 6: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

22 XRY

0 1 2 3 4 5 6 7 8 9 10 X

Y

Page 7: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

Cxmy .

tdcy

tbax

.

.

Parametric form of the equation

Page 8: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 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)

Page 9: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM335

Parametric form of the equation for a circle

x = Rcos()

y = Rsin()

(x,y)

Page 10: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

(X,Y)

(X,-Y)

(Y,X)(-Y,X)

(-X,Y)

(-X,-Y)

(-Y,-X) (Y,-X)

Exploiting symmetry in the circle

Page 11: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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

Page 12: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM335

General curves

Page 13: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

33

2210

33

2210

..

...

tbtbtbby

tatataax

Parametric cubic equations

Page 14: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

P0

P1

P2

P3

Figure 4.4

The Bezier Curve

Page 15: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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

Page 16: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The 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)

Page 17: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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)

Page 18: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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

Page 19: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

P0

P1

P2

P3

Figure 4.6 : P2, P3 & P4 are co-linear to achieve continuity

P3

P4 P5

P6

Joining Bezier Curves

Page 20: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

Ni-1

Ni

Ni+1

Si

Si+1

Figure 4.7 : Spline Segments

33

2210

33

2210

tbtbtbby

tatataax

Page 21: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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

Page 22: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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

Page 23: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

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

Page 24: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM366

Errors with linear interpolation

Page 25: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM335

Errors with cubic interpolation

Page 26: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM335

Errors with spline interpolation

Page 27: COM335 TOPIC 5 DRAWING CURVES. COM335 Two types of curve : Regular curves Which have a simple equation. For example the circle x 2 + y 2 = r 2 The equation

COM335