Fundamentals of Graphics Using MATLAB® Ranjan Parkeh
Fundamentals of Graphics Using MATLAB® Ranjan Parkeh
Using MATLAB®
Fundamentals of Graphics
Using MATLAB®
Ranjan Parekh
MATLAB ® is a trademark of The MathWorks, Inc. and is used with permission. The MathWorks does not warrant the
accuracy of the text or exercises in this book. This book’s use or discussion of MATLAB® software or related products
does not constitute endorsement or sponsorship by The MathWorks of a particular pedagogical approach or particular
use of the MATLAB ® software
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been
made to publish reliable data and information, but the author and publisher cannot assume responsibility for the
validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the
copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to
publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let
us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or
utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including
photocopying, microfilming, and recording, or in any information storage or retrieval system, without written
permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://
www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA
01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users.
For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been
arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.
Preface, xi
Author, xv
v
vi ◾ Contents
REFERENCES407
INDEX409
Preface
xi
xii ◾ Preface
Chapter 2 introduces the concept of blending functions and how these functions are
used to derive equations for hybrid splines which pass through only a subset of their control
points or where conditions other than control points are used for deriving their equations.
Specifically, the chapter deals with the Hermite spline, Cardinal spline, Catmull–Rom
spline, and Bezier spline. For Bezier splines, both the quadratic and cubic variants are dis-
cussed along with Bernstein polynomials used to formulate their blending functions. As
in other chapters, the theoretical concepts are followed by numerical examples, MATLAB
codes and graphical plots for visualization. The chapter ends with a discussion about how
one spline type can be converted to another.
Chapter 3 discusses how polynomial equations are derived for approximating splines
that do not pass through any of their control points and how their blending functions
are computed. Specifically, the chapter provides detailed discussions about the Cox de
Boor algorithm and how it can used to derive equations for linear, quadratic, and cubic
B-splines. Essentially, B-splines consist of multiple curve segments with continuity at join
points. Values of the parametric variable at the join points are stored in a vector called
the knot vector. If the knot values are equally spaced, then the resulting spline is called
uniform B-spline; otherwise, it is referred to as non-uniform. B-splines are called open-
uniform when knot vector values are repeated. The chapter provides representations of the
knot vector and illustrates how the spacing in the vector generates the above-mentioned
variants. As before, the theoretical concepts are followed by numerical examples, MATLAB
codes, and graphical plots for visualization.
Chapter 4 formally introduces a 2D coordinate system and then lays the foundations of
a homogeneous coordinate system using which all the transformations can be represented
in a uniform manner. Two-dimensional transformations are used to change the location,
orientation, and shapes of splines in 2D plane. These transformations are translation, rotation,
scaling, reflection, and shear applied individually or in combination of two or more; hence, they
are known as composite transformations. Given known coordinates of a point, each of these
transformations is represented by a matrix which when multiplied to the original coordinates
produces a new set of transformed coordinates. The transformation matrices are first derived,
and then their applications are illustrated using examples, MATLAB codes, and graphical
plots. Both affine and perspective transformation types are discussed. The chapter ends with a
discussion on viewing transformations used for mapping a window to a viewport, and coordi-
nate system transformation used for mapping between multiple coordinate systems.
Chapter 5 enumerates some of the common properties of splines and how these can be
calculated from spline equations. First, it discusses the critical points namely minimum
and maximum of spline curves. Additionally for splines of degree 3 or more, the point of
inflection (POI) is of interest. Next, it discusses how the tangent and normal to a spline
curve can be calculated. The tangent to a curve is the derivative of the curve equation,
while the normal is the line perpendicular to the tangent. The third property is calculation
of length of a spline curve between any two given points, both for spatial and parametric
equations. The fourth property is to calculate the area under a curve, which is bounded
by a primary axis and two horizontal or vertical lines. An extension to this is calculation
of area bounded by two curves. The fifth property is calculation of centroid of an area,
Preface ◾ xiii
the point of the center of gravity for plates of uniform density. The chapter ends with a
discussion on interpolation and curve fitting for data points and a list of some commonly
used built-in MATLAB functions for plotting 2D graphs and plots.
The second part of this book focuses on concepts and problems related to 3D graphics
and spans over the remaining four chapters namely (6) Vectors, (7) 3D Transformations,
(8) Surfaces, and (9) Projections.
Chapter 6 introduces the concept of vectors and their mathematical representations in
2D and 3D spaces. Vectors involve both magnitude and direction. They are represented
in terms of orthogonal reference components of unit magnitudes along the primary axes
together with a set of scaling factors. The chapter discusses how vectors can be added and
multiplied together. Vector products can either be scalar, called a dot product, or vector,
called a cross product. Using these concepts, the chapter then provides details of how vec-
tor equations of lines and planes can be derived. Next, the chapter discusses how vectors
can be aligned to specific directions and finally how vector equations can be represented
using homogeneous coordinates. The chapter ends with a section on how the tangent vector
and the normal vector can be calculated for a curve. As before, the theoretical concepts are
followed by numerical examples, MATLAB codes, and graphical plots for visualization.
Chapter 7 demonstrates how 3D transformations can be treated as extensions of 2D
transformations. These are used to change the location, orientation, and shapes of splines
in 3D space. These transformations are translation, rotation, scaling, reflection, shear
applied individually or in combination of two or more, known as composite transforma-
tion. This chapter formally introduces a 3D coordinate system and then uses homogeneous
coordinates to derive transformation matrices for the above operations. Their applications
are then illustrated using examples, MATLAB codes and graphical plots for v isualization.
The latter part of the chapter deals with vector alignment in 3D space and uses these
concepts to derive rotation matrices in 3D space around vectors and arbitrary lines.
Chapter 8 takes a look at how surfaces can be created and represented using parametric
and implicit equations, and how the nature of the surface depends on the parameters of
the equations. Depending on creation process, surfaces can be categorized as extruded
and surfaces of revolution, both of which are discussed with examples and graphical plots.
The chapter then takes a look at how tangent planes of surfaces can be computed and
provides methods for computing area and volume of surfaces. The latter part of the chapter
deals with surface appearances namely how textures can be mapped on surfaces and how
illumination models can be used to determine brightness intensities at a point on the
surface. The chapter ends with a discussion on some commonly used built-in MATLAB
functions for plotting 3D graphs.
Chapter 9 studies various types of projections and derives matrices for each. Projection
is used to map a higher-dimensional object to a lower-dimensional view. Projection can be
of two types: parallel and perspective. In parallel projection, projection lines are parallel to
each other, while in perspective projection, projection lines appear to converge to a r eference
point. Parallel projection can again be of two types: orthographic and oblique. In parallel
orthographic projection, the projection lines are perpendicular to the view plane, while
in parallel oblique projection, the projection lines can be oriented at any arbitrary angle
xiv ◾ Preface
to the view plane. Usually for 3D projection, parallel orthographic projection can also be
sub-divided into two types: multi-view and axonometric. In multi-view projection, the
projection occurs on the primary planes i.e. XY-, YZ-, or XZ-planes, while in axonometric
projection, the projection occurs on any arbitrary plane. The chapter illustrates each type
of projection using examples, MATLAB codes, and graphical plots for visualization.
Each chapter is followed by a summarized list of salient points discussed in the chapter.
A set of review questions and a list of practice problems are provided at the end of each
chapter for self-evaluation. This book contains more than 90 solved numerical examples
with their corresponding MATLAB codes and an additional 90 problems given for prac-
tice. Readers are encouraged to execute the codes given in the examples and also write
their own codes to solve the practice problems. Most of the MATLAB codes given in this
book will require MATLAB version 2015 or later to execute properly. Some of the func-
tions mentioned have been specifically introduced from version 2016 and these have been
mentioned at the appropriate places. The usage of about 70 different MATLAB functions
related to graphics and plotting have been demonstrated in this book and a list of these
functions with a short description is provided at Appendix I. Readers are asked to use
MATLAB help utilities to get further information on these. The MATLAB codes are writ-
ten in a verbose manner for a better understanding of the readers who are new to the
subject matter. Some of the codes could have been written in a more compact manner but
that might have reduced their comprehensibility. Around 170 figures have been included in
this book to help the readers get proper visualization cues of the problems especially for 3D
environments. Answers to the practice problems are provided in Appendix II.
All readers are encouraged to provide feedback about the content matter of the book as
well as any omissions or typing errors. The author can be contacted at ranjan_parekh@
yahoo.com.
Ranjan Parekh
Jadavpur University
Calcutta 700032, India
2019
Dr. Ranjan Parekh, PhD (Engineering), is Professor at the School of Education
Technology, Jadavpur University, Calcutta, India, and is involved with teaching subjects
related to Graphics and Multimedia at the post graduate level. His research interests
include multimedia information processing, pattern recognition, and computer vision.
He is also the author of Principles of Multimedia (McGraw Hill, 2012; http://www.mhhe.
com/parekh/multimedia2).
xv
Chapter 1
Interpolating Splines
1.1 INTRODUCTION
Splines are irregular curve segments with known mathematical properties. Splines are fre-
quently encountered in vector graphics when graphic objects are required to have a defined
shape in 2D planes (Figure 1.1a) or 3D space (Figure 1.1b) or moved along a specified path
(Figure 1.1c). Based on the coordinates of some of the points on the curves, or slopes of
lines along the curves, the graphics system needs to calculate a mathematical representa-
tion of the curve before storing them onto a disk. This representation usually takes the
form of “vectors” or a series of values stored in matrices. The values are calculated using
an orthogonal 2D coordinate system consisting of the origin, X-axis, and Y-axis. These
coordinate axes are often called the primary or principal axes.
The term “spline” has been derived from the ship building industry where it is used to
refer to wooden planks bent between wooden posts for building the curved hull of ships
(O’Rourke, 2003). The location of the fixed posts controlled the shape of the plank. In
graphics, we use specific points along the spline curve to control the shape of the spline
and hence they are aptly referred to as “control points,” shortened as CPs. Depending on
the relationship between the CPs and the actual curves, the splines can be broadly cat-
egorized into three types: (1) interpolating splines, where the spline actually goes through
the CPs; (2) approximating splines, where the spline goes near the CPs but not actually
through them; and (3) hybrid splines, where the spline goes through some of the CPs but
not through all (Hearn and Baker, 1996) (see Figure 1.2).
Splines are mathematically modeled using polynomials. Polynomials are expressions
constructed from variables and constants, and involve addition, subtraction, multiplication,
and non-negative integer exponents. A polynomial can be 0 (zero) or a sum of non-zero
terms. Each term consists of a constant, called coefficient, multiplied by a variable. The
exponent of the variable is called its degree. The first example is a valid polynomial, but
the second example is not, because the variable is associated with a division operation and
also because of the fractional exponent. The general nth degree polynomial is shown in the
third example.
1
2 ◾ Fundamentals of Graphics Using MATLAB®
(a)
(b)
10
0
z
-5
-10
10 10
5 5
0 0
-5 -5
y -10
-10 x
(c)
FIGURE 1.1 Use of splines in graphics for creating (a) 2D shapes (b) 3D surfaces (c) motion path
trajectory
Interpolating Splines ◾ 3
1 − 2 x + 3x 2
2
1− + 3x 2.5
x
an x n + an −1x n −1 + + a1x + a0
A polynomial equation is written when one polynomial is set equal to another. It can
either be in explicit form e.g. y = f (x ) when either side of the equation contains variables of
explicit type, or it can be in implicit form e.g. f ( x , y ) = 0 where multiple types of variables
can be on the same side. Examples are shown below:
y = x2
x 3 + y 3 − 5 xy = 0
Polynomial equations can also be represented in parametric form where the variables are
expressed as functions of another variable t e.g. x = f (t ), y = g (t ). The advantage of para-
metric equations is that the variables x and y do not need to be constrained by a single
equation and can be changed independently of each other, which offers more flexibility for
representing complex curves. As a convention, the value of t is usually taken to lie between
0 and 1 unless otherwise specified. The value of t = 0 corresponds to the start point and
t = 1 to the end point of the spline curve. Examples are shown below:
x = t, y = t2
x = r ⋅ cos t , y = r ⋅ sin t
A polynomial equation is frequently represented using graphs, which are useful in visually
depicting how one variable changes with another. The graph of a zero polynomial i.e. f ( x ) = 0
is the X-axis. The graph of a zero degree polynomial represented by f ( x ) = a, where a is a
constant, is a line parallel to the X-axis at a distance a from it. The graph of a degree 1 polyno-
mial, represented by f ( x ) = a + bx, is a straight line with a slope b and intercept a. The graph
4 ◾ Fundamentals of Graphics Using MATLAB®
0 0 0
-5 -5 -5
-5 0 5 -5 0 5 -5 0 5
0 0 0
y
-5 -5 -10
-5 0 5 -5 0 5 -10 -5 0 5 10
x
0 0 0
-1 -1 -1
-4 -2 0 2 4 -4 -2 0 2 4 -1 -0.5 0 0.5 1
a
y = a + bx = 1 x (1.1)
b
Substitute the given points in the starting equation to generate two equations. Two equa-
tions are sufficient to solve for the two unknown coefficients a and b
Interpolating Splines ◾ 5
y1 = a + bx1
(1.2)
y 2 = a + bx 2
The two equations are written in matrix form Y = C·A, where C is the constraint matrix and
A is the coefficient matrix:
y1 1 x1 a
= (1.3)
y 2 1 x2 b
The equations are solved to find the values of the unknown coefficients. Thus, we have
A = inv(C)·Y
−1
a 1 x1 y1
b = 1 x2
(1.4)
y 2
The values of the coefficients are substituted in the starting equation to arrive at the
equation of the spline.
−1
1 x1 y1
y = 1 x (1.5)
1 x 2 y 2
Example 1.1
Find the equation of a line through points P1(3, 2) and P2(8, −4).
Choose a starting equation
a
y = a + bx = 1 x
b
2 = a + b(3)
−4 = a + b(8)
2 1 3 a
−4 = 1 8 b
6 ◾ Fundamentals of Graphics Using MATLAB®
P1
2
-1
y
-2
-3
-4
P2
-5
0 2 4 6 8 10
x
y = 5.6 − 1.2 x
b = A(2);
fprintf('Required equation : \n');
y = a + b*x;
y = vpa(y)
%plotting
xx = linspace(x1, x2);
yy = subs(y, x, xx);
plot(xx, yy, 'b-'); hold on;
scatter(X, Y, 20, 'r', 'filled');
xlabel('x'); ylabel('y');
grid; axis square;
axis([0 10 -5 3]);
d = 0.5;
text(x1+d, y1, 'P_1');
text(x2+d, y2, 'P_2');
hold off;
NOTE
%: signifies a comment line
axis: controls appearance of the axes of the plot, specifies ordered range of values to display
clc: clears workspace of previous text
clear: clears memory of all stored variables
fprintf: prints out strings and values using formatting options
grid: turns on display of grid lines in a plot
hold: holds the current graph state so that subsequent commands can add to the same graph
inv: computes inverse of a matrix
linspace: creates 100 linearly spaced values between the two end-points specified
plot: creates a graphical plot from a set of values
scatter: type of plot where the data is represented by colored circles
subs: substitutes symbolic variable with a matrix of values for generating a plot
syms: declares the arguments following as symbolic variables
text: inserts textual strings at specified locations in the graph
title: displays a title on top of the graph
vpa: displays symbolic values as variable precision floating point values
xlabel, ylabel: puts text labels along the corresponding primary axes
An alternate form of the standard line equation can be formed where, instead of two
given points, only one point and the slope of the line are given. This aspect is discussed
below.
Let the given points be P1(x1, y1) and s be the slope of the line. Choose a starting linear
equation that is written in matrix form as before in Equation (1.1).
a
y = a + bx = 1 x
b
8 ◾ Fundamentals of Graphics Using MATLAB®
dy
y′ = = b (1.6)
dx
Substitute the given values in the starting equation to generate two equations
y1 = a + bx1
(1.7)
s =b
The two equations are written in matrix form Y = C·A as before
y1 1 x1 a
= (1.8)
s 0 1 b
The equations are solved to find the values of the coefficients: A = C−1·Y
−1
a 1 x1 y1
b = (1.9)
0 1 s
The values of the coefficients are substituted in the starting equation to arrive at the
equation of the spline.
−1
1 x1 y1
y = 1 x (1.10)
0 1 s
Example 1.2
Find the equation of a line through the point P(−1, 1) and having slope 2.
Choose a starting equation
a
y = a + bx = 1 x
b
dy
y′ = = b
dx
1 = a + b(−1)
2=b
Interpolating Splines ◾ 9
2
y
P
0
-2
-4
-4 -3 -2 -1 0 1 2
x
1 1 −1 a
2 = 0 1 b
a 1 1 1 3
b = 0 =
1 2 2
y = 2 x + 3
dy
Verification: Putting x = −1 in the above equation, we get y = 1. Also, slope =2
dx
(Figure 1.5).
A = inv(C)*Y';
a = A(1);
b = A(2);
fprintf('Required equation:\n');
y = a + b*x;
y = vpa(y)
%plotting
xx = linspace(x1-3, x1+3);
yy = subs(y, x, xx);
plot(xx, yy, 'b-', x1, y1, 'bo'); hold on;
scatter(x1, y1, 20, 'r', 'filled');
xlabel('x'); ylabel('y');
grid; axis square; axis tight;
text(x1+0.5, y1, 'P');
hold off;
a
P(t ) = a + bt = 1 t (1.11)
b
Substitute given points in the starting equation by choosing t = 0 at start and t = 1 at end.
P0 = a + b(0)
(1.12)
P1 = a + b(1)
Write equations in matrix form G = C·A, where G is called the geometry matrix
P0 1 0 a
= (1.13)
P1 1 1 b
Solve the equation for A i.e. A = C−1·G = B·G, where B is called the basis matrix
−1
a 1 0 P0
b = (1.14)
1 1 P1
Interpolating Splines ◾ 11
−1
1 0 P0
P(t ) = 1 t (1.15)
1 1 P1
NOTE
In reality, the parametric equations should be written separately for x and y i.e.
x (t ) = ax + bx ⋅ t and y(t ) = a y + by ⋅ t . However, we use a compact notation here by substituting
P(t ) = [x (t ), y(t )], a = ax , a y , b = bx , by . After solving for a and b, we separate out the
individual components and substitute them in the respective equations for x and y.
Example 1.3
Find the equation of a line through points P0(3, 2) and P1(8, −4) in parametric form.
Choose a starting equation.
a
P(t ) = a + bt = 1 t
b
Write equations in matrix form G = C·A, where G is called the geometry matrix
3 2 1 0 a
8 =
−4 1
1 b
3 2
P(t ) = 1 t
5 −6
The required parametric equations are obtained by separating out the x and y
components
x = 3 + 5t
y = 2 − 6t
7 1 1
0 0
6
-1 -1
x
y
5
-2 -2
4 -3 -3
P1
3 -4 -4
0 0.5 1 0 0.5 1 2 4 6 8
t t x
%plotting
tt = linspace(0,1);
xx = subs(x, t, tt);
yy = subs(y, t, tt);
NOTE
subplot: displays multiple plots within a single figure window
Substitute the given points in the starting equation to generate three equations. They are
sufficient to solve for the three unknown coefficients a, b, and c.
y1 = a + bx1 + cx1 2
y 2 = a + bx 2 + cx 2 2 (1.17)
y3 = a + bx 3 + cx 3 2
The three equations are written in matrix form Y = C·A as before
y1 1 x1 x1 2 a
y2 = 1 x2 x 2 2 b (1.18)
y 1 x3
x 3 2 c
3
The equations are solved to find the values of the coefficients: A = C−1·Y
−1
a 1 x1 x1 2 y1
b = 1
x2 x2 2 y2 (1.19)
c 1 x3
x 3 2 y3
The values of the coefficients are substituted in the starting equation to arrive at the
e quation of the spline
−1
1 x1 x1 2 y1
y = 1 x x 2 1 x2 x2 2 y2
(1.20)
1 x3 x 3 2 y3
14 ◾ Fundamentals of Graphics Using MATLAB®
Example 1.4
Find the equation of a quadratic spline through points P1(3, 2), P2(6, 5), and P3(8, −4).
Choose starting equation
a
y = a + bx + cx = 12
x x 2 b
c
2 1 3 9 a
−4 = 1 8 64 b
5 1 6 36 c
5
P2
P1
P3
-5
y
-10
-15
-20
1 2 3 4 5 6 7 8 9 10
x
%plotting
d = 0.5;
xx = linspace(x1-2, x3+2);
yy = subs(y, x, xx);
plot(xx,yy, 'b-');
hold on; grid;
scatter(X, Y, 20, 'r', 'filled');
xlabel('x'); ylabel('y');
axis square; axis tight;
text(x1+d, y1, 'P_1');
text(x2+d, y2, 'P_2');
text(x3+d, y3, 'P_3');
hold off;
a
P(t ) = a + bt + ct = 1
2
t t 2 b (1.21)
c
Substitute the given points in the starting equation by choosing t = 0 at start, t = k at the
middle, and t = 1 at end.
16 ◾ Fundamentals of Graphics Using MATLAB®
P0 = a + b(0) + c(0)2
P2 = a + b(1) + c(1)2
P0 1 0 0 a
P1 = 1 k k2 b (1.23)
P2 1 1 1 c
−1
a 1 0 0 P0
b = 1 k k2
P1 (1.24)
c 1 1 1
P2
−1
1 0 0 P0
P(t ) = 1 t t 2 1 k k2
P1 (1.25)
1 1 1 P2
Example 1.5
Find the equation of a quadratic spline through points P0(3, 2), P1(8, −4),
and P2(6, 5) in parametric form with sub-division ratio k = 0.8.
Choose starting equation
a
P(t ) = a + bt + ct = 1
2
t t 2 b
c
3 2 1 0 0 a
8 −4 = 1 0.8 0.64 b
6 5 1 1 1 c
Interpolating Splines ◾ 17
t -x t-y x-y
9 5 5
P2
8 P0
7 0 0
6
x
y
P1
5 -5 -5
3 -10 -10
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 2 4 6 8 10
t t x
a 3 2
b = 19.25 −49.5
c −16.25 52.5
x = 3 + 19.25t − 16.25t 2
2
y = 2 − 49.5t + 52.5t
Verification: x (0) = 3, y(0) = 2, x (0.8) = 8, y(0.8) = −0.4, x (1) = 6, y(1) = 5 (Figure 1.8).
%plotting
tt = linspace(0,1);
xx = subs(x, t, tt);
18 ◾ Fundamentals of Graphics Using MATLAB®
yy = subs(y, t, tt);
subplot(131), plot(tt, xx);
xlabel('t'); ylabel('x'); title('t - x');
grid; axis square;
subplot(132), plot(tt, yy);
xlabel('t'); ylabel('y'); title('t - y');
grid; axis square;
subplot(133), plot(xx, yy, 'b-');
hold on; grid; axis square;
scatter(X, Y, 20, 'r', 'filled');
xlabel('x'); ylabel('y'); title('x - y');
d = 0.5;
text(x0+d, y0, 'P_0');
text(x1+d, y1, 'P_1');
text(x2-1, y2-1, 'P_2');
hold off;
y 2 = a + bx 2 + cx 2 2 + dx 2 3
(1.27)
y3 = a + bx 3 + cx 3 2 + dx 3 3
y 4 = a + bx 4 + cx 4 2 + dx 4 3
The four equations are written in matrix form Y = C·A
y1 1 x1 x1 2 x1 3 a
y2 1 x2 x2 2
x2 3 b
=
y3 c
(1.28)
1 x3 x3 2 x3 3
y4 1 x4 x4 2 x 4 3 d
Interpolating Splines ◾ 19
The equations are solved to find the values of the coefficients: A = C−1·Y
−1
1 x1 x1 2 x1 3 y1
a
b 1 x2 x2 2 x2 3 y2
= (1.29)
c 1 x3 x3 2 x3 3 y3
d 1 x4 x4 2 x 4 3 y4
The values of the coefficients are substituted in the starting equation to arrive at the
equation of the spline.
−1
1 x1 x1 2 x1 3 y1
1 x2 x2 2 x2 3 y2
y = 1 x x2 x3 (1.30)
1 x3 x3 2 x3 3 y3
1 x4 x4 2 x 4 3 y4
Example 1.6
Find the equation of a cubic spline through points P1(−1, 2), P2(0, 0), P3(1, −2),
and P4(2, 0).
Choose starting equation
a
b
y = a + bx + cx 2 + dx 3 = 1 x x2 x3
c
d
2 1 −1 1 −1 a
0 1 0 0 0 b
=
−2 1 1 1 1 c
0 1 2 4 8 d
a 0
b −2.67
=
c 0
d 0.67
20 ◾ Fundamentals of Graphics Using MATLAB®
12
10
4
y
P1
2
P2
0
P4
P3
-2
-4
-2 -1 0 1 2 3
x
y = −2.67 x + 0.67 x 3
%plotting
X = [x1, x2, x3, x4]; m = min(X); n = max(X);
xx = linspace(m - 1, n + 1);
yy = subs(y, x, xx);
plot(xx,yy, 'b');
hold on; grid;
scatter(X, Y, 20, 'r', 'filled');
xlabel('x'); ylabel('y'); axis square;
e = 1;
text(x1, y1+e, 'P_1');
text(x2, y2+e, 'P_2');
text(x3, y3+e, 'P_3');
text(x4, y4+2*e, 'P_4');
hold off;
Interpolating Splines ◾ 21
22 ◾ Fundamentals of Graphics Using MATLAB®
a
b
P(t ) = a + bt + ct 2 + dt 3 = 1 t t2 t3 (1.31)
c
d
Substitute the given points in the starting equation by choosing t = 0 at start, t = m, n at the
middle points, and t = 1 at end.
P0 = a + b(0) + c(0)2
P1 = a + b(m) + c(m)2
(1.32)
2
P2 = a + b(n) + c(n)
P3 = a + b(1) + c(1)2
P0 1 0 0 0 a
P1 = 1 m m2 m3 b
(1.33)
P2 1 n n2 n3 c
d
P3 1 1 1 1
−1
a 1 0 0 0 P0
b 1 m m2 m3 P1 (1.34)
=
c 1 n n2 n3 P2
d
1 1 1 1 P3
−1
1 0 0 0 P0
1 m m2 m3 P1 (1.35)
P(t ) = 1 t t2 t3
1 n n2 n3 P2
1 1 1 1 P3
Example 1.7
Find the equation of a cubic spline through points (−1, 2), (0, 0), (1, −2), and
(2, 0) in parametric form with sub-division ratios m = 0.1 and n = 0.9.
Choose starting equation written in matrix form
a
b
P(t ) = a + bt + ct 2 + dt 3 = 1 t t2 t3
c
d
−1 2 1 0 0 0 a
0 0 1 0.1 0.01 0.001 b
=
1 −2 1 0.9 0.81 0.729 c
2 0 1 1 1 1 d
a −1 2
b 12.7222 −21.4444
=
c −29.1667 13.8889
d 19.4444 5.5556
Verification: x (0) = −1, x (0.1) = 0, x (0.9) = 1, x (1) = 2, y(0) = 2, y(0.1) = 0, y(0.9) = −2,
y(1) = 0
The actual values might differ slightly due to round-off errors (Figure 1.10).
24 ◾ Fundamentals of Graphics Using MATLAB®
1.5 1
0 P3
P1
0
1 -1
-1
x
y
0.5 -2
-2
P2
0 -3
-3
-0.5 -4 -4
-1 -5 -5
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 -2 -1 0 1 2 3
t t x
%plotting
tt = linspace(0,1);
xx = subs(x, t, tt);
yy = subs(y, t, tt);
subplot(131), plot(tt,xx); grid;
xlabel('t'); ylabel('x'); title('t - x'); axis square;
subplot(132), plot(tt,yy); grid;
xlabel('t'); ylabel('y'); title('t - y'); axis square;
subplot(133), plot(xx,yy,'b-'); grid;
xlabel('x'); ylabel('y'); title('x - y'); axis square;
hold on;
scatter(X, Y, 20, 'r', 'filled');
axis([-2 3 -5 3]);
e = 0.5;
text(x0+e, y0, 'P_0');
text(x1+e, y1, 'P_1');
Interpolating Splines ◾ 25
3
A
B
2.5 C
A
P2
2
1.5
C
y
1
P1
0.5
B
0
P3 P4
-0.5
0 1 2 3 4 5 6 7 8
x
B: y = a2 + b2 x + c2 x 2 + d2 x 3 (1.36)
C: y = a3 + b3 x + c3 x 2 + d3 x 3
So altogether there are 12 different unknowns and at least 12 different equations are needed
to solve them.
In order to formulate these 12 equations, various constraints are used to ensure that three
separate spline segments join together to form a single smooth curve. The first c onstraint
is known as C0 continuity condition, which states that in order to form a smooth curve the
three splines should physically meet at their joining points (Hearn and Baker, 1996). In
other words, spline A should pass through points P1 and P2, spline B should pass through
points P2 and P3, and spline C should pass through points P3 and P4. Substituting the point
coordinates in the respective starting equations the f ollowing six equations are obtained.
If S(Pk) denotes segment S passing through point Pk, we can write:
A( P1 ): y1 = a1 + b1x1 + c1 x1 2 + d1 x1 3
A( P2 ): y 2 = a1 + b1x 2 + c1 x 2 2 + d1 x 2 3
B( P2 ): y 2 = a2 + b2 x 2 + c2 x 2 2 + d2 x 2 3
(1.37)
B( P3 ): y3 = a2 + b2 x 3 + c2 x 3 2 + d2 x 3 3
C ( P3 ): y3 = a3 + b3 x 3 + c3 x 3 2 + d3 x 3 3
C ( P4 ): y 4 = a3 + b3 x 4 + c3 x 4 2 + d3 x 4 3
The second constraint to be obeyed is known as C1 continuity condition, which states that
to form a smooth curve the slopes of the individual spline segments should be equal at
their meeting points (Hearn and Baker, 1996). Taking the derivative of the spline equations
the following are obtained:
C ′: y ′ = b3 + 2c3 ⋅ x + 3d3 ⋅ x 2
In this case: slope of A at P2 = slope of B at P2. If S′(Pk) denotes slope of segment S at point
Pk we have:
Rearranging:
Rearranging:
The third constraint to be obeyed is known as C2 continuity condition, which states that to
form a smooth curve the curvatures of the individual spline segments should be equal at
their meeting points (Hearn and Baker, 1996). Taking the double derivative of the spline
equations the following are obtained:
Rearranging:
Rearranging:
The last constraint to be taken into consideration pertains to end-point conditions. The
starting slope of spline A and the ending slope of spline C should also be known in order
to specify the splines unambiguously. Let the start and end-point slopes be s1 and s2,
respectively. Thus in this case, s1 = A′(P1) and s2 = C′(P4). Inserting the slope values in the
derivative equations, the following are obtained:
s1 = b1 + 2c1 ⋅ x1 + 3d1 ⋅ x1 2
(1.44)
s2 = b3 + 2c3 ⋅ x 4 + 3d3 ⋅ x 4 2
28 ◾ Fundamentals of Graphics Using MATLAB®
To find a solution to this system, all the 12 equations are plugged into the matrix form
Y = C · A:
1 x1 x1 2 x1 3 0 0 0 0 0 0 0 0
y1 a1
1 x2 x2 2 x2 3 0 0 0 0 0 0 0 0
y2 b1
0 0 0 0 1 x2 x2 2 x2 3 0 0 0 0
y2 c1
0 0 0 0 1 x3 x3 2
x3 3
0 0 0 0
y3 d1
y3 0 0 0 0 0 0 0 0 1 x3 x3 2 x3 3 a2
y4 = 0 0 0 0 0 0 0 0 1 x4 x4 2 x4 3 b2
0 c2
0 0 −1 −2 x 2 −3x 2 2 0 1 2x 2 3x 2 2 0 0 0
d2
0 0 0 0 0 0 −1 −2 x 3 −3x 3 2
0 1 2x3 3x 3 2
0 a3
0 0 −2 −6 x 2 0 0 2 6x 2 0 0 0 0
0 0 0 0 0 0 0 −2 −6 x 3 0 0 2 6x3
b3
s1 c3
0 1 2 x1 3 x1 2 0 0 0 0 0 0 0 0
s2 d3
0 0 0 0 0 0 0 0 0 1 2x 4 3x 4 2
(1.45)
Example 1.8
Find piecewise cubic equation of a curve passing through P1(0, 1), P2(2, 2),
P3(5, 0), and P4(8, 0). Slopes at first and last points are 4 and −2, respectively
Plugging the given values into the solution matrix
a1
1 1 0 0 0 0 0 0 0 0 0 0 0
b1
2 1 2 4 8 0 0 0 0 0 0 0 0
c1
2 0 0 0 0 1 2 4 8 0 0 0 0
d1
0 0 0 0 0 1 5 25 125 0 0 0 0
a2
0 0 0 0 0 0 0 0 0 1 5 25 125
0 = 0 0 0 0 0 0 0 0 1 8 64 512 b2
0 0 −1 −4 −12 0 1 4 12 0 0 0 0 c2
0 0 0 0 0 0 −1 −10 −75 0 1 10 75 d2
0 0 0 −2 −12 0 0 2 12 0 0 0 0
a3
0 0 0 0 0 0 0 −2 −30 0 0 2 30 b3
4 0 1 0 0 0 0 0 0 0 0 0 0
c3
−2 0 0 0 0 0 0 0 0 0 1 16 192
d3
Solving the coefficients and substituting into the starting equations, the required
solution is
Interpolating Splines ◾ 29
10
A
B
C
P2
y
P1
0
P3 P4
-5
0 1 2 3 4 5 6 7 8
x
A = inv(C)*Y ;
a1=A(1); b1=A(2); c1=A(3); d1=A(4);
a2=A(5); b2=A(6); c2=A(7); d2=A(8);
a3=A(9); b3=A(10); c3=A(11); d3=A(12);
fprintf('Equations for segments : \n');
yA = a1 + b1*x + c1*x^2 + d1*x^3; yA = vpa(yA, 3)
yB = a2 + b2*x + c2*x^2 + d2*x^3; yB = vpa(yB, 3)
yC = a3 + b3*x + c3*x^2 + d3*x^3; yC = vpa(yC, 3)
%plotting
xx = 0:0.1:9;
yp1 = subs(yA, x, xx);
yp2 = subs(yB, x, xx);
yp3 = subs(yC, x, xx);
plot(xx, yp1, 'k-', xx, yp2, 'k--', xx, yp3, 'k-.');
axis([0, 8, -5, 10]); grid on; hold on;
plot(X1, Y1, 'ko');
scatter(X1, Y1, 20, 'r', 'filled');
text(0.5,1,'P_1');
text(2.5,2,'P_2');
text(5.5,-0.5,'P_3');
text(7.5,0,'P_4');
legend('A', 'B', 'C'); xlabel('x'); ylabel('y');
hold off;
%verification
vrf1 = eval(subs(yA, x, x1)) %should return y1
vrf2 = eval(subs(yB, x, x2)) %should return y2
vrf3 = eval(subs(yC, x, x3)) %should return y3
NOTE
From Figure 1.12, it can be observed that A, B, and C are three different splines having differ-
ent shapes, which is expected since they have different equations. However, the continuity
conditions have constrained them in such a way that they have formed a single smooth curve
only within the interval P1 to P4. Beyond their respective intervals they have diverged out into
different trajectories.
B: x = m2 + n2t (1.46)
C: x = m3 + n3t
For spline A, at the starting point t = 0 and x = x1 and at the ending point t = 1 and x = x2.
This provides the solution:
m1 = x1
(1.47)
n1 = x 2 − x1
For spline B, at the starting point t = 0 and x = x2 and at the ending point t = 1 and x = x3.
This provides the solution:
m2 = x 2
(1.48)
n2 = x 3 − x 2
For spline C, at the starting point t = 0 and x = x3 and at the ending point t = 1 and x = x4.
This provides the solution:
m3 = x 3
(1.49)
n3 = x 3 − x 4
That was quite easily done and so the attention is now turned toward a more harder
roblem of dealing with the cubic y vs. t relations. Here, as expected, the starting relation
p
is of the form: y = a + bt + ct 2 + dt 3
32 ◾ Fundamentals of Graphics Using MATLAB®
y1 = a1
(1.53)
y 2 − y1 = b1 + c1 + d1
For spline B at the starting point t = 0 and y = y2 and at the ending point t = 1 and y = y3.
This provides the solution:
y 2 = a2
(1.54)
y3 − y 2 = b2 + c2 + d2
For spline C at the starting point t = 0 and y = y3 and at the ending point t = 1 and y = y4.
This provides the solution:
y3 = a3
(1.55)
y 4 − y3 = b3 + c3 + d3
The second constraint to be obeyed is the C1 continuity condition, which states that to
form a smooth curve the slopes of the individual spline segments should be equal at their
meeting points. In this case slope of A at point P2 should be equal to the slope of B at point
P2 i.e. A′ (at t = 1) = B′ (at t = 0). But the slopes are equal in spatial domain (physical slopes
Interpolating Splines ◾ 33
in space) and the derivative equations (as shown above) are calculated in the parametric
domain (y vs. t) so they cannot simply be equated, rather some kind of conversion from
one domain to another is first needed. For arriving at the conversion factor the chain rule
of differentiation is utilized.
By chain rule of differentiation:
dy dy dt ∆t 1− 0 1
= ⋅ = y′ ⋅ = y′ ⋅ = y′ ⋅
dx dt dx ∆x ∆x ∆x
2 2
d2 y d dy d dy dt d dy dt dt d dy dt 1
= = ⋅ = ⋅ ⋅ = ⋅ = y ′′ ⋅
2
dx dx dx
dt dx dx
dt dt
dx dx dt dt dx ∆x
(1.56)
This specifies the required conversion factor: that the derivative in spatial domain is equal
to the derivative in parametric domain multiplied by a scaling factor of (1/Δx) for a specific
curve segment. Similarly the double derivative in spatial domain is equal to the double
derivative in parametric domain multiplied by the scaling factor (1/Δx)2.
Plugging these multipliers into the C1 constraint equations:
Slope of A at P2 = slope of B at P2
A′(t = 1) = B′(t = 0) [in spatial domain]
b1 + 2c1 + 3d1 b2
=
x 2 − x1 x3 − x 2
Rearranging:
( x 3 − x 2 ) ⋅ b1 + 2 ( x 3 − x 2 ) ⋅ c1 + 3( x 3 − x 2 ) ⋅ d1 − ( x 2 − x1 ) ⋅ b2 = 0 (1.57)
Slope of B at P3 = slope of C at P3
B′(t = 1) = C′(t = 0) [in spatial domain]
b2 + 2c2 + 3d2 b3
=
x3 − x 2 x 4 − x3
Rearranging:
( x 3 − x 2 )2 ⋅ c1 + ( x 3 − x 2 )2 ⋅ 3d1 − ( x 2 − x1 )2 ⋅ c2 = 0 (1.59)
Curvature of B at P3 = curvature of C at P3
B″(t = 1) = C″(t = 0) [in spatial domain]
( x 4 − x 3 )2 ⋅ c2 + ( x 4 − x 3 )2 ⋅ 3d2 − ( x 3 − x 2 )2 ⋅ c3 = 0 (1.60)
The domain conversion factors are also required for the end-point conditions.
Let s1 be the slope at start point of segment A and s2 be the end-point slope of segment
C. Then:
b1
s1 = A′(t = 0): s1 =
( x 2 − x1 )
b1 = s1 ⋅ ( x 2 − x1 ) (1.61)
b3 + 2c3 + 3d3
s2 = C ′(t = 1): s2 =
( x 4 − x3 )
b3 + 2c3 + 3d3 = s2 ⋅ ( x 4 − x 3 ) (1.62)
Example 1.9
Find piecewise parametric cubic equation of a curve passing through
P1(0, 1), P2(2, 2), P3(5, 0), and P4(8, 0). Slopes at first and last points
are 2 and 1, respectively. Assume linear relation between t and x.
Since x − t relations are linear, let the starting equations are of the form: x = m + nt .
Equations for the separate spines:
A: x = m1 + n1t
B: x = m2 + n2t
C: x = m3 + n3t
Substituting given points into the above equations and solving for the unknown
coefficients:
m1 = x1 = 0, n1 = x 2 − x1 = 2, m2 = x 2 = 2, n2 = x 3 − x 2 = 3, m3 = x 3 = 5, n3 = x 4 − x 3 = 3
B: x = 2 + 3t
C: x = 5 + 3t
Since y − t relations are cubic, let the starting equations are of the form:
y = a + b ⋅ t + c ⋅ t 2 + d ⋅ t 3.
Equations for the separate splines:
b1
1 1 1 1 0 0 0 0 0 0
−2 0 0 0 1 1 1 0 0 0 c1
d1
0 0 0 0 0 0 0 1 1 1
0 3 6 9 −2 0 0 0 0 0 b2
0 = 0 0 0 3 6 9 −3 0 0 c2
0 0 9 27 0 −4 0 0 0 0 d2
0 0 0 0 0 9 27 0 −9 0
b3
4 1 0 0 0 0 0 0 0 0 c3
3 0 0 0 0 0 9 1 2 3 d3
Interpolating Splines ◾ 37
y
0.5 0.5
P3 P4
2 0 0
-0.5 -0.5
0 -1 -1
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 0 2 4 6 8
t t x
C=[ 1, 1, 1, 0, 0, 0, 0, 0, 0;
0, 0, 0, 1, 1, 1, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 1, 1;
(x3-x2), 2*(x3-x2), 3*(x3-x2), -(x2-x1), 0, 0, 0, 0, 0;
0, 0, 0, (x4-x3), 2*(x4-x3), 3*(x4-x3), -(x3-x2), 0, 0;
0, (x3-x2)^2, 3*(x3-x2)^2, 0, -(x2-x1)^2, 0, 0, 0, 0;
0, 0, 0, 0, (x4-x3)^2, 3*(x4-x3)^2, 0, -(x3-x2)^2, 0;
1, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 2, 3
];
A = inv(C)*G';
38 ◾ Fundamentals of Graphics Using MATLAB®
xA = x1 + (x2-x1)*t; xA = vpa(xA)
yA = aA + bA*t + cA*t^2 + dA*t^3; yA = vpa(yA, 3)
xB = x2 + (x3-x2)*t; xB = vpa(xB)
yB = aB + bB*t + cB*t^2 + dB*t^3; yB = vpa(yB, 3)
xC = x3 + (x4-x3)*t; xC = vpa(xC)
yC = aC + bC*t + cC*t^2 + dC*t^3; yC = vpa(yC, 3)
%plotting
tt = linspace(0,1);
xa = subs(xA, t, tt);
ya = subs(yA, t, tt);
xb = subs(xB, t, tt);
yb = subs(yB, t, tt);
xc = subs(xC, t, tt);
yc = subs(yC, t, tt);
text(0.5,1,'P_1');
text(2.5,2,'P_2');
text(5.5,0,'P_3');
text(7,0,'P_4');
legend('A', 'B', 'C');
xlabel('x'); ylabel('y'); title('x - y'); axis square;
hold off;
NOTE
The importance of the end-point conditions can be observed here. Compare Examples 1.8
and 1.9. Even though the CPs remain the same in both cases, the shape of the piecewise
curve has changed, as shown in Figures 1.12 and 1.13, due to changes in end-point slopes
only.
Interpolating Splines ◾ 39
1.10 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
1.11 REVIEW QUESTIONS
1.
What is meant by “spline”? From where has the word originated?
2. What are the different types of splines possible?
3. What is meant by polynomial equation?
4. Differentiate between the standard form and parametric form of representation.
5. What is a constraint matrix, coefficient matrix, geometry matrix, and basis matrix?
6. Explain the difference between the notations P0 and P(0).
7. What is a linear spline and how is it represented using a polynomial?
40 ◾ Fundamentals of Graphics Using MATLAB®
1.12 PRACTICE PROBLEMS
1. Find the equation of a linear spline passing through (−3, −3) and having slope −3.
2. Find equation of a quadratic curve passing through (0, 0), (π/2, 1), and (π, 0).
3. Find the equation of a quadratic curve through (0, 2), (−2, 0), and (2, −2) in parametric
form with k = 0.4.
4. Derive parametric equations of a quadratic curve, which goes through three points
(−2, 1), (−1, 2), and (2, −1) in such a way that the middle point divides the curve in the
ratio 1:2.
5. Find the equation of a cubic interpolating spline through the points (3, 2), (8, −4),
(6, 5), and (1, 0).
6. Find the equation of a cubic spline passing through four points (3, 2), (8, −4), (6, 5),
and (1, 0), in parametric form, with sub-division ratios m = 0.1 and n = 0.7.
7. Find piecewise cubic equation of a curve passing through (−5, −2), (−1, −1), (5, 0), and
(7, −2). Slopes at first and last points are 1 and 1, respectively.
8. Find the equation of a quadratic curve in the form y = f ( x ), which passes through the
three points (k , − k ), (0, k ), and (− k , 0), where k is a constant.
9. For what value(s) of k will two curve segments having equations y = k + 2k ⋅ x − 3k ⋅ x 2
and y = 3k − 2k ⋅ x + k ⋅ x 2 satisfy the C1 continuity condition at point P(k, −k), where k
is a constant?
10. Find piecewise parametric cubic equation of a curve passing through (1, −2), (2, −3),
(3, −4), and (4, −5). Slopes at first and last points are 5 and −6, respectively. Assume
x vs. t relations to be linear.
Chapter 2
Blending Functions
and Hybrid Splines
2.1 INTRODUCTION
In the previous chapter, we have discussed how equations of interpolating splines can be
derived by substituting the coordinate information into the starting equatisons. Apart from
interpolating splines, there are other types of splines, which do not always pass through
the CPs or all the CPs may not be known or conditions other than CPs need to be used for
deriving their equations. For such splines, the techniques previously discussed would not
be applicable and a new set of techniques need to be devised. The newer techniques are
designed in such a way so that spline equations can be made independent of the coordi-
nates of the CPs. Such techniques are applicable to hybrid splines, which only pass through
a subset of the CPs, and approximating splines, which in general do not pass through any
of its CPs. This has led to the concept of blending functions (BFs), which provides us with
ways of determining where a spline is located in the neighborhood of a CP. In the following
sections, the concept of BFs is explained and then applied to both interpolating and non-
interpolating splines. The latter part of this chapter lays the foundation of hybrid splines.
Hybrid splines are those which pass through few CPs but not through others or boundary
conditions other than CPs are used to derive their equations. To fully define hybrid splines
therefore, some additional constraints are often specified, for example slope of the curve
at a point. Four types of hybrid splines are discussed, namely, Hermite spline, Cardinal
spline, Catmull–Rom (C–R) spline, and Bezier spline. In each case, we discuss mostly the
cubic curves in parametric form, as they are most often used in graphics and moreover, the
ideas discussed can be extended to other degree of curves also.
2.2 BLENDING FUNCTIONS
The concept of “BFs” was proposed as a means for calculating equations of curves, which
are non-interpolating i.e. they do not pass through some or all of the CPs (Hearn and
Baker, 1996). For interpolating curves, the coordinates of the CPs are substituted in the
41
42 ◾ Fundamentals of Graphics Using MATLAB®
starting equations to solve for the unknown coefficients. However, if the curve does not
pass through the CPs then this method does not work and a new approach is required to
determine the trajectory of a point on the curve with respect to the CPs.
Consider a spline curve whose CPs are at locations P0, P1, P2, and P3. In general, none of
the CPs are actually located on the curve but would be around it somewhere in the neigh-
borhood (see Figure 2.1). Let us assume that four masses L0, L1, L2, and L3 are located on the
CPs P0, P1, P2, and P3, which exert gravitational pull on the spline. Then the center of mass
is located at P given by
L0 P0 + L1P1 + L2 P2 + L3 P3
P= (2.1)
L0 + L1 + L2 + L3
Now, consider that the masses are not constant but change their values as a function of the
parametric variable t(0 ≤ t ≤ 1) i.e. Li = f(t). Then, the center of mass P will also shift to dif-
ferent points. If all these points are joined together as t takes on various values from 0 to
1, then the locus of P would indicate the actual spline we are interested in. The functions
Li = f(t) are known as BFs. It is obvious that the total number of BFs would be equal to the
total number of masses which in turn would be equal to the total number of CPs. Thus for
a cubic curve there are in general four BFs.
To reduce complexity of computations, an additional constraint is applied: ΣLi = 1
i.e. L0 + L1 + L2 + L3 = 1. This reduces the denominator of the above equation to 1 and gives us
P = L0 P0 + L1P1 + L2 P2 + L3 P3 (2.2)
1.5
P2 (L2)
0.5 P3 (L3)
P
0
-0.5
P0 (L0) P1 (L1)
-1
-1.5
-1.5 -1 -0.5 0 0.5 1 1.5
It might be obvious to reflect that larger the mass at a CP, more will be its gravitational pull
on the spline and nearer will the spline be drawn towards that point. Writing the above in
matrix form:
P0
P1
P = L0 P0 + L1P1 + L2 P2 + L3 P3 = L0 L1 L2 L3 = L ⋅ G (2.3)
P2
P3
The matrix L provides us with a characterization of any point P on the spline independent
of the CPs located in matrix G. Matrix L is referred to as the “BF matrix” while the individ-
ual elements L0, L1, L2, and L3 are the BFs (sometimes also referred to as “basis functions”).
The matrix G is the geometry matrix as it defines the locations of the CPs, which determine
the geometry of the curve.
From the previous chapter, we know that in general for a parametric cubic curve:
a
b
P(t ) = a + bt + ct 2 + dt 3 = 1 t t2 t3 = T ⋅ A = T ⋅ ( B ⋅ G ) = (T ⋅ B) ⋅ G = L ⋅ G
c
d
(2.4)
where A is the coefficient matrix, B is the basis matrix, T is the parametric matrix, G is
the geometry matrix, and L is the BF matrix. The above expression shows us that L = T·B
i.e. the BF matrix L is nothing but the product of the parametric matrix T and the basis
matrix B.
Below we summarize all the relevant relations, which will be required for solving
numerical problems:
G =C⋅A
B = inv (C )
A = B ⋅G
P = L ⋅G (2.5)
L = T ⋅B
P =T ⋅A
P = T ⋅ B ⋅G
44 ◾ Fundamentals of Graphics Using MATLAB®
Example 2.1
A cubic curve has the following BFs, where a, b, and c are constants:
L0 = −4at , L1 = at 3 , L2 = bt 2 , L3 = −c . Find its basis matrix.
From Equation (2.4), L = T·B
For a cubic curve, L = L0 L1 L2 L3 = 1 t t 2 t 3 ⋅ B
x11 x12 x13 x14
x 21 x 22 x 23 x 24
Let B =
x 31 x 32 x 33 x 34
x 41 x 42 x 43 x 44
From inspection, x11 = 0, x 21 = −4a, x 31 = 0, x 41 = 0
Similarly, the other terms are obtained in a likewise manner and compiled together
to form the basis matrix:
0 0 0 −c
−4a 0 0 0
B=
0 0 b 0
0 a 0 0
Verification: The correctness of the basis matrix could be verified by computing the
product T·B, which would give us the given L matrix back.
MATLAB® Code 2.1
clear; clc;
syms a b c t;
T = [1, t, t^2, t^3];
L01 = 0; L02 = -4*a*t; L03 = 0; L04 = 0;
L11 = 0; L12 = 0; L13 = 0; L14 = a*t^3;
L21 = 0; L22 = 0; L23 = b*t^2; L24 = 0;
L31 = -c; L32 = 0; L33 = 0; L34 = 0;
% Let B = [x11 x12 x13 x14 ; x21 x22 x23 x24 ; x31 x32 x33 x34 ; x41 x42 x43 x44]
x11 = L01 / T(1);
x21 = L02 / T(2);
x31 = L03 / T(3);
x41 = L04 / T(4);
x12 = L11 / T(1);
x22 = L12 / T(2);
x32 = L13 / T(3);
x42 = L14 / T(4);
x13 = L21 / T(1);
x23 = L22 / T(2);
x33 = L23 / T(3);
x43 = L24 / T(4);
x14 = L31 / T(1);
x24 = L32 / T(2);
x34 = L33 / T(3);
x44 = L34 / T(4);
B = [x11 x12 x13 x14 ; x21 x22 x23 x24 ; x31 x32 x33 x34 ; x41 x42 x43 x44]
% verification
Blending Functions and Hybrid Splines ◾ 45
fprintf('Verification : L = T*B\n');
T*B
46 ◾ Fundamentals of Graphics Using MATLAB®
−1
1 0 1 0
B= =
1 1 −1 1
1 0
L(t ) = T ⋅ B = 1 t = 1− t, t (2.6)
−1 1
L0 = 1 – t
(2.7)
L1 = t
By varying t over all possible values from 0 to 1, we can plot the above BFs as shown in
Figure 2.2.
1
L0
0.9 L1
0.8
0.7
0.6
0.5
L
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
One observation which can immediately be made from the graph is that the constraint
∑ Li = 1 holds good i.e. for any value of t the sum of the values of L0 and L1 always equals 1.
That is expected since L0 + L1 = = (1 − t) + t = 1
The following important points can now be inferred from the above discussion:
• The BFs depict how the masses at the CPs change with varying values of t
• For each value of t, sum of the BF values is always equal to 1
• Although BFs are functions of t, at each CP t has a specific value and so all BFs become
scalar constants
For quadratic interpolating splines, we have seen in Section 1.5 the following, where k is
the sub-division ratio
−1
1 0 0
B= 1 k k2
1 1 1
This gives us:
−1
1 0 0
L(t ) = T ⋅ B = 1 t t2 1 k k2
(2.8)
1 1 1
Example 2.2
Find the BFs of a quadratic spline having sub-division ratio k = 0.8.
From Equation (2.8)
−1
1 0 0 1 0 0
2 =1
L(t ) = 1 t t 1 0.8 0.64 t t −2.25
2
6.25 −4
1 1 1 1.25 −6.25 5
Simplifying:
L0 = 1 − 2.25t + 1.25t 2
L1 = 6.25t − 6.25t 2
L2 = −4t + 5t 2
48 ◾ Fundamentals of Graphics Using MATLAB®
NOTE
Two methods for plotting the BFs are shown in the code above. In the first method shown in
Figure 2.3a each BF is plotted individually. In the second method shown in Figure 2.3b, all
the three BFs are plotted together. The second plot indicates how the BFs are related to each
other. For example, it shows that only one BF is non-zero at each of the CPs i.e. at t = 0, at
t = 0.8 and at t = 1.
For cubic interpolating splines, we have seen in Section 1.7 the following, where m and
n are the sub-division ratios
−1
1 0 0 0
1 m m2 m3
B=
1 n n2 n3
1 1 1 1
Blending Functions and Hybrid Splines ◾ 49
−1
1 0 0 0
1 m m2 m3
L(t ) = T ⋅ B = 1 t t2 (2.9)
1 n n2 n3
1 1 1 1
1
0.5
0.5
0 0
0 0.5 1 0 0.5 1
t t
5 t2 - 4 t
1
0.5
-0.5
0 0.5 1
t
(b)
2
L0
L1
1.5 L2
0.5
L
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
Example 2.3
Find the BFs of a cubic spline having sub-division ratios m = 0.1 and n = 0.7.
Hence, show that at each CP only one component of the BFs is non-zero.
From Equation (2.9)
−1
1 0 0 0
1 0.1 0.01 0.001
L(t ) = 1 t t 2
1 0.7 0.49 0.343
1 1 1 1
1 0 0 0
−12.4286 12.963 −0.7937 0.2593
= 1 t t 2
25.7143 −31.4815 8.7302 −2.963
−14.2857 18.5185 −7.9365 3.7037
At CP 1: Putting t = 0, L0 = 1, L1 = L2 = L3 = 0
At CP 2: Putting t = 0.1, L1 = 1, L0 = L2 = L3 = 0
At CP 3: Putting t = 0.7, L2 = 1, L0 = L1 = L3 = 0
At CP 4: Putting t = 1, L3 = 1, L0 = L1 = L2 = 0 (Figure 2.4)
%plotting
%Method-1
Blending Functions and Hybrid Splines ◾ 51
(a)
(180 t2)/7 - (87 t)/7 - (100 t3)/7 + 1 (350 t)/27 - (850 t2)/27 + (500 t3)/27
1
1.5
0.5 1
0 0.5
-0.5 0
-0.5
0 0.5 1 0 0.5 1
t t
(550 t2)/63 - (50 t)/63 - (500 t3)/63 (7 t)/27 - (80 t2)/27 + (100 t3)/27
1
0.8
0.6
0.5 0.4
0.2
0
0
-0.2
0 0.5 1 0 0.5 1
t t
(b)
2
L0
L1
1.5 L2
L3
0.5
L
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
figure,
subplot(221), ezplot(L(1), [0,1]);
subplot(222), ezplot(L(2), [0,1]);
subplot(223), ezplot(L(3), [0,1]);
subplot(224), ezplot(L(4), [0,1]);
%Method-2
figure,
tt = 0:.01:1;
L0 = subs(L(1), t, tt);
L1 = subs(L(2), t, tt);
L2 = subs(L(3), t, tt);
L3 = subs(L(4), t, tt);
plot(tt, L0, 'k-', tt, L1, 'k--', tt, L2, 'k-.', tt, L3, 'k:');
grid;
xlabel('t');
ylabel('L');
legend('L0', 'L1', 'L2', 'L3');
The above discussions pave the way for an important conclusion. In general, an approxi-
mating spline will not go through any of its CPs, so the location of any point on the curve
with respect to the position of the CPs, is determined by the net gravitational pull of all
masses together (i.e. the center of mass). However, for interpolating splines the curve
actually goes through all of its CPs one after another. Analyzing this behavior from the
viewpoint of BFs, leads us to conclude that at each point where the curve actually goes
through a CP, the mass at that point must be so great as to nullify the effects of all other
masses and force the center of mass to shift to that particular CP. We can therefore say
that an interpolating spline can be considered as a special case of an approximating spline
where each mass is large enough to nullify the effects of other masses and force the locus of
the spline to go through a particular CP. This fact is also evident from the plots of the BFs
above. At each CP only a specific BF has value equal to 1 while all the others are reduced
to 0. This tells us where the CPs are located by noting where the BF graph has a single 1 and
all other zeros. For example for the cubic curve above, the first CP is at t = 0, the second CP
is at t = 0.1, the third CP is at t = 0.7 and the fourth CP is at t = 1.
2.4 HERMITE SPLINE
A hybrid spline differs from an interpolating spline in that it passes through only a subset
of its CPs and hence requires some additional information for its unique characterization.
The first hybrid spline that we are going to study is the Hermite spline, named after French
Blending Functions and Hybrid Splines ◾ 53
3.5
T(1)
3
P(1)
2.5
2
y
1.5
T(0)
1
P(0)
0.5
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x
mathematician Charles Hermite (Hearn and Baker, 1996). For a cubic Hermite spline only
the start point P(0) i.e. P(t) at t = 0, and the end point P(1) i.e. P(t) at t = 1, is known. To be
uniquely specified two additional pieces of information are also given: the tangent slopes at
the start point and end points i.e. T (0) = P ′(0) and T (1) = P ′(1) (see Figure 2.5).
To find the equation of a cubic Hermite spline, we start with the general equation of a
cubic spline:
a
b
P(t ) = a + bt + ct 2 + dt 3 = 1 t t2 t3
c
d
Substituting the given conditions in both the starting equation and derivative equation:
P(0) = a = P0 (say)
P(1) = a + b + c + d = P1 (say)
(2.11)
P ′(0) = b
P ′(1) = b + 2c + 3d
54 ◾ Fundamentals of Graphics Using MATLAB®
P0 = a
P1 = a + b + c + d (2.12)
P ′ (0) = b
P ′ (1) = b + 2c + 3d
P0 1 0 0 0 a
P1 = 1 1 1 1 b (2.13)
P ′(0) 0 1 0 0 c
P ′(1) 0 1 2 3 d
Solving for A:
a 1 0 0 0 P0
P1
b = 0 0 1 0
(2.14)
c −3 3 −2 −1 P ′(0)
d 2 −2 1 1 P ′(1)
1 0 0 0 P0
P1
P(t ) = T ⋅ B ⋅ G = 1 t t2 t3 0 0 1 0
−3 3 −2 −1 P ′(0) (2.15)
2 −2 1 1 P ′(1)
1 0 0 0
L(t ) = T ⋅ B = 1 t t2 t3 0 0 1 0 (2.16)
−3 3 −2 −1
2 −2 1 1
L0 = 1 − 3t 2 + 2t 3
L1 = 3t 2 − 2t 3 (2.17)
2 3
L2 = t − 2t + t
L3 = −t 2 + t 3
Blending Functions and Hybrid Splines ◾ 55
L0
0.8 L1
L2
L3
0.6
0.4
L
0.2
-0.2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
Figure 2.6 shows the plot of the four BFs. One difference with the BFs of a cubic interpolat-
ing spline shown in Figure 2.4 can be observed. While for an interpolating spline, the BFs
have four points for which only one is non-zero, Figure 2.6 shows that in this case there are
only two such points (start and end points) for which this condition is true. This is a reflec-
tion of the fact that a hybrid spline passes through a subset of its CPs viz. only the first and
last points and not through the intermediate points.
Example 2.4
Find the parametric equation of a cubic Hermite spline through points (1, 1)
and (4, 3) and having parametric slopes (3, 6) and (1, −1) at these points.
Here, P0 = (1, 1), P1 = (4, 3), P ′(0) = (3, 6), P ′(1) = (1, − 1).
From Equation (2.14)
a 1 0 0 0 1 1 1 1
b 4 3 3 6
= 0 0 1 0 =
c −3 3 −2 −1 3 6 2 −5
d 2 −2 1 1 1 −1 −2 1
x (t ) = 1 + 3t + 2t 2 − 2t 3
y(t ) = 1 + 6t − 5t 2 + t 3
56 ◾ Fundamentals of Graphics Using MATLAB®
3.5 3
3
3 P1
2.5
2.5 2
x
y
2
2
1
1.5 1.5 P0
1 1 0
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 0 1 2 3 4 5
t t x
P0 = [1, 1];
P1 = [4, 3];
T0 = [3, 6];
T1 = [1, -1];
C = [1 0 0 0; 1 1 1 1; 0 1 0 0; 0 1 2 3];
B = inv(C);
X = [P0(1), P1(1)];
Y = [P0(2), P1(2)];
G = [P0; P1; T0; T1];
A = B*G;
T = [1 t t^2 t^3];
P = T*A;
fprintf('Required equations :\n');
x = P(1)
y = P(2)
%plotting
tt = 0:.01:1;
xx = subs(P(1), t, tt);
yy = subs(P(2), t, tt);
subplot(131), plot(tt, xx);
xlabel('t'); ylabel('x'); axis square; title('t - x');
subplot(132), plot(tt, yy);
xlabel('t'); ylabel('y'); axis square; title('t - y');
Blending Functions and Hybrid Splines ◾ 57
NOTE
colormap: specifies a color scheme using predefined color look-up tables
2.5 CARDINAL SPLINE
A Cardinal spline is similar to a Hermite spline in that it actually passes through two
given points P1 and P2, and has known gradients at these points, but unlike a Hermite
spline, these gradients are not explicitly given. Rather two additional points are given, a
previous point P0 and a subsequent point P3, and the gradients at P1 and P2 are expressed as
scalar multiples of the gradients of the line joining P0 with P2, and P1 with P3, respectively
(Hearn and Baker, 1996) (see Figure 2.8).
The line vector connecting P0 and P2 is given by the differences of their position vectors i.e.
( P2 − P0 ). The gradient at P1 is a scalar multiple of this line vector i.e. P1′ = s ( P2 − P0 ), where s is
referred to as the “shape parameter.” Likewise, the line vector connecting P1 and P3 is given
by the differences of their position vectors i.e. ( P3 − P1 ). The gradient at P2 is a scalar multiple
3 P2
1
P1
-1
P0
-2
P3
-3
0 1 2 3 4 5 6
of this line vector i.e. P2′ = s ( P3 − P1 ). The value of s typically ranges from 0 to 1. Smaller values
of s produce shorter and tighter curves, larger values of s produce longer and looser curves.
If s > 1, the curve crosses over to the other size of the tangent line (see Figure 2.9).
To derive equation of a cubic Cardinal spline, we start with a general third-degree
parametric equation
a
b
P(t ) = a + bt + ct 2 + dt 3 = 1 t t2 t3
c
d
P(0) = a = P1
P(1) = a + b + c + d = P2
(2.18)
P ′(0) = b = P ′(1) = s ( P2 − P0 )
P ′(1) = b + 2c + 3d = P ′(2) = s ( P3 − P1 )
1
P0 = a + 1 − b + c + d
s
1 2 3
P3 = a + b + c + d
s s s
Blending Functions and Hybrid Splines ◾ 59
Solving for A:
a 0 1 0 0 P0
b −s 0 s 0 P1
= (2.20)
c 2s s−3 3 − 2s −s P2
d −s 2−s s−2 s P3
0 1 0 0 P0
P1
P(t ) = T ⋅ B ⋅ G = 1 t t2 t3 −s 0 s 0 (2.21)
2s s−3 3 − 2s −s P2
−s 2−s s−2 s P3
0 1 0 0
L(t ) = T ⋅ B = 1 t t2 t3 −s 0 s 0 (2.22)
2s s−3 3 − 2s −s
−s 2−s s−2 s
L0 = − s ⋅ t + 2s ⋅ t 2 − s ⋅ t 3
L1 = 1 + (s − 3) ⋅ t 2 + (2 − s ) ⋅ t 3
(2.23)
L2 = s ⋅ t + (3 − 2s ) ⋅ t 2 + (s − 2) ⋅ t 3
L3 = − s ⋅ t 2 + s ⋅ t 3
Example 2.5
Find the BFs of a cubic Cardinal spline with shape parameter s = 1.5 and generate a
plot to visualize them.
60 ◾ Fundamentals of Graphics Using MATLAB®
0.8 L0
L1
L2
0.6 L3
0.4
L
0.2
-0.2
-0.4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
( )
L0 = − 3t 3 2 + 3t 2 − (3t )/2
L = t 2 − ( 3t ) 2 + 1
1
3 2
3
L2 = − t 2 + (3t )/2
( )
L3 = 3t 3 2 − 3t 2 2 ( )
MATLAB Code 2.5
clear all; clc;
syms t;
s = 1.5;
B = [0 1 0 0 ; -s 0 s 0 ; 2*s s-3 3-2*s -s ; -s 2-s s-2 s];
T = [1 t t^2 t^3];
L = T*B;
fprintf('Blending functions are :\n');
disp(L(1)), disp(L(2)), disp(L(3)), disp(L(4));
%plotting
tt = 0:.01:1;
L0 = subs(L(1), t, tt);
L1 = subs(L(2), t, tt);
L2 = subs(L(3), t, tt);
L3 = subs(L(4), t, tt);
Blending Functions and Hybrid Splines ◾ 61
plot(tt, L0, 'k-', tt, L1, 'k--', tt, L2, 'k-.', tt, L3, 'k:');
xlabel('t'); ylabel('L'); grid;
legend('L0', 'L1', 'L2', 'L3');
2.6 CATMULL–ROM SPLINE
A Cardinal spline with value of the shape parameter s = 0.5 is referred to as C–R spline
named after the American s cientist Edwin Catmull and Israeli scientist Raphael Rom.
The BFs and basis matrix are computed by substituting s = 0.5 in Equation (2.22) to obtain
the following:
0 1 0 0
−0.5 0 0.5 0
B= (2.24)
1 −2.5 2 −0.5
−0.5 1.5 −1.5 0.5
L0 = −0.5t + t 2 − 0.5t 3
L1 = 1 − 2.5t 2 + 1.5t 3
(2.25)
L2 = 0.5t + 2t 2 − 1.5t 3
L3 = −0.5t 2 + 0.5t 3
L0
0.8 L1
L2
L3
0.6
0.4
L
0.2
-0.2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
3.5 3
3 P2
2
3
2.5 1
2.5 P1
x
y
2 0
2
-1
1.5 P0 P3
1.5 -2
1 1 -3
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 -1 0 1 2 3 4 5 6
t t x
Example 2.6
Find the parametric equation of a cubic C–R spline associated with the CPs (0, −1),
(1, 1), (4, 3), and (5, −2).
From Equations (2.21) and (2.24), substituting the given values
0 1 0 0 0 −1
P(t ) = 1 t t2 t3 −0.5 0 0.5 0 1 1
1 −2.5 2 −0.5 4 3
−0.5 1.5 −1.5 0.5 5 −2
x (t ) = −2t 3 + 3t 2 + 2t + 1
y(t ) = −3.5t 3 + 3.5t 2 + 2t + 1
%plotting
tt = linspace(0, 1);
xx = subs(x, t, tt);
yy = subs(y, t, tt);
subplot(131), plot(tt,xx); title('t - x');
xlabel('t'); ylabel('x'); axis square;
subplot(132), plot(tt,yy); title('t - y');
xlabel('t'); ylabel('y'); axis square;
subplot(133), plot(xx, yy, 'b-', 'LineWidth', 1.5);
xlabel('x'); ylabel('y'); axis square; title('x - y');
grid; hold on;
scatter(X, Y, 20, 'r', 'filled');
axis([-1 6 -3 4]);
text(P0(1), P0(2)-1, 'P_0');
text(P1(1), P1(2)-1, 'P_1');
text(P2(1), P2(2)-1, 'P_2');
text(P3(1), P3(2)+1, 'P_3');
hold off;
2.7 BEZIER SPLINE
Bezier spline, named after French engineer Pierre Bezier, is a popular type of hybrid splines,
which satisfies the following characteristics: (a) it passes through the first and last CPs,
(b) line joining the first CP and the second CP is tangential to the curve, (c) line joining
the last CP and the previous CP is tangential to the curve, and (d) the curve is contained
entirely within the convex hull formed by joining all the CPs in sequence (see Figure 2.13).
To satisfy the above conditions the BFs of a Bezier spline is generated by Bernstein
polynomials (Hearn and Baker, 1996), named after the Russian mathematician Sergei
Bernstein. Bernstein polynomials of degree n is shown below, where n Ck denotes the
combination of k items out of n and 0 ≤ t ≤ 1.
quadratic cubic
4 4
P1 P1
3 3
2
2
1 P2
1
y
P2
0
0
-1
P0
-1 -2
P0
P3
-2 -3
0 1 2 3 4 5 0 1 2 3 4 5 6
x x
Lk = nCk ⋅ (1 − t )n − k ⋅ t k
n! (2.26)
n
Ck =
k !(n − k )!
n n
P(t ) = ∑
k=0
Lk Pk = ∑ C ⋅(1 − t )
k=0
n
k
n− k
⋅ t k ⋅ Pk (2.27)
A quadratic Bezier curve is of degree 2 and associated with three CPs P0, P1, and P2.
Equation of the curve is derived by putting n = 2 in the Bernstein polynomial
2 2
P(t ) = ∑L P = ∑ C ⋅(1 − t )
k=0
k k
k=0
2
k
2− k
⋅ t k ⋅ Pk (2.28)
Expanding:
L0 = (1 − t )2 = 1 − 2t + t 2
L1 = 2(1 − t ) ⋅ t = 2t − 2t 2 (2.30)
L2 = t 2
1 0 0
B = −2 2 0 (2.31)
1 −2 1
1 0 0 P0
P(t ) = 1 t t 2 −2 2 0 P
1 (2.32)
1 −2 1 P2
Blending Functions and Hybrid Splines ◾ 65
1
L0
0.9 L1
L2
0.8
0.7
0.6
0.5
L
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
Example 2.7
Find parametric equations of a quadratic Bezier spline with three CPs P0(1, −1),
P1(4, 3), and P2(5, −2).
From Equation (2.32), substituting given conditions
1 0 0 1 −1
P(t ) = 1 t t 2 −2 2 0 4 3
1 −2 1 5 −2
x (t ) = 1 + 6t – 2t 2
y(t ) = −1 + 8t – 9t 2
y
0
-1
2 -1
P0
-1.5 -2
P2
1 -2 -3
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 0 2 4 6
t t x
T = [1 t t^2 ];
B = [1 0 0 ; -2 2 0 ; 1 -2 1];
G = [P0; P1; P2];
P = T*B*G;
fprintf('Required equations :\n');
x = P(1)
y = P(2)
%plotting
tt = linspace(0,1);
xx = subs(x, t, tt);
yy = subs(y, t, tt);
subplot(131), plot(tt, xx); title('t - x');
xlabel('t'); ylabel('x'); axis square;
subplot(132), plot(tt, yy); title('t - y');
xlabel('t'); ylabel('y'); axis square;
subplot(133), plot(xx, yy, 'b-', 'LineWidth', 1.5);
title('x - y');
hold on;
scatter(X, Y, 20, 'r', 'filled');
xlabel('x'); ylabel('y'); axis square;
grid on; d = 0.5;
text(P0(1), P0(2)-d, 'P_0');
text(P1(1)+d, P1(2), 'P_1');
text(P2(1), P2(2)-d, 'P_2');
axis([-1 6 -3 4]);
hold off;
A cubic Bezier curve is of degree 3 and associated with four CPs P0, P1, P2, and P3. Equation
of the curve is derived by putting n = 3 in the Bernstein polynomial (Foley et al., 1995;
Shirley, 2002)
3 3
P(t ) = ∑L P = ∑ C ⋅(1 − t )
k=0
k k
k=0
3
k
3− k
⋅ t k ⋅ Pk (2.33)
Blending Functions and Hybrid Splines ◾ 67
Expanding:
P(t ) = (1 − t )3 ⋅ P0 + 3 ⋅ (1 − t )2 ⋅ t ⋅ P1 + 3 ⋅ (1 − t ) ⋅ t 2 ⋅ P2 + t 3 ⋅ P3 (2.34)
L0 = (1 − t )3 = 1 − 3t + 3t 2 − t 3
L1 = 3(1 − t )2 ⋅ t = 3t − 6t 2 + 3t 3
(2.35)
2 2 3
L2 = 3(1 − t ) ⋅ t = 3t − 3t
L3 = t 3
A plot of the BFs is shown below in Figure 2.16.
Since L = T·B, the basis matrix can also be computed trivially:
1 0 0 0
−3 3 0 0
B= (2.36)
3 −6 3 0
−1 3 −3 1
Equation of the curve can be expressed in P = T·B·G form
1 0 0 0 P0
−3 3 0 0 P1
P(t ) = 1 t t2 t3 (2.37)
3 −6 3 0 P2
−1 3 −3 1 P3
1
L0
0.9 L1
L2
0.8 L3
0.7
0.6
0.5
L
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
1 3
P2
4 0.5 2
P1
0 1
3
x
y
-0.5 0
-1 -1
2 P0
-1.5 -2
P3
1 -2 -3
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 0 2 4 6
t t x
Example 2.8
Find parametric equations of a cubic Bezier spline associated with four CPs.
P0(1, −1), P1(1, 1), P2(4, 3), P3(5, −2).
From Equation (2.37) substituting given conditions
1 0 0 0 1 −1
(t ) = 1 t t2 t3 −3 3 0 0 1 1
3 −6 3 0 4 3
−1 3 −3 1 5 −2
Separating out the components:
x (t ) = 1 + 9t 2 − 5t 3
y(t ) = −1 + 6t − 7t 3
%plotting
tt = linspace(0,1);
Blending Functions and Hybrid Splines ◾ 69
xx = subs(x, t, tt);
yy = subs(y, t, tt);
2.8 SPLINE CONVERSIONS
Before ending this chapter, we take a look at conversions of one spline form to another.
A spline of one type can be expressed as a spline of another type. Since the spline physically
remains the same, its equation is unaltered. However, the basis matrix and CPs change
(Hearn and Baker, 1996).
Suppose a spline of type 1 specified by its basis matrix B1 and CPs in geometry matrix G1
needs to be converted to a spline of type 2 with basis matrix B2 and geometry matrix G1. Then
equation of the first spline is P1(t) = T·B1·G1 and equation of the second spline is P2(t) = T·B2·G2.
Note, however, that the actual spline physically remains the same and it is only represented
in two different manners. The two equations are therefore equivalent, and can be equated:
P(t ) = T ⋅ B1 ⋅ G1 = T ⋅ B2 ⋅ G2 (2.38)
Basis matrices of both splines are known as they are characteristic of the spline type. CPs
of the first spline should also be known. CPs of the second spline are found out by solving
the following:
G2 = B2−1 ⋅ B1 ⋅ G1 (2.39)
Even if for the first curve the CPs are not specifically known but the curve is expressed in
{( )( )}
the parametric form {x (t ), y(t )} = a0 + a1 ⋅ t + a2 ⋅ t 2 + a3 ⋅ t 3 , b0 + b1 ⋅ t + b2 ⋅ t 2 + b3 ⋅ t 3 , then
we can represent it as:
a0 b0
a1 b1 = T ⋅ ( B ⋅ G ) (2.40)
{x (t ), y(t )} = 1 t t2 t3
a2 b2
a3 b3
70 ◾ Fundamentals of Graphics Using MATLAB®
The second matrix gives the product of B and G and can be directly plugged into
Equation (2.39) to calculate G2.
Example 2.9
Consider a C–R spline through P0(1, −1), P1(1, 4), P2(4, 4), and P3(5, −2). Convert it
to a Bezier spline and find its new CPs. Also compare equations of both the curves.
1 −1 0 1 0 0
1 −0.5 0 0.5 0
4
In this case: GCR = , BCR = ,
4 4 1 −2.5 2 −0.5
5 −2 0.5
−0.5 1.5 −1.5
1 0 0 0
−3 3 0 0
BBZ =
3 −6 3 0
−1 3 −3 1
From Equation (2.39)
1 4
1.5 4.83
−1
GBZ = BBZ ⋅ BCR ⋅ GCR =
3.33 5
4 4
Thus CPs of the Bezier curve are Q0 (1,4), Q1 (1.5,4.83), Q2 (3.33,5),and Q3 (4,4).
To verify that both of these represent the same physical spline.
Equation of CR spline:
0 1 0 0 1 −1
PCR = T ⋅ BCR ⋅ GCR = 1 t t2 t3 −0.5 0 0.5 0 1 4
1 −2.5 2 −0.5 4 4
−0.5 1.5 −1.5 0.5 5 −2
( )(
= 1 + 1.5t + 4t 2 − 2.5t 3 , 4 + 2.5t − 2t 2 − 0.5t 3 )
Equation of Bezier spline:
1 0 0 0 1 4
1.5
PBZ = T ⋅ BBZ ⋅ GBZ = 1 t t2 t3 −3 3 0 0 4.83
3 −6 3 0 3.33 5
−1 3 −3 1 4 4
( )(
= 1 + 1.5t + 4t 2 − 2.5t 3 , 4 + 2.5t − 2t 2 − 0.5t 3 )
Blending Functions and Hybrid Splines ◾ 71
y
1 1
0 0
-1 -1
P0
-2 -2
P3
-3 -3
0 1 2 3 4 5 6 0 1 2 3 4 5 6
x x
As can be seen both of these equations point to the same physical spline, only the
basis matrix and CPs are different in each case (Figure 2.18).
MATLAB Code 2.9
clear all; clc;
B_CR = [0, 1, 0, 0 ; -0.5, 0, 0.5, 0 ; 1, -2.5, 2, -0.5 ; -0.5, 1.5, -1.5, 0.5];
B_BZ = [1, 0, 0, 0 ; -3, 3, 0, 0 ; 3, -6, 3, 0 ; -1, 3, -3, 1];
G_CR = [1, -1 ; 1, 4 ; 4, 4 ; 5, -2];
%verification
syms t;
T = [1, t, t^2, t^3];
PCR = T * B_CR * G_CR
PBZ = T * B_BZ * G_BZ
%plotting
72 ◾ Fundamentals of Graphics Using MATLAB®
tt = linspace(1,0);
xcr = subs(PCR(1), t, tt);
ycr = subs(PCR(2), t, tt);
xbz = subs(PBZ(1), t, tt);
ybz = subs(PBZ(2), t, tt);
NOTE
axis: Specifies range of values to display in the order [xmin, xmax, ymin, ymax]
Blending Functions and Hybrid Splines ◾ 73
74 ◾ Fundamentals of Graphics Using MATLAB®
2.9 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
• BFs are functions of the parametric variable t used to derive equations of non-
interpolating splines.
• BFs are derived by assuming variable masses kept on the CPs, which exert gravita-
tional influence on the spline.
• The actual spline is the locus of the center of mass of these variable masses.
• Equation of a spline can be represented as product of BFs and CP locations.
• A Hermite spline is specified by two CPs and slopes of the curve at these points.
• A Cardinal spline is similar to Hermite spline but slopes are not directly specified.
• Slopes of Cardinal splines are determined by lines joining these points with two
additional points.
• A Cardinal spline is also associated with a shape parameter “s,” which determine the
shape of the curve.
• A C–R spline is a special case of a Cardinal spline with s = 0.5.
• A Bezier curve passes through the first and last CPs and is entirely contained within
its convex hull.
• BFs of Bezier splines can be derived from Bernstein polynomials of the same degree.
• A spline of one type can be converted to a spline of another type having a different
set of CPs.
2.10 REVIEW QUESTIONS
1. What are meant by BFs?
2. How are BFs related to basis matrix?
3. For an interpolating spline, under what conditions are BFs 0 and 1?
4. Justify that interpolating splines are a special case of non-interpolating splines.
5. What are the boundary conditions for specifying Hermite splines?
6. How are Cardinal splines different from a Hermite spline?
7. What does the shape parameter of Cardinal splines determine?
8. Why is a C–R spline a special case of Cardinal spline?
9. Why are BFs of Bezier splines derived from Bernstein polynomials?
10. How is a spline of one type converted to a spline of another type?
Blending Functions and Hybrid Splines ◾ 75
2.11 PRACTICE PROBLEMS
1. For a cubic Hermite spline P(t ) = 1 + bt – 2at 2 – bt 3 having equal slopes at start and end
points, find a relation between constants a and b.
2. Find equations of a Cardinal spline through (1, −1), (1, 1), (4, 3), and (5, −2) with shape
parameter (1) s = 0.1 and (2) s = 0.9.
3. A cubic curve has the following BFs, where a, b, and c are the constants:
L0 = 1 − at + bt 2 − 2ct 3 , L1 = 2bt − ct 2 + at 3 , L2 = a − 2t + 3t 2 , L3 = −3 − at 2 − ct 3. Find its
basis matrix.
4. A cubic interpolating spline with CPs (3, −4), (2, 3), (−2, −3), and (1, 0) and sub-
division ratios 0.3 and 0.5 are converted into a Cardinal spline with shape factor 0.7.
Find its CPs and equation of the curves.
5. A quadratic Bezier curve is associated with following CPs: (2, 2), (0, 0), and (−4, 4).
Find its equation.
6. Find parametric equations of a cubic Bezier spline having CPs (3, −4), (2, 3), (0, 0), and
(−2, −3).
7. Show that for a quadratic Bezier curve with CPs P0, P1, and P2, the following
expression holds true: P(0) + P ′(1) = 2 ( P2 − P0 ).
8. Show that for a cubic Bezier curve with CPs P0, P1, P2, and P3 the following
expressions are true: P ′(0) = 3( P1 − P0 ) and P ′(1) = 3( P3 − P2 ).
( )
9. Convert the parametric curve 1 + 2t + 3t 2 , 4 + 5t + 6t 2 to a quadratic Bezier curve
and find its CPs.
10. Convert the cubic Bezier spline having CPs (3, −4), (2, 3), (0, 0), and (−2, −3) to a cubic
C–R spline and find its CPs.
Chapter 3
Approximating Splines
3.1 INTRODUCTION
In the previous chapters, we have discussed how interpolating splines and hybrid splines
are characterized using control points (CPs) and blending functions (BFs). Bezier
splines in particular are quite popular and extensively used tools in graphics packages
for modeling splines. However, Bezier splines have two major drawbacks, which have
been the motivation for designing approximating splines. The first drawback is that the
number of CPs is dependent on the degree of the curve i.e. it is not possible to increase
the number of CPs for smoother control without increasing the degree of the poly-
nomial and hence computational complexity. The second drawback is that there is no
provision of local control i.e. shifting the location of a single CP will change the shape
of the entire spline instead of a local portion of the spline. In graphics local, control is
usually desirable since it enables small adjustments in the spline to be made (Hearn and
Baker, 1996).
To overcome, these drawbacks a new type of spline called B-spline has been proposed.
B-spline stands for “basis spline” and these are truly approximating i.e. in general they do
not go through any of the CPs but under special conditions they may be forced to do so.
Essentially, B-splines consist of multiple curve segments with continuity at join points.
This enables local control i.e. when a CP is moved only a set of local curve segments are
changed instead of the entire curve. The BFs of B-splines are calculated using an algorithm
known as Cox de Boor algorithm, named after the German mathematician Carl-Wilhelm
Reinhold de Boor. Values of the parametric variable t at the join points are stored in a vec-
tor called the “knot vector (KV).” If the knot values are equally spaced, then the resulting
spline is called uniform B-spline; otherwise, it is referred to as non-uniform. B-splines are
called open uniform when KV values are repeated.
The BFs and equation of B-splines are not single entities but a collection of expres-
sions for each segment. For example, if there are four segments, designated as A, B, C,
and D then each BF B has four sub-components BA, BB, BC, and BD and represented as
B = {BA, BB, BC, BD}. If there are say three CPs P0, P1, and P2 then there would be three
77
78 ◾ Fundamentals of Graphics Using MATLAB®
BFs B0, B1, and B2 each with four sub-components i.e. B0A, … , B0D, B1A, … , B1D, and
B2A, … , B2D. Usually the BFs are represented one per line as:
Similarly, the curve equation P also would have four sub-components P = {PA, PB, PC,
PD}. We have seen before that equation of a curve can be expressed as products of CPs
and BFs, hence the sub-components can be written as: PA = P0.B0A + P1.B1A + P2.B2A,
PB = P0.B0B + P1.B1B + P2.B2B and so on. Note that the sub-components are valid for
different ranges of t and hence cannot be added up but needs to be represented as a matrix
of values. Typically segment A is valid over the range t0 ≤ t < t1, B over t1 ≤ t < t 2 and so on.
Since the curve expressions can be quite large, in most cases the equations are written
vertically instead of horizontally, with the range of values mentioned for each segment, as
shown below:
The details about computations of the B and P values are illustrated in the following
sections.
Let the curve segments be designated as A, B, C, and D and the CPs be P0, P1, and P2. Let
the elements in the KV be designated as = {tk}, where k cycles over the values {0, 1, 2, 3, 4}.
Approximating Splines ◾ 79
In this case, the KV is T = [t0 , t1 , t 2 , t3 , t 4 ]. Let the BFs be designated in the form Bk,d. Since
there are three CPs, the BFs of the curve are given by B0,2, B1,2, and B2,2 and higher values of
k are not relevant here. The equation of the curve is given by:
Figure 3.1 is a schematic diagram which indicates how the three CPs P0, P1, and P2 are
associated with the three BFs B0,2, B1,2, and B2,2, the four segments A, B, C, and D and the
five element KV [t0, t1, t2, t3, t4]. Irrespective of the actual locations of the CPs, their influ-
ences over specific segments remain the same. CP P0 exerts influence over segments A and
B, P1 over segment B and C, and P2 over C and D. This provides the local control property
characteristic of B-splines: changing the location of one CP changes only two segments of
the spline while the remaining parts of the spline remains unchanged. The dashed lines in
the figure indicate the range of influence. In the remaining part of this section, we provide
verification of this fact.
The expressions of the BFs are calculated using an algorithm known as Cox de Boor
algorithm, (Hearn and Baker, 1996) which is represented below:
Bk ,1 = 1, if t k ≤ t ≤ t k +1 , else 0
t − tk t −t (3.2)
Bk ,d = ⋅ Bk ,d −1 + k + d ⋅ Bk +1,d −1
t k + d −1 − t k t k + d − t k +1
The first line states that the first-order terms, with d = 1, of the BFs are equal to 1 if value of
t for a particular segment lies within a specific range, else 0. The second line dictates how
the higher order terms, with d > 1, are calculated based on first-order terms. The algorithm
will be illustrated later. One point to be noted is that the algorithm is essentially recursive
in nature as the higher order terms, pertaining to degree d, are calculated based on the
lower order terms, pertaining to degree d − 1. Thus even though the equation of the curve
requires only second-order terms Bk,2 as shown in Equation (3.1), the first-order terms Bk,1
would need to be computed first.
1
A B C D
P0 P1 P2
-1
t0 t1 t2 t3 t4
To find the BF and curve equation each segment has to be analyzed separately. But first,
we need numerical values for the KV. Let us make a simplifying assumption that KV values
are as follows: t0 = 0, t1 = 1, t 2 = 2, t3 = 3, t 4 = 4 . Thus the KV becomes T = [0, 1, 2, 3, 4]. In
due course, we will justify this assumption.
Segment A: t0 ≤ t < t1
t − t0 t −t
B0,2 = ⋅ B0,1 + 2 ⋅ B1,1 = t
t1 − t0 t 2 − t1
t − t1 t −t
B1,2 = ⋅ B1,1 + 3 ⋅ B2,1 = 0
t 2 − t1 t3 − t 2
t − t2 t −t
B2,2 = ⋅ B2,1 + 4 ⋅ B3,1 = 0
t3 − t 2 t 4 − t3
Segment B: t1 ≤ t < t2
t − t0 t −t
B0,2 = ⋅ B0,1 + 2 ⋅ B1,1 = 2 − t
t1 − t0 t 2 − t1
t − t1 t3 − t
B1,2 = ⋅ B + ⋅ B = t − 1
t 2 − t1 t − t
1,1 2,1
3 2
t − t2 t −t
B2,2 = ⋅ B2,1 + 4 ⋅ B3,1 = 0
t3 − t 2 t 4 − t3
Segment C: t2 ≤ t < t3
t − t0 t −t
B0,2 = ⋅ B0,1 + 2 ⋅ B1,1 = 0
t1 − t0 t 2 − t1
t − t1 t −t
B1,2 = ⋅ B1,1 + 3 ⋅ B2,1 = 3 − t
t 2 − t1 t3 − t 2
t − t2 t −t
B2,2 = ⋅ B2,1 + 4 ⋅ B3,1 = t − 2
t3 − t 2 t 4 − t3
Approximating Splines ◾ 81
Segment D: t3 ≤ t < t4
t − t0 t −t
B0,2 = ⋅ B0,1 + 2 ⋅ B1,1 = 0
t1 − t0 t 2 − t1
t − t1 t −t
B1,2 = ⋅ B1,1 + 3 ⋅ B2,1 = 0
t 2 − t1 t3 − t 2
t − t2 t −t
B2,2 = ⋅ B2,1 + 4 ⋅ B3,1 = 4 − t
t3 − t 2 t 4 − t3
Note that each segment has a specific value of k but we still cycle k over other possible val-
ues while calculating the BFs because we want to find out the influence of other CPs onto
that segment. For example, for segment A, = 0, but we calculate B1,2 and B2,2 to find out what
is the influence of the second and third CP on segment A. In this case, we see that both B1,2
and B2,2 are zeros, which signifies that segment A is only controlled by the first CP P0 and is
not influenced by the other CPs P1 and P2. See Equation (3.1). Similarly for segment B, k = 1
but we see that both B0,2 and B1,2 are non-zeros, which implies that segment B is influenced
by two CPs P0 and P1.
Now as per Equation (3.1), the BFs required to determine curve equation are B0,2, B1,2,
and B3,2. These BFs, however, have different values for different segments. So all these values
need to be collected together specifying the segment for which they are valid. We use a
separate subscript A, B, C, and D to denote the relevant segment.
Table 3.1 summarizes the segment-wise calculation of the BF values. As mentioned before
a KV of T = [0, 1, 2, 3, 4] is assumed.
Substituting the above values in Equation (3.3) we get:
Equation (3.4) specifies the BFs of a linear uniform B-spline having three CPs (represented
by three vertical rows) and four CPs (represented by four element vectors per row).
82 ◾ Fundamentals of Graphics Using MATLAB®
An alternative way of representation of the BFs is shown in Equation (3.5) where only
the non-zero values are indicated along with the segment name and range for t for which
they are valid.
t (0 ≤ t < 1)A
B0,2 =
2−t (1 ≤ t < 2)B
t − 1 (1 ≤ t < 2)B
B1,2 = (3.5)
3 − t (2 ≤ t < 3)C
t − 2 (2 ≤ t < 3)C
B2,2 =
4−t (3 ≤ t < 4)D
Equation (3.5) shows that B0,2 and hence, P0 influences segments A and B, B1,2 and P1
influences segments B and C, and B2,2 and P2 influences segments C and D, a fact that has
been indicated by the dashed lines in Figure 3.1.
The plot of the BFs is shown in Figure 3.2. Each BF has the same shape but shifted
toward the right by 1 with respect to the previous one. Thus each BF can be obtained from
the previous one by substituting t with (t − 1) as is evident from Equation (3.5). Also as
shown in Equation (3.4), each BF has four sub-divisions out of which two are non-zeros.
The first curve for B0,2 has non-zero parts for segments A (0 ≤ t < 1) and B (1 ≤ t < 2), the
second curve for B1,2 has non-zero parts for segments B (1 ≤ t < 2) and C (2 ≤ t < 3), the third
curve for B2,2 has non-zero parts for segments C (2 ≤ t < 3) and D (3 ≤ t < 4). Since the BFs
are associated with the CPs, this again provides an indication of the local control property
Approximating Splines ◾ 83
1
A
0.9 B02 B12 B22 B
C
0.8 D
0.7
0.6
0.5
B
0.4
0.3
0.2
0.1
0
0 0.5 1 1.5 2 2.5 3 3.5 4
t
of the spline i.e. the first CP has influence over the first two segments A and B, the second
CP has influence over B and C, and so on. This means that if the first CP is changed, it will
affect only the two of the four segments while the rest of the spline will remain unchanged.
This is in contrast with the interpolating and hybrid curves where each BF is valid over the
entire range of t.
The equation of the spline is a collection of the equations of its four segments:
PA (0 ≤ t < 1)
PB (1 ≤ t < 2)
P(t ) = (3.6)
PC (2 ≤ t < 3)
PD (3 ≤ t < 4)
where
Substituting values of the BFs from Table 3.1 into Equation (3.6) we get:
P0 ⋅ t (0 ≤ t < 1)
P0 ⋅ (2 − t ) + P1 ⋅ (t − 1) (1 ≤ t < 2)
P(t ) = (3.8)
P1 ⋅ (3 − t ) + P2 ⋅ (t − 2) (2 ≤ t < 3)
P2 ⋅ (4 − t ) (3 ≤ t < 4)
Equation (3.8) represents the equation of a linear uniform B-spline with three CPs and four
segments. The four parts are the equations of the four segments.
Example 3.1
Find the equation of a linear uniform B-spline having CPs (2, −3), (5, 5), and (8, −1).
Also write a program to plot the BFs.
From Equation (3.8) substituting the values of the given CPs:
2t (0 ≤ t < 1)
3t − 1 (1 ≤ t < 2)
x (t ) =
3t − 1 (2 ≤ t < 3)
−8t + 32 (3 ≤ t < 4)
−3t (0 ≤ t < 1)
8t − 11 (1 ≤ t < 2)
y(t ) =
−6t + 17 (2 ≤ t < 3)
t−4 (3 ≤ t < 4)
MATLAB® Code 3.1
clear all; format compact; clc;
t0 = 0; t1 = 1; t2 = 2; t3 = 3; t4 = 4;
T = [t0, t1, t2, t3, t4];
syms t P0 P1 P2;
%Segment A
B01 = 1; B11 = 0; B21 = 0; B31 = 0; B41 = 0; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02A = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12A = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22A = B22;
%Segment B
B01 = 0; B11 = 1; B21 = 0; B31 = 0; B41 = 0; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02B = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12B = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22B = B22;
%Segment C
B01 = 0; B11 = 0; B21 = 1; B31 = 0; B41 = 0; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02C = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12C = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22C = B22;
%Segment D
B01 = 0; B11 = 0; B21 = 0; B31 = 1; B41 = 0; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02D = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12D = B12;
Approximating Splines ◾ 85
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22D = B22;
fprintf('Blending functions :\n');
fprintf('\n');
fprintf('\n');
x0 = 2; x1 = 5; x2 = 8;
y0 = -3; y1 = 5; y2 = -1;
86 ◾ Fundamentals of Graphics Using MATLAB®
%plotting BFs
figure
plot(tta, B02aa, 'k-', ttb,B02bb, 'k--', ttc,B02cc, 'k-.', ttd,B02dd, 'k:'); hold on;
plot(tta, B12aa, 'k-', ttb,B12bb, 'k--', ttc,B12cc, 'k-.', ttd,B12dd, 'k:');
plot(tta, B22aa, 'k-', ttb,B22bb, 'k--', ttc,B22cc, 'k-.', ttd,B22dd, 'k:'); hold off;
xlabel('t'); ylabel('B');
legend('A', 'B', 'C', 'D');
text(0.6, 0.9, 'B02'); text(1.6, 0.9, 'B12'); text(2.6, 0.9, 'B22');
NOTE
See Figure 3.2 for a plot of the BFs.
Approximating Splines ◾ 87
88 ◾ Fundamentals of Graphics Using MATLAB®
Let the curve segments be designated as A, B, C, D, and E (see Figure 3.3) and the CPs be
P0, P1, P2, and P3.
Number of elements in the KV: d + n +1 = 6
Let the elements in the KV be designated as T = {tk}, where k cycles over the values
{0, 1, 2, 3, 4, 5}. In this case, T = [t0, t1, t2, t3, t4, t5, t6].
Number of BFs is same as the number of CPs i.e. 4. Let the BFs be designated as Bk,d.
In this case, the BFs are B0,2, B1,2, B2,2, and B3,2. Higher values of k are not relevant and hence
need not be calculated. Equation of the curve is given by:
If the process outlined in the previous section is followed, the reader will be able to verify
that this does indeed produce a valid B-spline. It is therefore left as an exercise. The final
BFs and the equation of the curve are given below as a reference. Compare with Equations
(3.5) and (3.8) to see the difference.
t (0 ≤ t < 1)
B0,2 =
2 − t (1 ≤ t < 2)
1
A B C D E
P0 P1 P2 P3
-1
t0 t1 t2 t3 t4 t5
t − 1 (1 ≤ t < 2)
B1,2 =
3 − t (2 ≤ t < 3)
t − 2 (2 ≤ t < 3)
B2,2 =
4−t (3 ≤ t < 4)
t − 3 (3 ≤ t < 4)
B3,2 =
5 − t (4 ≤ t < 5)
P0 ⋅ t (0 ≤ t < 1)
P0 ⋅ (2 − t ) + P1 ⋅ (t − 1) (1 ≤ t < 2)
P(t ) = P1 ⋅ (3 − t ) + P2 ⋅ (t − 2) (2 ≤ t < 3)
P2 ⋅ (4 − t ) + P3 ⋅ (t − 3) (3 ≤ t < 4)
P3 ⋅ (5 − t ) (4 ≤ t < 5)
Let the curve segments be A, B, C, D, E, and F and CPs be P0, P1, P2, and P3 (see Figure 3.4).
1
A B C D E F
P0 P1 P2 P3
-1
t0 t1 t2 t3 t4 t5 t6
Let the KV be T = {tk} for k = {0, 1, 2, 3, 4, 5, 6}. In this case, T = [t0, t1, t2, t3, t4, t5, t6].
Let the BFs be B0,3, B1,3, B2,3, and B3,3
Equation of the curve is:
As before, we assume the KV to be T = [0, 1, 2, 3, 4, 5, 6]. The first-order terms B0,3, B0,1,
B1,1, B2,1, B3,1, and B4,1 will be either 0 or 1 as per the first condition of the Cox de Boor
algorithm. According to the second condition of the algorithm, the second-order terms
are computed as follows:
This time, there are also third-order terms, which are calculated from second-order terms:
1 1
B0,3 = (t − 0) ⋅ B0,2 + (3 − t ) ⋅ B1,2
2 2
1 1
B1,3 = (t − 1) ⋅ B1,2 + (4 − t ) ⋅ B2,2
2 2
(3.12)
1 1
B2,3 = (t − 2) ⋅ B2,2 + (5 − t ) ⋅ B3,2
2 2
1 1
B3,3 = (t − 3) ⋅ B3,2 + (6 − t ) ⋅ B4,2
2 2
1 2
t (0 ≤ t < 1) A
2
3
B0,3 = −t 2 + 3t − (1 ≤ t < 2) B
2
1 2
(t − 3) (2 ≤ t < 3) C
2
1 2
(t − 1) (1 ≤ t < 2) B
2
11
B1,3 = −t 2 + 5t − (2 ≤ t < 3) C
2
1 2
(t − 4) (3 ≤ t < 4) D
2
1
(t − 2)2 (2 ≤ t < 3) C (3.14)
2
23
B2,3 = −t 2 + 7t − (3 ≤ t < 4) D
2
1
(t − 5)2 (4 ≤ t < 5) E
2
1
(t − 3)2 (3 ≤ t < 4) D
2
39
B3,3 = −t 2 + 9t − (4 ≤ t < 5) E
2
1
(t − 6)2 (5 ≤ t < 6) F
2
Equation (3.14) represents the BFs of a quadratic uniform B-spline with four CPs and
six segments. The plot of the BFs is shown in Figure 3.5. Each BF has the same shape
but shifted towards the right by 1 with respect to the previous one. Thus each BF can be
Approximating Splines ◾ 93
1
A
0.9 B03 B13 B23 B33 B
C
D
0.8 E
F
0.7
0.6
0.5
B
0.4
0.3
0.2
0.1
0
0 1 2 3 4 5 6
t
obtained from the previous one by substituting t with (t − 1). Each BF has six sub-divisions
out of which three are non-zeros. The first curve for B0,3 has non-zero parts for segments
A(0 ≤ t < 1), B(1 ≤ t < 2), C(2 ≤ t < 3), the second curve for B1,3 has non-zero parts for seg-
ments B(1 ≤ t < 2), C(2 ≤ t < 3), D(3 ≤ t < 4), the third curve for B2,3 has non-zero parts for
segments C(2 ≤ t < 3), D(3 ≤ t < 4), E(4 ≤ t < 5), and the fourth curve for B3,3 has non-zero
parts for segments D(3 ≤ t < 4), E(4 ≤ t < 5), F (5 ≤ t < 6).
The equation of the spline is a collection of the equations of its six segments:
PA (0 ≤ t < 1)
PB (1 ≤ t < 2)
PC (2 ≤ t < 3)
P(t ) =
PD (3 ≤ t < 4) (3.15)
PE (4 ≤ t < 5)
PF (5 ≤ t < 6)
94 ◾ Fundamentals of Graphics Using MATLAB®
where
Substituting values of the BFs from Table 3.2 into Equation (3.15) and specifying the range
for which they are valid we get:
1
P0 ⋅ t 2 (0 ≤ t < 1)
2
3 1
P0 ⋅ −t 2 + 3t − + P1 ⋅ (t − 1)2 (1 ≤ t < 2)
2 2
1 11 1
P0 ⋅ (t − 3)2 + P1 ⋅ −t 2 + 5t − + P2 ⋅ (t − 2)2 (2 ≤ t < 3)
2 2 2
P(t ) = (3.17)
1 2 2 23 1 2
P1 ⋅ 2 (t − 4) + P2 ⋅ −t + 7t − 2 + P3 ⋅ 2 (t − 3) (3 ≤ t < 4)
1 39
P2 ⋅ (t − 5)2 + P3 ⋅ −t 2 + 9t − (4 ≤ t < 5)
2 2
1
P3 ⋅ (t − 6)2 (5 ≤ t < 6)
2
Equation (3.17) represents the equation of a quadratic uniform B-spline with four CPs and
six segments. The six parts of the equation represent the six segments of the curve.
Example 3.2
Find the equation of a uniform quadratic B-spline having CPs P0(1, 2), P1(4, 1),
P2(6, 5), and P3(8, −1). Also write a program to plot the BFs and the actual curve.
Approximating Splines ◾ 95
t-x t-y
8 4
7
3
6
5 2
4
x
y
3 1
2
0
1
0 -1
0 1 2 3 4 5 6 0 1 2 3 4 5 6
t t
x-y
6
5
P2
4
3
P0
2
y
1
P1
0
P3
-1
-2
0 2 4 6 8
x
From Equation (3.17), substituting the values of the given CPs (Figure 3.6):
t2 (0 ≤ t < 1)
t2
(0 ≤ t < 1) 3t 2 5
2
− 2 + 5t − 2 (1 ≤ t < 2)
1
t2 −t + (1 ≤ t < 2) 2
2 5t 27
2 2 − 11t + 2 (2 ≤ t < 3)
x (t ) = t 11 y(t ) =
+ 5t − (2 ≤ t < 3) 2
2 2 −5t + 34t − 54 (3 ≤ t < 4)
2t − 1 (3 ≤ t < 4) 7t 2
− 34t + 82 (4 ≤ t < 5)
−5t + 42t − 81
2
(4 ≤ t < 5) 2
1
4(t − 6)2 (5 ≤ t < 6) − (t − 6)2 (5 ≤ t < 6)
2
MATLAB Code 3.2
clear all; format compact; clc;
t0 = 0; t1 = 1; t2 = 2; t3 = 3; t4 = 4; t5 = 5; t6 = 6;
T = [t0, t1, t2, t3, t4, t5, t6];
syms t P0 P1 P2 P3;
%Segment A
B01 = 1; B11 = 0; B21 = 0; B31 = 0; B41 = 0; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02A = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12A = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22A = B22;
(t - t3)/(t4
S1 = - t3); S2 = (t5 - t)/(t5 - t4); B32 = S1*B31 + S2*B41; B32A = B32;
(t - t4)/(t5
S1 = - t4); S2 = (t6 - t)/(t6 - t5); B42 = S1*B41 + S2*B51; B42A = B42;
(t - t0)/(t2
S1 = - t0); S2 = (t3 - t)/(t3 - t1); B03 = S1*B02 + S2*B12; B03A = B03;
(t - t1)/(t3
S1 = - t1); S2 = (t4 - t)/(t4 - t2); B13 = S1*B12 + S2*B22; B13A = B13;
96 ◾ Fundamentals of Graphics Using MATLAB®
(t - t2)/(t4
S1 = - t2); S2 = (t5 - t)/(t5 - t3); B23 = S1*B22 + S2*B32; B23A = B23;
(t - t3)/(t5
S1 = - t3); S2 = (t6 - t)/(t6 - t4); B33 = S1*B32 + S2*B42; B33A = B33;
%Segment B
B01 = 0; B11 = 1; B21 = 0; B31 = 0; B41 = 0; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02B = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12B = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22B = B22;
(t - t3)/(t4
S1 = - t3); S2 = (t5 - t)/(t5 - t4); B32 = S1*B31 + S2*B41; B32B = B32;
(t - t4)/(t5
S1 = - t4); S2 = (t6 - t)/(t6 - t5); B42 = S1*B41 + S2*B51; B42B = B42;
(t - t0)/(t2
S1 = - t0); S2 = (t3 - t)/(t3 - t1); B03 = S1*B02 + S2*B12; B03B = B03;
(t - t1)/(t3
S1 = - t1); S2 = (t4 - t)/(t4 - t2); B13 = S1*B12 + S2*B22; B13B = B13;
(t - t2)/(t4
S1 = - t2); S2 = (t5 - t)/(t5 - t3); B23 = S1*B22 + S2*B32; B23B = B23;
(t - t3)/(t5
S1 = - t3); S2 = (t6 - t)/(t6 - t4); B33 = S1*B32 + S2*B42; B33B = B33;
%Segment C
B01 = 0; B11 = 0; B21 = 1; B31 = 0; B41 = 0; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02C = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12C = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22C = B22;
(t - t3)/(t4
S1 = - t3); S2 = (t5 - t)/(t5 - t4); B32 = S1*B31 + S2*B41; B32C = B32;
(t - t4)/(t5
S1 = - t4); S2 = (t6 - t)/(t6 - t5); B42 = S1*B41 + S2*B51; B42C = B42;
(t - t0)/(t2
S1 = - t0); S2 = (t3 - t)/(t3 - t1); B03 = S1*B02 + S2*B12; B03C = B03;
(t - t1)/(t3
S1 = - t1); S2 = (t4 - t)/(t4 - t2); B13 = S1*B12 + S2*B22; B13C = B13;
(t - t2)/(t4
S1 = - t2); S2 = (t5 - t)/(t5 - t3); B23 = S1*B22 + S2*B32; B23C = B23;
(t - t3)/(t5
S1 = - t3); S2 = (t6 - t)/(t6 - t4); B33 = S1*B32 + S2*B42; B33C = B33;
%Segment D
B01 = 0; B11 = 0; B21 = 0; B31 = 1; B41 = 0; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02D = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12D = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22D = B22;
(t - t3)/(t4
S1 = - t3); S2 = (t5 - t)/(t5 - t4); B32 = S1*B31 + S2*B41; B32D = B32;
(t - t4)/(t5
S1 = - t4); S2 = (t6 - t)/(t6 - t5); B42 = S1*B41 + S2*B51; B42D = B42;
(t - t0)/(t2
S1 = - t0); S2 = (t3 - t)/(t3 - t1); B03 = S1*B02 + S2*B12; B03D = B03;
(t - t1)/(t3
S1 = - t1); S2 = (t4 - t)/(t4 - t2); B13 = S1*B12 + S2*B22; B13D = B13;
(t - t2)/(t4
S1 = - t2); S2 = (t5 - t)/(t5 - t3); B23 = S1*B22 + S2*B32; B23D = B23;
(t - t3)/(t5
S1 = - t3); S2 = (t6 - t)/(t6 - t4); B33 = S1*B32 + S2*B42; B33D = B33;
%Segment E
B01 = 0; B11 = 0; B21 = 0; B31 = 0; B41 = 1; B51 = 0; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02E = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12E = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22E = B22;
(t - t3)/(t4
S1 = - t3); S2 = (t5 - t)/(t5 - t4); B32 = S1*B31 + S2*B41; B32E = B32;
(t - t4)/(t5
S1 = - t4); S2 = (t6 - t)/(t6 - t5); B42 = S1*B41 + S2*B51; B42E = B42;
(t - t0)/(t2
S1 = - t0); S2 = (t3 - t)/(t3 - t1); B03 = S1*B02 + S2*B12; B03E = B03;
Approximating Splines ◾ 97
S1 =
(t - t1)/(t3 - t1); S2 = (t4 - t)/(t4 - t2); B13 = S1*B12 + S2*B22; B13E = B13;
S1 = (
t - t2)/(t4 - t2); S2 = (t5 - t)/(t5 - t3); B23 = S1*B22 + S2*B32; B23E = B23;
S1 =
(t - t3)/(t5 - t3); S2 = (t6 - t)/(t6 - t4); B33 = S1*B32 + S2*B42; B33E = B33;
%Segment F
B01 = 0; B11 = 0; B21 = 0; B31 = 0; B41 = 0; B51 = 1; B61 = 0;
(t - t0)/(t1
S1 = - t0); S2 = (t2 - t)/(t2 - t1); B02 = S1*B01 + S2*B11; B02F = B02;
(t - t1)/(t2
S1 = - t1); S2 = (t3 - t)/(t3 - t2); B12 = S1*B11 + S2*B21; B12F = B12;
(t - t2)/(t3
S1 = - t2); S2 = (t4 - t)/(t4 - t3); B22 = S1*B21 + S2*B31; B22F = B22;
(t - t3)/(t4
S1 = - t3); S2 = (t5 - t)/(t5 - t4); B32 = S1*B31 + S2*B41; B32F = B32;
(t - t4)/(t5
S1 = - t4); S2 = (t6 - t)/(t6 - t5); B42 = S1*B41 + S2*B51; B42F = B42;
(t - t0)/(t2
S1 = - t0); S2 = (t3 - t)/(t3 - t1); B03 = S1*B02 + S2*B12; B03F = B03;
(t - t1)/(t3
S1 = - t1); S2 = (t4 - t)/(t4 - t2); B13 = S1*B12 + S2*B22; B13F = B13;
(t - t2)/(t4
S1 = - t2); S2 = (t5 - t)/(t5 - t3); B23 = S1*B22 + S2*B32; B23F = B23;
(t - t3)/(t5
S1 = - t3); S2 = (t6 - t)/(t6 - t4); B33 = S1*B32 + S2*B42; B33F = B33;
fprintf('\n');
fprintf('\n');
x0 = 1; x1 = 4; x2 = 6; x3 = 8;
y0 = 2; y1 = 1; y2 = 5; y3 = -1;
%plotting BFs
figure,
plot(tta, B03aa, 'k-', ttb, B03bb, 'k--', ttc, B03cc, 'k-.', ...
ttd, B03dd, 'b-', tte, B03ee, 'b--', ttf, B03ff, 'b-.');
hold on;
plot(tta, B13aa, 'k-', ttb, B13bb, 'k--', ttc, B13cc, 'k-.', ...
ttd, B13dd, 'b-', tte, B13ee, 'b--', ttf, B13ff, 'b-.');
plot(tta, B23aa, 'k-', ttb, B23bb, 'k--', ttc, B23cc, 'k-.', ...
ttd, B23dd, 'b-', tte, B23ee, 'b--', ttf, B23ff, 'b-.');
plot(tta, B33aa, 'k-', ttb, B33bb, 'k--', ttc, B33cc, 'k-.', ...
ttd, B33dd, 'b-', tte, B33ee, 'b--', ttf, B33ff, 'b-.');
xlabel ('t'); ylabel('B'); title('B03 - B13 - B23 - B33');
100 ◾ Fundamentals of Graphics Using MATLAB®
figure
subplot(131),
plot(tta, xaa, 'k-', ttb, xbb, 'k--', ttc, xcc, 'k-.', ...
ttd, xdd, 'b-', tte, xee, 'b--', ttf, xff, 'b-.');
xlabel ('t'); ylabel('x'); axis square; title('t - x');
subplot(132),
plot(tta, yaa, 'k-', ttb, ybb, 'k--', ttc, ycc, 'k-.', ...
ttd, ydd, 'b-', tte, yee, 'b--', ttf, yff, 'b-.')
xlabel ('t'); ylabel('y'); axis square; title('t - y');
subplot(133),
plot(xaa, yaa, 'k-', xbb, ybb, 'k--', xcc, ycc, 'k-.', ...
xdd, ydd, 'b-', xee, yee, 'b--', xff, yff, 'b-.'); hold on;
scatter(X, Y, 20, 'r', 'filled');
xlabel ('x'); ylabel('y'); axis square; grid; title('x - y');
axis([0 9 -2 6]);
d = 0.5;
text(x0, y0+d, 'P_0');
text(x1, y1-d, 'P_1');
text(x2, y2-d, 'P_2');
text(x3, y3+d, 'P_3');
hold off;
NOTE
...: continues the current command or function call onto the next line
simplify: simplifies an equation by resolving all intersections and nestings
Approximating Splines ◾ 101
102 ◾ Fundamentals of Graphics Using MATLAB®
1 2
t (0 ≤ t < 1)A
50
t 2 3t 3
B0,3 = − + − (1 ≤ t < 2)B
25 5 2
1 2
(t − 15) (2 ≤ t < 3)C
50
1
(t − 5)2 (1 ≤ t < 2)B
50
t2 11
B1,3 = − + t − (2 ≤ t < 3)C
25 2
1 2
50 (t − 20) (3 ≤ t < 4)D
(3.18)
1 2
(t − 10) (2 ≤ t < 3)C
50
t 2 7t 23
B2,3 = − + − (3 ≤ t < 4)D
25 5 2
1 2
(t − 25) (4 ≤ t < 5)E
50
1 2
(t − 15) (3 ≤ t < 4)D
50
t 2 9t 39
B3,3 = − + − (4 ≤ t < 5)E
25 5 2
1 2
(t − 30) (5 ≤ t < 6)F
50
Approximating Splines ◾ 103
t
Comparing with Equation (3.14) tells us that t has been replaced by i.e. all t values have
5
been scaled by a factor of 5. So now, to get the same B values require the t values to be
5 times larger than earlier. This is reflected in the BF plot in Figure 3.7, which shows the
t-axis to be expanded by 5 times. Compare with the BF plot in Figure 3.5.
On plugging the new KV into Example 3.2, we can observe the effects on the parametric
curves. The x(t) and y(t) values shown below are seen to be affected similar to the BFs i.e.
the values have been scaled by 5.
t2 t2
(0 ≤ t < 1) (0 ≤ t < 1)
50 25
t 2
t 1 3t 2
5
− + (1 ≤ t < 2) − +t − (1 ≤ t < 2)
25 5 2 50 2
t 2
11 t 11t 27
2
+t − (2 ≤ t < 3) − + (2 ≤ t < 3)
x (t ) = 50 2 y(t ) = 10 5 2
2t 2
− t + 34t − 54
−1 (3 ≤ t < 4) (3 ≤ t < 4)
5 5 5
2
− t + 42t − 81
2
(4 ≤ t < 5) 7t − 34t + 82 (4 ≤ t < 5)
5 5 50 5
4
1
− (t − 30)2
(t − 30)2 (5 ≤ t < 6) (5 ≤ t < 6)
25 50
0.5
0.4
B
0.3
0.2
0.1
0
0 5 10 15 20 25 30
t
On plotting the actual curves, the x − t and y − t graphs are seen to be similarly affected
i.e. the t values have changed by 5 times. See Figure 3.8. The different line types indicate
the segment intervals. However, since the x and y values have remained unchanged, the
x − y plot remains exactly the same as before as it is unaffected by any change in t values
so long as it affects the x and y values uniformly by the same amount. Compare with
Figure 3.6. For example, from Figure 3.6 for t = 2, x = 2.5, y = 1.5 and from Figure 3.8 for
t = 10, x = 2.5, y = 1.5, which implies that the x vs. y point remains unaffected. This is true
for all the points and so the x − y plot of the curve remains same as before. Obviously, this
is true for any scaling factor by which t is changed.
Readers are encouraged to generate the plots and verify the results by using different KV
values. Then can easily do so by using MATLAB Code 3.2 and simply changing the KV in
the second line of the program.
This leads us to conclude that the actual curve in the spatial domain is independent
of the KV values since it does not depend on the scaling factor of the t values. Hence,
it is customary to choose the smallest t values viz. 0, 1, 2, … to reduce the complexity
of the equations but in reality, we can choose any values for the KV and would get the
same result.
t-x t-y
8 4
7
3
6
5 2
4
x
3 1
2
0
1
0 -1
0 5 10 15 20 25 30 0 5 10 15 20 25 30
t t
x-y
6
5
P2
4
3
P0
2
y
1
P1
0
P2
-1
-2
0 2 4 6 8
x
3 2 7
− t + 5t − (2 ≤ t < 3) C
2 2
B1,3 =
1
(t − 3)2 (3 ≤ t < 4) D
2
(3.19)
1
(t − 1)2 (2 ≤ t < 3) C
2
B2,3 =
− t 2 + 7t − 15
3
(3 ≤ t < 4) D
2 2
Equation (3.19) represents the BFs of a quadratic open-uniform B-spline with four CPs. The
plot of the BFs is shown in Figure 3.9. Only the portion pertaining to segments C(2 ≤ t < 3)
and D(3 ≤ t < 4) are present.
The curve equation is obtained by substituting the BF values into Equation (3.15):
0 (0 ≤ t < 1)
0 (1 ≤ t < 2)
3 7 1
P0 ⋅ (t − 2)2 + P1 ⋅ − t 2 + 5t − + P2 ⋅ (t − 1)2 (2 ≤ t < 3)
2 2 2
P(t ) = (3.20)
P1 ⋅ 1 (t − 3)2 + P2 ⋅ − 3 t 2 + 7t − 15 + P3 ⋅ (t − 2)2 (3 ≤ t < 4)
2 2 2
0 (4 ≤ t < 5)
0 (5 ≤ t < 6)
106 ◾ Fundamentals of Graphics Using MATLAB®
A plot of the curve in Figure 3.10 shows the effect of multiplicity on the spline: it has
forced the curve to actually go through the first and last CPs. This has created a spline with
only two segments C and D, while the other segments A, B, E, and F are non-existent. We
can, therefore, conclude that an approximating spline can behave like an interpolating or
hybrid spline if the KV has repeated values.
Approximating Splines ◾ 107
1
A
0.9 B
C
0.8 D
E
0.7 F
0.6
0.5
B
0.4
0.3
0.2
0.1
0
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
t
t-x t-y
8 4
3
6
2
4
x
2
0
0 -1
1 1.5 2 2.5 3 1 1.5 2 2.5 3
t t
x-y
6
P2
4
P0
2
y
P1
0
P3
-2
0 2 4 6 8
x
Example 3.3
Find the equation of an open-uniform quadratic B-spline having CPs (1, 2),
(4, 1), (6, 5), and (8, −1) assuming a KV T = [1, 1, 1, 2, 3, 3, 3]. Also
write a program to plot the BFs and the actual curve.
From Equation (3.20) substituting the given CPs we get:
0 (0 ≤ t < 1) 0 (0 ≤ t < 1)
0 (1 ≤ t < 2) 0 (1 ≤ t < 2)
2 2
−2t + 10t − 7 (2 ≤ t < 3) 3t − 8t + 7 (2 ≤ t < 3)
x (t ) = y(t ) =
2
−t − 2t + 5 (3 ≤ t < 4) 2
−8t + 36t − 37 (3 ≤ t < 4)
0 (4 ≤ t < 5) 0 (4 ≤ t < 5)
0 (5 ≤ t < 6) 0 (5 ≤ t < 6)
The code will be almost exactly same as MATLAB Code 3.2 except for an additional check
to avoid a divide-by-zero condition before calculating each BF. If a divide-by-zero condi-
tion exists, then the expression is to replaced by 0; otherwise, it should be calculated in the
normal process. This is illustrated as follows.
Example 3.4
Find the equation of a non-uniform quadratic B-spline having CPs (1, 2), (4, 1), (6, 5),
and (8, −1) assuming a KV T = [0, 1, 3, 5, 15, 18, 20].
Using a segment-wise analysis:
Approximating Splines ◾ 109
0.5
B
0.4
0.3
0.2
0.1
0
0 2 4 6 8 10 12 14 16 18 20
t
t2 2t 2
(0 ≤ t < 1) (0 ≤ t < 1)
3 3
5t 2
t 1 11t 2
9t 9
+ − (1 ≤ t < 2) − + − (1 ≤ t < 2)
24 4 8 24 4 8
7t 2 13t 37 7t 2
9t 45
− + − (2 ≤ t < 3) − + (2 ≤ t < 3)
24 4 8 24 4 8
x (t ) = 2
y (t ) = 2
t 9t 137 − 31t + 19t − 95
− + + (3 ≤ t < 4) (3 ≤ t < 4)
780 26 52 390 13 26
2 2
− 38t + 232t − 1,672 (4 ≤ t < 5) 43t − 98t + 830 (4 ≤ t < 5)
65 13 13 195 13 13
4(t − 20)2 (t − 20)2
(5 ≤ t < 6) − (5 ≤ t < 6)
5 10
Let the curve segments be A, B, C, D, E, F, G, and H and CPs be P0, P1, P2, P3, and P4
(see Figure 3.12).
Let the KV be T = {tk} for k = {0, 1, 2, 3, 4, 5, 6, 7, 8}. In this case,
T = [t0 , t1 , t 2 , t3 , t 4 , t5 , t6 , t7 , t8 ].
Let the BFs are B0,4, B1,4, B2,4, B3,4, and B4,4.
Equation of the curve is given by:
A B C D E F G H
P0 P1 P2 P3 P4
-1
t0 t1 t2 t3 t4 t5 t6 t7 t8
1 1
B0,3 = (t − 0) ⋅ B0,2 + (3 − t ) ⋅ B1,2
2 2
1 1
B1,3 = (t − 1) ⋅ B1,2 + (4 − t ) ⋅ B2,2
2 2
1 1
B2,3 = (t − 2) ⋅ B2,2 + (5 − t ) ⋅ B3,2
2 2
1 1
B3,3 = (t − 3) ⋅ B3,2 + (6 − t ) ⋅ B4,2
2 2 (3.23)
1 1
B4,3 = (t − 4) ⋅ B4,2 + (7 − t ) ⋅ B5,2
2 2
1 1
B5,3 = (t − 5) ⋅ B5,2 + (8 − t ) ⋅ B6,2
2 2
1 1
B6,3 = (t − 6) ⋅ B6,2 + (9 − t ) ⋅ B7,2
2 2
1 1
B0,4 = (t − 0) ⋅ B0,3 + (4 − t ) ⋅ B1,3
3 3
1 1
B1,4 = (t − 1) ⋅ B1,3 + (5 − t ) ⋅ B2,3
3 3
1 1
B2,4 = (t − 2) ⋅ B2,3 + (6 − t ) ⋅ B3,3 (3.24)
3 3
1 1
B3,4 = (t − 3) ⋅ B3,3 + (7 − t ) ⋅ B4,3
3 3
1 1
B4,4 = (t − 4) ⋅ B4,3 + (8 − t ) ⋅ B5,3
3 3
112 ◾ Fundamentals of Graphics Using MATLAB®
B2,4 = { B2,4 A , B2,4 B , B2,4C , B2,4 D , B2,4 E , B2,4 F , B2,4G , B2,4 H } (3.25)
1 3
t (0 ≤ t < 1)A
6
3
− t + 2t 2 − 2t + 2 (1 ≤ t < 2)B
3 3
B0,4 =
3
t 2 22
− 2 − 4t + 10t − 3 (2 ≤ t < 3)C
1
− (t − 4)3 (3 ≤ t < 4)D
6
1 3
(t − 1) (1 ≤ t < 2)A
6
3 2
− t + 7t − 15t + 31 (2 ≤ t < 3)B
2 2 2 6
B1,4 =
3 2
t 11t 39t 131
−2− 2 + 2 − 6 (3 ≤ t < 4)C
1
− (t − 5)3 (4 ≤ t < 5)D
6
1 3
(t − 2) (2 ≤ t < 3)A
6
3 2
− t + 5t − 16t + 50 (3 ≤ t < 4)B
2 2 3
B2,4 =
3
t 7t 2 142
2 − + 32t − (4 ≤ t < 5)C
2 3
1
− (t − 6)3 (5 ≤ t < 6)D
6
Approximating Splines ◾ 115
1 3
(t − 3) (3 ≤ t < 4)A
6
t 3 13t 2 55t 229
− + − + (4 ≤ t < 5)B
2 2 2 6
B3,4 = 3 2
t − 17t + 95t − 521 (5 ≤ t < 6)C
2 2 2 6
1
− (t − 7)3 (6 ≤ t < 7)D
6
(3.26)
1 3
(t − 4) (4 ≤ t < 5)A
6
t 3
218
− + 8t 2 − 42t + (5 ≤ t < 6)B
2 3
B4,4 = 3
t − 10t 2 + 66t − 430 (6 ≤ t < 7)C
2 3
1
− (t − 8)3 (7 ≤ t < 8)D
6
Equation (3.26) represents the BFs of a cubic uniform B-spline with five CPs and eight seg-
ments. The plot of the BFs is shown in Figure 3.13. Each BF has the same shape but shifted
toward the right by 1 with respect to the previous one. Thus, each BF can be obtained from
the previous one by substituting t with (t − 1). As shown in Equation (3.26), each BF has eight
sub-divisions out of which four are non-zeros. The first curve for B04 has non-zero parts
for segments A (0 ≤ t < 1), B (1 ≤ t < 2), C (2 ≤ t < 3), and D (3 ≤ t < 4), the second curve for
B14 has non-zero parts for segments B (1 ≤ t < 2), C (2 ≤ t < 3), D (3 ≤ t < 4), and E (4 ≤ t <
5), the third curve for B24 has non-zero parts for segments C (2 ≤ t < 3), D (3 ≤ t < 4), E (4
≤ t < 5), and F (5 ≤ t < 6), the fourth curve for B34 has non-zero parts for segments D (3 ≤ t
< 4), E (4 ≤ t < 5), F (5 ≤ t < 6), and G (6 ≤ t < 7), and the fourth curve for B44 has non-zero
parts for segments E (4 ≤ t < 5), F (5 ≤ t < 6), G (6 ≤ t < 7), and H (7 ≤ t < 8). Since the BFs are
associated with the CPs, this provides an indication of the local control property of the cubic
spline. The first CP has influence over the first four segments A, B, C, and D, the second CP
has influence over B, C, D, and E, and so on. This means that if the first CP is changed, it
will affect only the first four segments while the rest of the spline will remain unchanged.
116 ◾ Fundamentals of Graphics Using MATLAB®
0.3
0.2
0.1
0
0 1 2 3 4 5 6 7 8
t
The equation of the spline is a collection of the equations of its eight segments:
PA (0 ≤ t < 1)
PB (1 ≤ t < 2)
PC (2 ≤ t < 3)
PD (3 ≤ t < 4)
P(t ) =
PE (4 ≤ t < 5) (3.27)
PF (5 ≤ t < 6)
PG (6 ≤ t < 7)
PH (7 ≤ t < 8)
where
PA = P0 ⋅ B0,4 A + P1 ⋅ B1,4 A + P2 ⋅ B2,4 A + P3 ⋅ B3,4 A + P4 ⋅ B4,4 A
PB = P0 ⋅ B0,4 B + P1 ⋅ B1,4 B + P2 ⋅ B2,4 B + P3 ⋅ B3,4 B + P4 ⋅ B4,4 B
Substituting values of the BFs from Table 3.4 into Equation (3.28) we get:
1 3
P0 ⋅ 6 t
P ⋅ − t + 2t 2 − 2t + 2 + P ⋅ 1 (t − 1)3
3
0 1
3 3 6
P0 ⋅ − t − 4t 2 + 10t − 22 + P1 ⋅ − t + 7t − 15t + 31 + P2 ⋅ 1 (t − 2)3
3 3 2
2 3
2 2 2 6 6
3 2
3 2
P0 ⋅ 1 (4 − t )3 + P1 ⋅ − t − 11t + 39t − 131 + P2 ⋅ − t + 5t − 16t + 50 + P3 ⋅ 1 (t − 3)2
6
2 2 2 6
2 2 3 2
P(t ) =
P1 ⋅ 1 (5 − t )3 + P2 ⋅ t − 7t + 32t − 142 + P3 ⋅ − t + 13t − 55t + 229 + P4 ⋅ 1 (t − 4)3
3 2 3 2
6 2 2 3 2 2 2 6 6
3 2
3
P2 ⋅ 1 (6 − t )3 + P3 ⋅ t − 17t + 95t − 521 + P4 ⋅ − t + 8t 2 − 42t + 218
6
2 2 2 6
2 3
1 t3 430
P3 ⋅ (7 − t )3 + P4 ⋅ − 10t 2 + 66t −
6 2 3
1 3
P4 ⋅ (8 − t ) (3.29)
6
Equation (3.29) represents the curve equation of a cubic uniform B-spline with five CPs.
The eight sub-components of the equations represents the parts for the eight segments.
Example 3.5
Find the equation of a uniform cubic B-spline having CPs (−1, 0), (0, 1), (1, 0),
(0, −1), and (−0.5, −0.5). Also write a program to plot its BFs and the actual curve.
118 ◾ Fundamentals of Graphics Using MATLAB®
From Equation (3.29), substituting the values of the given CPs we get (Figure 3.14):
t3
(0 ≤ t < 1) 0 (0 ≤ t < 1)
6
(t − 1)3
t 3
2 (1 ≤ t < 2)
− 2t 2 + 2t − (1 ≤ t < 2) 6
2 3 3 2
t 3 − t + 7t − 15t + 31 (2 ≤ t < 3)
− + 3t − 8t + 6
2
(2 ≤ t < 3) 2 2 2 6
3 3
t3 t − 4t 2 + 15t − 52 (3 ≤ t < 4)
− + 3t 2 − 8t + 6 (3 ≤ t < 4) 3 3
3
x (t ) = ( ) =
yt t 3
3
5t − 6t 2 + 28t − 42 − 3t 2 + 11t − 12 (4 ≤ t < 5)
(4 ≤ t < 5) 4
12
t 3 9t 2 53t 101
t3
− 2t 2 + 3t −
1
(5 ≤ t < 6) −4+ 2 − 2 + 2 (5 ≤ t < 6)
12 3 3
t 3t 2 17t 29
12 2 − 2 + 2
− + (6 ≤ t < 7)
3
− t + 5t 2 − 33t + 215 (6 ≤ t < 7)
4 3
(t − 8)3
(t − 8) 3
(7 ≤ t < 8)
(7 ≤ t < 8) 12
12
t-x t-y
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
y
x
-0.2 -0.2
-0.4 -0.4
-0.6 -0.6
-0.8 -0.8
0 2 4 6 8 0 2 4 6 8
t t
x-y
1.5
0.5
0
y
-0.5
-1
-1.5
-1.5 -1 -0.5 0 0.5 1 1.5
x
x0 = -1; x1 = 0; x2 = 1; x3 = 0; x4 = -0.5;
y0 = 0; y1 = 1; y2 = 0; y3 = -1; y4 = -0.5;
t0 = 0; t1 = 1; t2 = 2; t3 = 3; t4 = 4; t5 = 5; t6 = 6; t7 = 7; t8 = 8;
T = [t0, t1, t2, t3, t4, t5, t6, t7, t8];
syms t P0 P1 P2 P3 P4;
syms B01 B11 B21 B31 B41 B51 B61 B71;
%Segment A
subs(B02,
B02A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B12,
B12A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B22,
B22A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B32,
B32A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B42,
B42A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B52,
B52A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B62,
B62A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B72,
B72A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B03,
B03A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B13,
B13A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B23,
B23A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
120 ◾ Fundamentals of Graphics Using MATLAB®
subs(B33,
B33A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B43,
B43A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B53,
B53A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B63,
B63A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B04,
B04A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B14,
B14A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B24,
B24A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B34,
B34A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B44,
B44A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
subs(B54,
B54A = {B01,B11,B21,B31,B41,B51,B61,B71},
{1,0,0,0,0,0,0,0});
%Segment B
subs(B02, {B01,B11,B21,B31,B41,B51,B61,B71},
B02B = {0,1,0,0,0,0,0,0});
subs(B12, {B01,B11,B21,B31,B41,B51,B61,B71},
B12B = {0,1,0,0,0,0,0,0});
B22B = ubs(B22,
s {B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B32B = subs(B32,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B42B =
subs(B42, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B52B = subs(B52,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B62B = subs(B62,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B72B =
subs(B72, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B03B = subs(B03,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B13B = subs(B13,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B23B = subs(B23,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B33B = subs(B33,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B43B = subs(B43,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B53B = subs(B53,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B63B = subs(B63,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B04B = subs(B04,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B14B = subs(B14,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B24B = subs(B24,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B34B =
subs(B34, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B44B = subs(B44,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
B54B = subs(B54,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,1,0,0,0,0,0,0});
%Segment C
subs(B02,
B02C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B12,
B12C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B22,
B22C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B32,
B32C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B42,
B42C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B52,
B52C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B62,
B62C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B72,
B72C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B03,
B03C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
subs(B13,
B13C = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
Approximating Splines ◾ 121
B23C = ubs(B23,
s {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B33C = subs(B33,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B43C =
subs(B43, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B53C = subs(B53,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B63C = subs(B63,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B04C =
subs(B04, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B14C = subs(B14,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B24C = subs(B24,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B34C = subs(B34,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B44C = subs(B44,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
B54C = subs(B54,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,1,0,0,0,0,0});
%Segment D
subs(B02,
B02D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B12,
B12D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B22,
B22D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B32,
B32D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
122 ◾ Fundamentals of Graphics Using MATLAB®
subs(B42,
B42D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B52,
B52D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B62,
B62D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B72,
B72D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B03,
B03D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B13,
B13D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B23,
B23D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B33,
B33D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B43,
B43D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B53,
B53D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B63,
B63D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B04,
B04D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B14,
B14D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B24,
B24D = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,1,0,0,0,0});
subs(B34, {B01,B11,B21,B31,B41,B51,B61,B71},
B34D = {0,0,0,1,0,0,0,0});
ubs(B44, {B01,B11,B21,B31,B41,B51,B61,B71},
B44D = s {0,0,0,1,0,0,0,0});
subs(B54, {B01,B11,B21,B31,B41,B51,B61,B71},
B54D = {0,0,0,1,0,0,0,0});
%Segment E
subs(B02,
B02E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B12,
B12E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B22,
B22E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B32,
B32E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B42,
B42E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B52,
B52E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B62,
B62E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B72,
B72E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B03,
B03E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B13,
B13E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B23,
B23E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B33,
B33E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B43,
B43E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B53,
B53E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B63,
B63E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B04,
B04E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B14,
B14E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B24,
B24E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B34,
B34E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B44,
B44E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
subs(B54,
B54E = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,1,0,0,0});
%Segment F
subs(B02, {B01,B11,B21,B31,B41,B51,B61,B71},
B02F = {0,0,0,0,0,1,0,0});
subs(B12, {B01,B11,B21,B31,B41,B51,B61,B71},
B12F = {0,0,0,0,0,1,0,0});
subs(B22, {B01,B11,B21,B31,B41,B51,B61,B71},
B22F = {0,0,0,0,0,1,0,0});
Approximating Splines ◾ 123
B32F = ubs(B32,
s {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B42F = subs(B42,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B52F =
subs(B52, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B62F = subs(B62,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B72F = subs(B72,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B03F =
subs(B03, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B13F = subs(B13,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B23F = subs(B23,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B33F = subs(B33,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B43F = subs(B43,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B53F = subs(B53,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B63F = subs(B63,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B04F = subs(B04,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B14F = subs(B14,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B24F = subs(B24,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B34F = subs(B34,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
B44F =
subs(B44, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
124 ◾ Fundamentals of Graphics Using MATLAB®
B54F = subs(B54,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,1,0,0});
%Segment G
subs(B02,
B02G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B12,
B12G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B22,
B22G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B32,
B32G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B42,
B42G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B52,
B52G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B62,
B62G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B72,
B72G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B03,
B03G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B13,
B13G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
subs(B23,
B23G = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B33G = ubs(B33,
s {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B43G = subs(B43,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B53G =
subs(B53, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B63G = subs(B63,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B04G = subs(B04,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B14G =
subs(B14, {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B24G = subs(B24,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B34G = subs(B34,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B44G = subs(B44,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
B54G = subs(B54,
{B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,1,0});
%Segment H
subs(B02,
B02H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B12,
B12H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B22,
B22H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B32,
B32H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B42,
B42H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B52,
B52H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B62,
B62H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B72,
B72H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B03,
B03H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B13,
B13H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B23,
B23H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B33,
B33H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B43,
B43H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B53,
B53H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B63,
B63H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B04,
B04H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B14,
B14H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B24,
B24H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
subs(B34,
B34H = {B01,B11,B21,B31,B41,B51,B61,B71},
{0,0,0,0,0,0,0,1});
Approximating Splines ◾ 125
subs(B44, {B01,B11,B21,B31,B41,B51,B61,B71},
B44H = {0,0,0,0,0,0,0,1});
ubs(B54, {B01,B11,B21,B31,B41,B51,B61,B71},
B54H = s {0,0,0,0,0,0,0,1});
B04 =
[B04A, B04B, B04C, B04D, B04E, B04F, B04G, B04H]; B04 = simplify(B04)
B14 = [B14A,
B14B, B14C, B14D, B14E, B14F, B14G, B14H]; B14 = simplify(B14)
B24 = [B24A,
B24B, B24C, B24D, B24E, B24F, B24G, B24H]; B24 = simplify(B24)
B34 = [B34A,
B34B, B34C, B34D, B34E, B34F, B34G, B34H]; B34 = simplify(B34)
B44 = [B44A,
B44B, B44C, B44D, B44E, B44F, B44G, B44H]; B44 = simplify(B44)
fprintf('\n');
fprintf('\n');
x = subs(P, ([P0, P1, P2, P3, P4]), ([x0, x1, x2, x3, x4])); x = simplify(x)
y = subs(P, ([P0, P1, P2, P3, P4]), ([y0, y1, y2, y3, y4])); y = simplify(y)
%plotting BFs
figure,
plot(tta, B04aa, 'k-', ttb, B04bb, 'k--', ttc, B04cc, 'k-.', ttd, ...
B04dd, 'b-', tte, B04ee, 'b--', ttf, B04ff, 'b-.', ttg, B04gg, 'r-', tth, B04hh, 'r--');
hold on;
plot(tta, B14aa, 'k-', ttb, B14bb, 'k--', ttc, B14cc, 'k-.', ttd, ...
B14dd, 'b-', tte, B14ee, 'b--', ttf, B14ff, 'b-.', ttg, B14gg, 'r-', tth, B14hh, 'r--');
plot(tta, B24aa, 'k-', ttb, B24bb, 'k--', ttc, B24cc, 'k-.', ttd, ...
B24dd, 'b-', tte, B24ee, 'b--', ttf, B24ff, 'b-.', ttg, B24gg, 'r-', tth, B24hh, 'r--');
plot(tta, B34aa, 'k-', ttb, B34bb, 'k--', ttc, B34cc, 'k-.', ttd, ...
B34dd, 'b-', tte, B34ee, 'b--', ttf, B34ff, 'b-.', ttg, B34gg, 'r-', tth, B34hh, 'r--');
plot(tta, B44aa, 'k-', ttb, B44bb, 'k--', ttc, B44cc, 'k-.', ttd, ...
B44dd, 'b-', tte, B44ee, 'b--', ttf, B44ff, 'b-.', ttg, B44gg, 'r-', tth, B44hh, 'r--');
xlabel ('t'); ylabel('B'); title('B04 - B14 - B24 - B34 - B44');
legend('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');
hold off;
%plotting curve
xa = x(1); ya = y(1);
xb = x(2); yb = y(2);
xc = x(3); yc = y(3);
xd = x(4); yd = y(4);
xe = x(5); ye = y(5);
xf = x(6); yf = y(6);
xg = x(7); yg = y(7);
xh = x(8); yh = y(8);
X = [x0, x1, x2, x3, x4]; Y = [y0, y1, y2, y3, y4];
figure
subplot(131), plot(tta, xaa, 'k-', ttb, xbb, 'k--', ttc, xcc, 'k-.', ...
ttd, xdd, 'b-', tte, xee, 'b--', ttf, xff, 'b-.', ttg, xgg, 'r-', tth, xhh, 'r--');
xlabel('t'); ylabel('x'); title('t - x'); axis square;
subplot(132), plot(tta, yaa, 'k-', ttb, ybb, 'k--', ttc, ycc, 'k-.', ...
Approximating Splines ◾ 129
ttd, ydd, 'b-', tte, yee, 'b--', ttf, yff, 'b-.', ttg, ygg, 'r-', tth, yhh, 'r--');
xlabel('t'); ylabel('y'); title('t - y'); axis square;
subplot(133), plot(xaa, yaa, 'k-', xbb, ybb, 'k--', xcc, ycc, 'k-.', ...
xdd, ydd, 'b-', xee, yee, 'b--', xff, yff, 'b-.', xgg, ygg, 'r-', xhh, yhh, 'r--');
hold on;
scatter(X, Y, 20, 'r', 'filled');
xlabel('x'); ylabel('y'); title('x - y'); axis square; grid;
axis([-1.5 1.5 -1.5 1.5]);
hold off;
The concepts for open-uniform and non-uniform splines are similar to those discussed for quadratic splines and the reader is expected
to extend the idea for cubic splines.
130 ◾ Fundamentals of Graphics Using MATLAB®
Approximating Splines ◾ 131
3.9 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
3.10 REVIEW QUESTIONS
1. What are main differences between B-splines and Bezier splines?
2. Differentiate between uniform, open-uniform, and non-uniform B-splines
3. What is a KV for a B-spline curve?
4. How are BFs of B-splines calculated, using the Cox de Boor algorithm?
5. Under what conditions can B-splines behave like hybrid splines?
6. What is meant by local control property of a B-spline?
7. Can the number of CPs be changed independent of the degree of B-splines?
8. How does changing the KV affect the spatial B-spline curve?
132 ◾ Fundamentals of Graphics Using MATLAB®
3.11 PRACTICE PROBLEMS
1. Find the equation of a linear uniform B-spline associated with the CPs (1, 0), (−1, 1),
and (1, −1).
2. Derive the BFs of a uniform linear B-spline with d = 2 and n = 3.
3. Derive the BFs of a uniform quadratic B-spline associated with five CPs.
4. A B-spline has degree 2 and is associated with four CPs. Find an expression for the
first BF B03 for the first two curve segments A and B, using Cox de Boor algorithm,
if the KV is of the form T = [0, 0.2, 0.5, 0.7, …].
5. A uniform B-spline has degree 1 and is associated with three CPs P0, P1, and P2.
Derive equation of the second curve segment using Cox de Boor algorithm, if KV is
of the form T = [0, 0.4, 0.5, 0.8, …].
6. A non-uniform B-spline has degree 1 and is associated with three CPs P0, P1, and P2.
Derive equation of the first and second curve segments if the KV is T = [0, 4, 5, 8, 9].
7. Find the equation of a uniform quadratic B-spline having CPs (2, 5), (4, −1), (5, 8), and
(7, −5).
8. Find the equation of a non-uniform quadratic B-spline having CPs (2, 5), (4, −1),
(5, 8), and (7, −5) and a KV T =[0, 4, 5, 8, 9, 13, 15].
9. Find the equations of the first two segments of a uniform cubic B-spline with CPs
(2, 0), (4, 1), (5, 7), (6, −5), and (8, −1).
10. Find the first two BFs of a non-uniform cubic B-spline with d = 4 and n = 4, and KV
T = [0, 1, 2, 5, 6, 8, 10, 12, 13].
Chapter 4
2D Transformations
4.1 INTRODUCTION
Two-dimensional transformations enable us to change the location, orientation and shapes
of splines in 2D space. These transformations are translation, rotation, scaling, reflection,
and shear (Hearn and Baker, 1996) applied individually or in combination of two or more.
Given known coordinates of a point, each of these transformations is represented by a
matrix which when multiplied to the original coordinates gives us a new set of coordinates.
An entire spline is transformed by transforming all the points of the spline. In the following
sections, the transformation matrices will be derived and applied to original points to
give new points. Before that, however, we introduce the concept of homogeneous coor-
dinates, which enable us to represent all types of transformation in a homogeneous or
uniform manner. To calculate the coordinates and directions, we use a 2D right-handed
coordinate system. Although, we have been using coordinates of points from Chapter 1,
now we establish a formal definition of a 2D coordinate system as transformation requires
a rigorous understanding of how distances and angles are calculated and how they are
changed during the course of transformation operations.
The concept of a 2D Cartesian coordinate system is attributed to the 17th century
French mathematician Rene Descartes and is widely used to measure location of a point
on a 2D plane from a reference point called the origin. The location is represented as a
pair of signed distances measured along two mutually perpendicular lines called axes
meeting at the origin. Sometimes, the axes are also referred to as the primary axes or
principal axes to distinguish them from other lines parallel to them. The first axis is usu-
ally depicted as a number line along the horizontal direction and called the X-axis while
the second axis as a number line along the vertical direction and called the Y-axis (see
Figure 4.1). Distance of a point on the plane measured along the X-axis from the ori-
gin is called the x-coordinate or abscissa and distance along the Y-axis is called the
y-coordinate or ordinate. The location of a point is, therefore, represented as a pair of
ordered numbers (x, y) called its coordinates. Since all distances are measured from the
origin, the origin itself has coordinates (0, 0) and denoted by O. In most cases, the origin
is visualized as a point in the center of the paper with the x-coordinates being measured
133
134 ◾ Fundamentals of Graphics Using MATLAB®
positive toward the right and negative toward the left, and the y-coordinates being posi-
tive toward the top and negative toward the bottom. However in the context of a com-
puter display, the origin is usually located at the lower-left corner of the screen with the
axes toward the right and top, while for some cases the origin is also depicted at the upper
left of the screen with the axes toward the right and bottom of the screen. Since the axes
are perpendicular to each other the system is often referred to as rectangular coordinate
system or orthogonal coordinate system. Numbers measured along the axes can in gen-
eral be floating point numbers although in some cases like measuring pixel dimensions
of images on screen, the numbers are thought to be only integers. The two axes divide
the plane into four parts called quadrants such that in the first quadrant (Q1) both x and
y are positive, in the second quadrant (Q2), x is negative, in the third quadrant (Q3) both
are negative, and in the fourth quadrant (Q4), y is negative. To fix the orientation of the
axes with respect to one another a convention called the right-hand rule is often used. If
the first finger and the second finger of the right-hand are stretched out at right angles to
each other then the direction from the hand to the tip of the finger would represent the
positive x and positive y directions, respectively. The angle of rotation around a primary
axis is considered positive when in counter-clockwise (CCW) direction when seen from
the tip of the axis toward the origin. Another way to determine this is to use the right-
handed convention: with the thumb of the right-hand pointing toward the positive end
of an axis (away from the origin) the direction of curvature of the fingers indicate the
positive direction of rotation around that axis (O’Rourke, 2003).
2D Coordinate System
5
4 (-2,4) (2,4)
3 y=(1/2)x 3
Q2 Q1
2
1
Y-axis
0
O (0,0)
-1
-2
Q3 Q4
-3
-4 (-2,-4) (2,-4)
-5
-5 -4 -3 -2 -1 -1 -1 -1 -1 -1 -1
X-axis
A Cartesian coordinate system plays one of the most important roles in graphics since it
enables representation of spline curves as a series of coordinate points e.g. a spline y = 0.5 x 3
can be described by the vector containing all points whose coordinates satisfy the equa-
tion. Moreover, the number of points and the gap between them can be adjusted according
to the resolution of the display system to always produce a smooth curve. This has led to
the growth of vector graphics.
4.2 HOMOGENEOUS COORDINATES
A point P ( x1 , y1 ) when translated by amounts (t x , t y ) has new coordinates Q ( x 2 , y 2 ) given
by (Foley et al., 1995):
x 2 = x1 + t x
y 2 = y1 + t y
x2 1 0 x1 tx
= + (4.1)
y 2 0 1 y1 t
y
Now for two main reasons, we would prefer a slightly different form of representation of
the transformation matrix than that shown above. The first reason is that when multiple
transformations are involved one after another we would prefer a multiplicative form of
the matrix rather than the additive form. This would enable us to multiply all the trans-
formations together and calculate the final coordinates at the end, rather than calculate
intermediate coordinates after each step. The product of two or more transformation
matrices is known as the “composite transformation” matrix, and provides the net effect
of multiple transformations within a single matrix (Hearn and Baker, 1996). The second
reason is that we would prefer a square transformation matrix whose inverse would give us
the “inverse transformation.” For these reasons, we use the form in Equation (4.2), which
is called “homogeneous coordinates” (Hearn and Baker, 1996) in contrast to Equation (4.1)
called “Cartesian coordinates.” To generate the square matrix, a third row is included,
which is typically ignored after the new coordinates are computed.
x2 1 0 t x x1
y2 = 0 1 t y y1
1 0 0 1 1
Since the matrices have three rows, the homogeneous coordinates are referred to as
( x , y , h), which implies Cartesian coordinates of ( x /h, y /h). In most cases h = 1 so the val-
ues are equal, but in some cases when h is not 1, we would need conversion from one sys-
tem to another. Such examples are also included in this book. The transformation matrices
for each type of transformation operation will now be dealt with in more details.
136 ◾ Fundamentals of Graphics Using MATLAB®
4.3 TRANSLATION
A point P ( x1 , y1 ) when translated by amounts (t x , t y ) has new coordinates Q ( x 2 , y 2 )
given by:
x2 1 0 t x x1
y2 = 0 1 t y y1 (4.2)
1 0 0 1 1
The inverse transformation is computed by taking the inverse of the matrix as below:
−1
x1 1 0 tx x2
y1 = 0 1 t y y2 (4.3)
1 0 0 1 1
It can be verified that the inverse of the matrix is equal to the negative of the arguments.
−1
1 0 tx 1 0 −t x
0 1 ty = 0 1 −t y (4.4)
0 0 1 0 0 1
Symbolically, if T denotes the forward translation operation with arguments (t x , t y ) and T′
denotes the reverse translation then the above can be written as:
T ′ (t x , t y ) = T ( −t x , −t y )
This is the convention followed throughout this book i.e. the operations themselves would
be denoted by single letters such as T, S, R, and so on for translation, scaling, and rotation
while a specific transformation matrix would be denoted with a letter with a subscript. For
example
1 0 3
T1 = T (3, −4) = 0 1 −4
0 0 1
Example 4.1
A square having vertices (0, 0), (1, 0), (1, 1), and (0, 1) is translated by amounts
(−3, 4). Find its new vertices.
0 1 1 0
Original coordinate matrix: C = 0 0 1 1
1 1 1 1
2D Transformations ◾ 137
0 1 −3
Translation matrix: T1 = T (−3, 4) = 0 0 4
0 0 1
−3 −2 −2 −3
New coordinate matrix: D = T1 * C = 4 4 5 5
1 1 1 1
New vertex coordinates are (−3, 4), (−2, 4), (−2, 5), and (−3, 5) (Figure 4.2)
6
original
new
4
0
y
-2
-4
-6
-6 -4 -2 0 2 4 6
x
%plotting
plot(C(1,:), C(2,:), 'b--', D(1,:),D(2,:), 'r');
xlabel('x');
ylabel('y');
legend('original', 'new');
axis([-6, 6, -6, 6]);
grid;
NOTE
for: initiates a for loop for printing out all the vertices
4.4 SCALING
A scaling operation alters the size of graphic objects by multiplying the X- and Y-coordinates
of each point of the object by scaling factors sx and sy. If scaling factors are less than 1, they
reduce the size of the object; if they are more than 1, they increase the size; and if they
are equal to 1, they keep the size unaltered. If the factors are positive, the size increases
along the original direction of coordinate axes; if they are negative, the coordinate signs
are flipped. If sx and sy are equal then scaling is uniform otherwise non-uniform.
A point P ( x1 , y1 ) when scaled by amounts ( s x , s y ) has new coordinates Q ( x 2 , y 2 )
given by:
x2 sx 0 0 x1
y2 = 0 sy 0 y1 (4.5)
1 0 0 1 1
It can be verified that the inverse of the matrix is equal to the reciprocal of the arguments.
−1
sx 0 0 1 sx 0 0
0 sy 0 = 0 1 sy 0 (4.6)
0 0 1 0 0 1
If S denotes the forward scaling operation with arguments ( s x , s y ) and S′ denotes the
reverse scaling then the above can be written as:
S′ ( s x , s y ) = S (1 s x , 1 s y )
Note that the scaling operation pertaining to the above matrix is always with respect to
the origin.
Example 4.2
A quadrilateral having vertices (−1, −1), (1, −2), (1, 2), (−1, 1) is scaled by amounts
(−2, 3). Find its new vertices.
2D Transformations ◾ 139
−1 1 1 −1
Original coordinate matrix: C = −1 −2 2 1
1 1 1 1
−2 0 0
Scaling matrix: S1 = S(−2, 3) = 0 3 0
0 0 1
2 −2 −2 2
New coordinate matrix: D = S1 ⋅ C = −3 −6 6 3
1 1 1 1
New vertex coordinates are (2, −3), (−2, −6), (−2, 6), and (2, 3) (Figure 4.3)
6
original
new
0
y
-2
-4
-6
-6 -4 -2 0 2 4 6
x
%plotting
plot(C(1,:), C(2,:), 'b--', D(1,:),D(2,:), 'r');
xlabel('x');
ylabel('y');
legend('original', 'new');
axis([-6, 6, -6, 6]); grid;
Note that here the center of the square is at point (0, 0) so the scaling is uniform with
respect to the origin.
4.5 ROTATION
A rotation operation moves a point along the circumference of a circle centered at the
origin and radius equal to the distance of the point from the origin. Rotation is considered
positive when it is in the CCW direction and negative along the clockwise (CW) direction.
A point P ( x1 , y1 ) when rotated by angle (θ) has new coordinates Q ( x 2 , y 2 ) given by
(Figure 4.4):
x2 cosθ − sinθ 0 x1
y2 = sinθ cosθ 0 y1 (4.7)
1 0 0 1 1
3.5
P(x2, y2)
3
2.5
P(x1, y1)
2 r
Y
1.5
1 r
0.5
0
0 1 2 3 4 5
O
X
To arrive at the above expression let angle between OP and X-axis be φ and r be the
length of OP.
It can be verified that the inverse of the matrix is equal to the negative of the argument,
remembering that cos(−θ ) = cos(θ ).
−1
cosθ − sinθ 0 cosθ sinθ 0
sinθ cosθ 0 = − sinθ cosθ 0 (4.8)
0 0 1 0 0 1
If R denotes the forward rotation operation with arguments (θ) and R′ denotes the reverse
rotation then the above can be written as:
R′(θ ) = R(−θ )
Note that the rotation operation pertaining to the above matrix is always with respect to
the origin.
Example 4.3
A square having vertices (−1, −1), (1, −1), (1, 1), and (−1, 1) is rotated by angle 30°
around the origin along CCW direction. Find its new vertices.
−1 1 1 −1
Original coordinate matrix: C = −1 −1 1 1
1 1 1 1
cos30 − sin30 0 0.87 −0.5 0
Rotation matrix: R1 = R(30) = sin30 cos30 0 = 0.5 0.87 0
0 0 1 0 0 1
−0.37 1.37 −0.37
1.37
New coordinate matrix: D = R1 ⋅ C = −1.37 −0.37 1.37 0.37
1 1 1 1
New vertex coordinates are (−0.37, −1.37), (1.37, −0.37), (0.37, 1.37), and (−1.37, 0.37)
(Figure 4.5)
142 ◾ Fundamentals of Graphics Using MATLAB®
4
original
new
3
0
y
-1
-2
-3
-4
-4 -3 -2 -1 0 1 2 3 4
x
NOTE
Here, the center of the square is at point (0, 0) so the rotation is with respect to the origin.
cos: calculates cosine of an angle in radians
deg2rad: converts degree to radian values
2D Transformations ◾ 143
4.6 FIXED-POINT SCALING
For a general fixed-point scaling with respect to a fixed point ( x f , y f ), the following steps
are taken:
Example 4.4
A square having vertices (−1, −1), (1, −1), (1, 1), and (−1, 1) is scaled by
amounts (−2, 3) about one of its vertices (−1, −1). Find its new vertices.
−1 1 1 −1
Original coordinate matrix: C = −1 −1 1 1
1 1 1 1
0 1 1
Forward translation matrix: T1 = T (1, 1) = 0 0 1
0 0 1
−2 0 0
Scaling matrix: S1 = S(−2, 3) = 0 3 0
0 0 1
0 1 −1
Reverse translation matrix: T2 = T (−1, −1) = 0 0 −1
0 0 1
−2 0 −3
Composite transformation matrix: M = T2 ⋅ S1 ⋅T1 = 0 3 2
0 0 1
−1 −5 −5 −1
New coordinate matrix: D = M ⋅ C = −1 −1 5 5
1 1 1 1
New vertex coordinates are (−1, −1), (−5, −1), (−5, 5), and (−1, 5) (Figure 4.6)
144 ◾ Fundamentals of Graphics Using MATLAB®
8
original
new
6
0
y
-2
-4
-6
-8
-8 -6 -4 -2 0 2 4 6 8
x
%plotting
plot(C(1,:), C(2,:), 'b--', D(1,:),D(2,:), 'r', xf, yf, 'ro');
xlabel('x');
ylabel('y');
2D Transformations ◾ 145
legend('original', 'new');
axis([-8, 8, -8, 8]);
axis square; grid;
4.7 FIXED-POINT ROTATION
For a general fixed-point rotation with respect to a fixed point ( x f , y f ), the following steps
are taken (Foley et al., 1995):
Example 4.5
A square having vertices (−1, −1), (1, −1), (1, 1), and (−1, 1) is rotated by angle 30°
along CCW direction about one of its vertices (−1, −1). Find its new vertices.
−1 1 1 −1
Original coordinate matrix: C = −1 −1 1 1
1 1 1 1
0 1 1
Forward translation matrix: T1 = T (1, 1) = 0 0 1
0 0 1
cos30 − sin30 0 0.87 −0.5 0
Rotation matrix: R1 = R(30) = sin30 cos30 0 = 0.5 0.87 0
0 0 1 0 0 1
0 1 −1
Reverse translation matrix: T2 = T (−1, −1) = 0 0 −1
0 0 1
0.87 −0.5 −0.63
Composite transformation matrix: M = T2 ⋅ R1 ⋅T1 = 0.5 0.87 0.37
0 0 1
−1 0.73 −0.27 −2
New coordinate matrix: D = M ⋅ C = −1 0 1.73 0.73
1 1 1 1
New vertex coordinates are (−1, −1), (0.73, 0), (−0.27, 1.73), and (−2, 0.73)
(Figure 4.7)
146 ◾ Fundamentals of Graphics Using MATLAB®
4
original
new
3
0
y
-1
-2
-3
-4
-4 -3 -2 -1 0 1 2 3 4
x
%plotting
plot(C(1,:), C(2,:), 'b--', D(1,:),D(2,:), 'r', xf, yf, 'ro');
xlabel('x');
ylabel('y');
legend('original', 'new');
axis([-4, 4, -4, 4]);
axis square; grid;
2D Transformations ◾ 147
NOTE
cosd: calculates cosine of an angle in degrees
sind: calculates sine of an angle in degrees
4.8 REFLECTION
A reflection operation about an axis reverses the sign of the coordinate perpendicular to
the axis. Thus, for example a reflection about the X-axis reverses the y-coordinate of a
point. The reflection matrices, therefore, are given by the following, where the subscript
denotes the axis about which the reflection takes place:
1 0 0
Fx = 0 −1 0 (4.9)
0 0 1
−1 0 0
Fy = 0 1 0 (4.10)
0 0 1
A reflection can also take place about the origin in which case both the x- and
y-coordinates gets reversed. This is indicated by the subscript “o.”
−1 0 0
Fo = 0 −1 0 (4.11)
0 0 1
Example 4.6
A triangle having vertices at (1, 2), (3, 2), and (3, 4) is reflected about the X-axis,
Y-axis, and the origin. Find its new coordinates.
1 3 3
Original coordinate matrix: C = 2 2 4
0 0 1
1 0 0
For reflection about X-axis: Fx = 0 −1 0
0 0 1
1 3 3
New coordinate matrix: Dx = Fx ⋅ C = − 2 −2 −4
0 0 1
148 ◾ Fundamentals of Graphics Using MATLAB®
−1 0 0
For reflection about Y-axis: Fy = 0 1 0
0 0 1
−1 −3 −3
New coordinate matrix: D y = Fy ⋅ C = 2 2 4
0 0 1
−1 0 0
For reflection about origin: Fo = 0 −1 0
0 0 1
−1 −3 −3
New coordinate matrix: Do = Fo ⋅ C = −2 −2 −4 (Figure 4.8)
0 0 1
5
original
Fx
4
Fy
Fo
3
0
y
-1
-2
-3
-4
-5
-5 0 5
x
Fx = [1 0 0; 0 -1 0; 0 0 1];
Fy = [-1 0 0; 0 1 0; 0 0 1];
Fo = [-1 0 0; 0 -1 0; 0 0 1];
Dx = Fx*C;
Dy = Fy*C;
Do = Fo*C;
%plotting
plot(C(1,:), C(2,:), 'b--', Dx(1,:),Dx(2,:), 'r', ...
Dy(1,:), Dy(2,:), 'r--', Do(1,:), Do(2,:), 'r-.');
xlabel('x'); ylabel('y');
axis([-5, 5, -5, 5]);
legend('original', 'Fx', 'Fy', 'Fo');
axis square; grid;
4.9 FIXED-LINE REFLECTION
Transformation matrix for reflection about the fixed line L: y = mx + c is obtained using
following steps (Chakraborty, 2010):
1. Intercept point of L with Y-axis is (0, c). Translate point to origin: T1 = T (0, −c )
2. Rotate L about origin by −θ, where θ = arctan(m): R1 = R(θ)
3
(0,c) = atan(m)
2 y = mx + c
0
Y
O
-1
-2
-3
-4
-5
-5 -4 -3 -2 -1 0 1 2 3 4 5
X
Example 4.7
A triangle having vertices (2, 4), (4, 7), and (5, 6) is reflected about the line
y = 0.5x + 2. Find its new coordinates.
2 4 5
Original coordinate matrix: C = 4 7 6
0 0 1
For given line L, we have m = 0.5, c = 2, k = arctan(m) = 0.463 radians = 26.56°
1 0 0
Forward translation matrix: T1 = T (0, −c ) = 0 1 −2
0 0 1
cos k − sin k 0
Forward rotation matrix: R1 = R(k ) = sin k cos k 0
0 0 1
0.89 0.45 0
= −0.45 0.89 0
0 0 1
1 0 0
Reflection about X-axis: Fx = 0 −1 0
0 0 1
2D Transformations ◾ 151
10
original
9 new
5
y
0
0 2 4 6 8 10
x
cos k sin k 0
Reverse rotation matrix: R2 = R(− k ) = − sin k cos k 0
0 0 1
0.89 −0.45 0
= 0.45 0.89 0
0 0 1
1 0 0
Reverse translation matrix: T2 = T (0, c ) = 0 1 2
0 0 1
New vertex coordinates are (2.8, 2.4), (6.4, 2.2), and (6.2, 3.6) (Figure 4.10).
152 ◾ Fundamentals of Graphics Using MATLAB®
%plotting
xx = linspace(0,10);
yy = m*xx + c;
plot(C(1,:),C(2,:), 'b--', D(1,:), D(2,:), 'r');
hold on;
plot(xx, yy, 'b-', 'LineWidth', 1.5);
legend('original', 'new'); axis([0, 10, 0, 10]);
grid; axis square;
xlabel('x'); ylabel('y');
hold off;
NOTE
atan: computes inverse tangent in radian values
4.10 SHEAR
A shear operation distorts a graphic object by changing a set of coordinate values while
keeping other values constant (Chakraborty, 2010). There can be two types of shear: one
along the X-direction and the other along the Y-direction. For an X-direction, shear the
x-coordinates of points are shifted in value by an amount proportional to their y-coordinates,
while the y-coordinates themselves remain constant (see Figure 4.11a).
It is evident here that: x 2 = x1 + h ⋅ y1, where h is a constant of proportionality. This means
that larger is the y-coordinate of a point more is its shift along the x-coordinate. Since the
y-coordinates remain unchanged, y 2 = y1. In matrix form:
2D Transformations ◾ 153
1 1
y
y
0 0
-1 -1
-1 0 1 2 3 -1 0 1 2 3
x x
1 h 0
Hx = 0 1 0 (4.12)
0 0 1
1 0 0
Hy = h 1 0 (4.13)
0 0 1
Example 4.8
A square having vertices at (−1, −1), (1, −1), (1, 1), and (−1, 1) is subjected to a shear
of 1.5 along the X-axis and then a shear of 2 along the y-axis. Find its new vertices.
−1 1 1 −1
Original coordinate matrix: C = −1 −1 1 1
1 1 1 1
1 1.5 0
Shear along X-direction: H1 = H x (1.5) = 0 1 0
0 0 1
154 ◾ Fundamentals of Graphics Using MATLAB®
6
original
new
4
0
y
-2
-4
-6
-6 -4 -2 0 2 4 6
x
1 0 0
Shear along Y-direction: H 2 = H y (2) = 2 1 0
0 0 1
New coordinate matrix: D = H 2 ⋅ H1 ⋅ C
New vertex coordinates are (−2.5, −6), (−0.5, −2), (2.5, 6), and (0.5, 2) (Figure 4.12)
ylabel('y');
legend('original', 'new');
axis equal; grid;
4.11 AFFINE TRANSFORMATIONS
A composite transformation involving a combination of translation, rotation, scaling, and
shear is referred to as affine transformation (Hearn and Baker, 1996; Shirley, 2002). An affine
transformation preserves the following property: if P was an arbitrary point located on the
line segment joining endpoints A and B before the transformation, then after the trans-
formation the transformed point P′ will still be located on the line segment joining the
transformed endpoints A′ and B′. In general, a rectangle under affine t ransformation will
be converted to a parallelogram (Rovenski, 2010).
Recalling that,
1 0 tx
T= 0 1 t y
0 0 1
sx 0 0
S= 0 sy 0
0 0 1
cosθ − sinθ 0
R = sinθ cosθ 0
0 0 1
1 hx 0
H = hy 1 0
0 0 1
Thus under affine transformation, the transformed coordinates x′, y′ of a point are related
to the original coordinates x, y by the general relation of the form:
x ′ = ax + by + c
(4.15)
y ′ = dx + ey + f
Reflection can also be added to the set of transformations, which will simply change the
sign of some of the coefficients depending on the type of reflection.
Example 4.9
Find the new coordinates of a square with vertices (1, 1), (1, −1), (−1, −1), and
(−1, 1) under the composite transformations involving the following: T(2, −4),
S(3, −1), R(π/2), and H(1, −2).
Here,
1 0 2
T= 0 1 −4
0 0 1
3 0 0
S= 0 −1 0
0 0 1
cos π /2 − sin π /2 0 0 −1 0
R = sin π /2 cos π /2 0 = 1 0 0
0 0 1 0 0 1
1 1 0
H = −2 1 0
0 0 1
6 −3 2
Composite transformation matrix: M = −1 −1 −4
0 0 1
−1 1 1 −1
Original coordinate matrix: C = −1 −1 1 1
1 1 1 1
2D Transformations ◾ 157
4 original
new
-2
y
-4
-6
-8
-6 -4 -2 0 2 4 6 8 10
x
−1 11 5 −7
New coordinate matrix: D = M ⋅ C = −2 −4 −6 −4
1 1 1 1
New vertex coordinates are (−1, −2), (11, −4), (5, −6), and (−7, −4) (Figure 4.13).
MATLAB Code 4.9
clear all; clc;
syms tx ty sx sy A hx hy ;
X = [-1 1 1 -1 -1];
Y = [-1 -1 1 1 -1];
C = [X ; Y ; 1 1 1 1 1];
M = T*S*R*H;
M1 = subs(M, [tx, ty, sx, sy, A, hx, hy], [2, -4, 3, -1, pi/2, 1, -2]);
D = M1 * C;
158 ◾ Fundamentals of Graphics Using MATLAB®
%plotting
plot(C(1,:), C(2,:), 'b--', D(1,:),D(2,:), 'r');
xlabel('x');ylabel('y');
legend('original', 'new');
axis equal; grid;
2D Transformations ◾ 159
4.12 PERSPECTIVE TRANSFORMATIONS
We have seen above that the affine transformation matrix described by Equation (4.14)
converts a rectangle into a parallelogram and is described as below, where (x′, y′) are the
transformed coordinates of the point (x, y).
x' a b c x
y' = d e f y (4.16)
1 0 0 1 1
The mapping relations between the old and new coordinates are given by Equation
(4.15). During this transformation, a set of constraints are applied, which is r esponsible
for the parallelogram shape. If a rectangle has a new set of vertices with coordinates
( x0 , y0 ) , ( x1 , y1 ) , ( x 2 , y2 ) , and ( x3 , y3 ) then the following constraints are applied:
( x1 − x 0 ) = ( x 2 − x 3 ) and ( y1 − y0 ) = ( y 2 − y3 ). These constraints simply state the lengths of
the opposite sides should be equal and this forces the new figure to be a p arallelogram.
However, if the constraints are not applied then a rectangle will be converted to an
arbitrary quadrilateral and the corresponding transformation does not remain affine
any more (Rovenski, 2010). The new transformation is called a perspective (or projective)
transformation and is described by the transformation matrix shown below:
x′ a b c x
y′ = d e f y (4.17)
w g h 1 1
where x′ and y′ are in homogeneous coordinates. The Cartesian coordinates are X = x ′ /w
and Y = y ′ /w. From Equation (4.17), we can derive the mapping relations between the old
and new coordinates:
x ′ ax + by + c
X= =
w gx + hy + 1
(4.18)
y ′ dx + ey + f
Y= =
w gx + hy + 1
Example 4.10
Find the new coordinates of a square with vertices (0,0), (1,0), (1,1), and (0,1)
5 2 5 5 2 5
under the following transformations: (a) 2 5 5 and (b) 2
5 5 .
0 0 1 5 2 1
Specify the type of transformation in each case.
160 ◾ Fundamentals of Graphics Using MATLAB®
(a)
0 1 1 0
Original coordinate matrix: C = 0 0 1 1
1 1 1 1
5 2 5
Transformation matrix: M = 2 5 5
0 0 1
5 10 12 7
New coordinate matrix: D = M ⋅ C = 5 7 12 10
1 1 1 1
New vertex coordinates are (5.00, 5.00), (10.00, 7.00), (12.00, 12.00), and (7.00, 10.00)
Let x 0 = 5, y0 = 5, x1 = 10, y1 = 7, x 2 = 12, y 2 = 12, x 3 = 7, y3 = 10
Now d1 = x1 − x 0 = 5, d2 = x 2 − x 3 = 5, d3 = y1 − y0 = 2, d4 = y 2 − y3 = 2
Since d1 = d2 and d3 = d4, the transformation is affine in nature (Figure 4.14a)
(b)
5 2 5
Transformation matrix: M = 2 5 5
5 2 1
5 10 12 7
New coordinate matrix: Dh = M ⋅ C = 5 7 12 10 (in homogeneous
coordinates) 1 6 8 3
5 1.67 1.5 2.33
New coordinate matrix: D = 5 1.17 1.5 3.33 (in Cartesian coordinates)
1 1 1 1
New vertex coordinates are (5.00, 5.00), (1.67, 1.17), (1.50, 1.50), and (2.33, 3.33)
Let x 0 = 5, y0 = 5, x1 = 1.67, y1 = 1.17, x 2 = 1.5, y 2 = 1.5, x 3 = 2.33, y3 = 3.33
Now d1 = x1 − x 0 = −3.33, d2 = x 2 − x 3 = −0.83, d3 = y1 − y0 = −3.83, d4 = y 2 − y3 = −1.83
Since d1 ≠ d2 and d3 ≠ d4, the transformation is perspective in nature (Figure 4.14b).
% (a)
C = [0 1 1 0 0 ; 0 0 1 1 0 ; 1 1 1 1 1]
M = [5 2 5 ; 2 5 5 ; 0 0 1]
D = M*C
2D Transformations ◾ 161
(a)
affine transformation
12
original
new
10
0
0 2 4 6 8 10 12
3.5
2.5
1.5
0.5
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
FIGURE 4.14 Plots for Example 4.10. (a) Affine transformation and (b) perspective transformation.
162 ◾ Fundamentals of Graphics Using MATLAB®
figure,
plot (C(1,:), C(2,:), 'b--', D(1,:), D(2,:), 'r-'); grid;
legend('original', 'new');
title('affine transformation');
x0 = D(1,1); y0 = D(2,1);
x1 = D(1,2); y1 = D(2,2);
x2 = D(1,3); y2 = D(2,3);
x3 = D(1,4); y3 = D(2,4);
d1 = x1 - x0, d2 = x2 - x3,
d3 = y1 - y0, d4 = y2 - y3,
if d1 = = d2 && d3 = = d4
fprintf('Transformation is affine\n');
else
fprintf('Transformation is perspective\n');
end
fprintf('\n\n');
% (b)
M = [5 2 5 ; 2 5 5 ; 5 2 1]
Dh = M*C;
for i=1:length(Dh)
D(:,i) = Dh(:,i)/Dh(3,i);
end
figure
plot (C(1,:), C(2,:), 'b--', D(1,:), D(2,:), 'r-'); grid;
legend('original', 'new');
title('perspective transformation');
x0 = D(1,1); y0 = D(2,1);
x1 = D(1,2); y1 = D(2,2);
2D Transformations ◾ 163
x2 = D(1,3); y2 = D(2,3);
x3 = D(1,4); y3 = D(2,4);
d1 = x1 - x0, d2 = x2 - x3,
d3 = y1 - y0, d4 = y2 - y3,
if d1 = = d2 && d3 = = d4
fprintf('Transformation is affine\n');
else
fprintf('Transformation is perspective\n');
end
4.13 VIEWING TRANSFORMATIONS
Viewing transformations are associated with displaying rendered graphics output on a
display device. It has already been mentioned that 2D graphics objects like splines and
polygons are created on a plane and stored using a coordinate system. The coordinate
system is used to measure location of points and store them in vectors. The creation
plane can extend on all sides indefinitely, limited only by the hardware resources. When
a portion of this plane is to be displayed on the output devices, the graphics display sys-
tem needs two additional functional components to make this possible: the window and
the viewport. The window is defined by the four corners of a rectangular area using the
coordinate system of the graphics application software, to select a particular portion of
the creation plane that needs to be displayed. The viewport is a mapped version of the
window to the coordinates of the output device like a monitor, to enable the hardware
to display the stored graphics data on screen. The viewport is also defined by the four
corners of a rectangular area but uses the coordinate system of the display device. The
mapping between the window and the viewport is collectively referred to as “viewing
transformations” (Hearn and Baker, 1996; Shirley, 2002) and involve both translation and
scaling (see Figure 4.15).
yvmax Viewport
30 15
ywmax Window
20
10
(xw, yw) (xv, yv)
10
ywmin
xwmin xwmax 5
0
yvmin
-10 0
-100 10 20 30 05 10 15
xvmin xvmax
The left-hand figure shows a spline generated using application specified coordinates,
and a rectangular window on it with its vertices defined as
The contents inside the window is mapped onto a device viewport showed on the right
side with its vertices defined as
The mapping from the window to the viewport involve the following steps (Foley et al.,
1995):
The scaling factors involve changing the dimension of the window to the viewport. The
width of the viewport is ( xv max − xv min ) and the width of the window is ( xw max − xw min ).
The horizontal scaling factor sx will be a ratio of these i.e.
xv max − xv min
sx = (4.19)
xw max − xw min
The height of the viewport is ( yv max − yv min ) and the height of the window is ( yw max − yw min ) .
The vertical scaling factor sy will be a ratio of these i.e.
yv max − yv min
sy = (4.20)
yw max − yw min
Example 4.11
Obtain a transformation that maps a window whose lower-left corner
is at A(1, 1) and upper-right corner at C(3, 5) onto a viewport that has
a lower-left corner at P(0, 0) and upper-right corner at Q(0.5, 0.5).
Translate A to P: T1 = T (−1, −1)
Apply scaling about P, since P is already at the origin: S1 = S ( s x , s y )
2D Transformations ◾ 165
0.25 0 −0.25
Composite transformation M = S1 ⋅T1 = 0 0.125 −0.125
0 0 1
xwmin=1;
ywmin=1;
xwmax=3;
ywmax=5;
xvmin=0;
yvmin=0;
xvmax=0.5;
yvmax=0.5;
tx = xvmin-xwmin;
ty = yvmin-ywmin;
T1 = [1 0 tx; 0 1 ty; 0 0 1];
M = S1*T1
xv − xv min xv − xv min
= max (4.21)
xw − xw min xw max − xw min
166 ◾ Fundamentals of Graphics Using MATLAB®
Example 4.12
A user works on a coordinate system, which is a square window with corner
coordinates P(−2, −5) and Q(8, 5). The user’s area is mapped to a square
viewport whose corner coordinates are P′(400, 500) and Q′(600, 800).
(a) Find the window to viewport transformation matrix and (b) find out
the origin of the user’s coordinate system after mapping to the viewport.
(a)
Here, xwmin = −2, ywmin = −5, xwmax = 8, ywmax = 5, xvmin = 400, yvmin = 500, xvmax =
600, yvmax = 800
sx = (xvmax − xvmin)/(xwmax − xwmin) = 20
sy = (yvmax − yvmin)/(ywmax − ywmin) = 30
Forward translation T1 = T(xvmin − xwmin, yvmin − ywmin)
Translation to origin T2 = T(−xvmin, −yvmin)
Scale about origin S1 = S(sx, sy)
Reverse translation T3 = T(xvmin, yvmin)
20 0 440
Composite transformation: M = T3 * S1 * T2 * T1 = 0 30 650
0 0 1
Verification: M*P = P′, M*Q = Q′
(b)
Here, xw = 0, yw = 0, xv = ?, yv = ?
From Equation (4.18)
xv = xvmin + (xw − xwmin)*sx = 440
yv = yvmin + (yw − ywmin)*sy = 650
Thus, point (0, 0) of the window maps to point (440, 650) of the viewport.
xwmin = -2;
ywmin = -5;
xwmax = 8;
ywmax = 5;
2D Transformations ◾ 167
xvmin = 400;
yvmin = 500;
xvmax = 600;
yvmax = 800;
sx = (xvmax - xvmin)/(xwmax - xwmin);
sy = (yvmax - yvmin)/(ywmax - ywmin);
T1 = [1, 0, xvmin - xwmin ; 0, 1, yvmin - ywmin ; 0, 0, 1];
T2 = [1, 0, 0 - xvmin ; 0, 1, 0 - yvmin ; 0, 0, 1];
S1 = [sx, 0, 0 ; 0, sy, 0 ; 0, 0, 1];
T3 = [1, 0, xvmin ; 0, 1, yvmin ; 0, 0, 1];
M = T3 * S1 * T2 * T1
xw = 0; yw = 0;
xv = xvmin + (xw - xwmin)*sx; xv = round(xv)
yv = yvmin + (yw - ywmin)*sy; yv = round(yv)
4.5
3.5 Y X
2.5
y
2
(x0, y0)
1.5
0.5
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
O x
Example 4.13
Find the equation of a straight line y = 2x + 3 in the X–Y coordinate system,
which results from a 90° rotation of the x–y coordinate system.
Original equation: y = mx + c
Two points on the original line A(0, c) and B(−c/m, 0)
To transform from x–y to X–Y system, the latter has to be rotated by −90° to
coincide with x–y system
Hence, rotation matrix is R1 = R(−90)
New coordinates: P = R1*A = (c, 0)
New coordinates: Q = R1*B = (0, c/m)
Equation of line joining P and Q: (Y –0)/( X – c ) = (c /m –0)/(0– c )
Simplifying: Y = (−1/m)X + (1/m)c
Substituting values of m and c we get:
Original equation: y = 2x + 3
New equation: Y = (−1/2)X + (3/2)
4.15 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
4.16 REVIEW QUESTIONS
1. What is meant by a right-handed coordinate system?
2. What are the advantages of homogeneous coordinates over Cartesian coordinates?
3. How does the original coordinates of a point change during translation and scaling
operations?
4. What are the effects of a negative and a fractional scaling factor?
5. What is the difference between a general rotation and a fixed-point rotation operation?
6. What is reflection about an axis and reflection about the origin?
7. What is the difference between affine and perspective transformations?
8. What is the difference between x- and y-direction shears?
9. What is mapping between window and viewport?
10. How can coordinates be transformed from one coordinate system to another?
4.17 PRACTICE PROBLEMS
1. Show that a reflection about the line y = x is equivalent to a reflection relative to the
x-axis followed by a CCW rotation of 90°.
2. Obtain a transformation that reduces rectangle ABCD formed from points A(0, 0),
B(5, 0), C(5, 4), and D(0, 4) to half its size keeping the point D fixed.
3. Reflect the triangle (2, 5), (3, 7), and (4, 6) about the line x = 2 y –1 and find its new
coordinates.
170 ◾ Fundamentals of Graphics Using MATLAB®
4. A square is placed within an animation sequence with its center at (5, 5). It is
subsequently shrunk by 1/50th of its size in each successive frame. Determine the
corresponding transformation matrix for each frame.
5. What transformation maps triangle ABC: A(0, 0), B(5, 0), and C(5, 4) onto a single
point C?
6. Two symbols consisting of a triangle (−1, 0), (0, 1), and (1, 0) and a square (0, 0),
(0, 1), (1, 1), and (1, 0) is combined to create a design with the triangle at (1, 1),
(1.5, 2), and (2, 1) and square at (1, 1), (1, 2), (2, 2), and (2, 1). Find the corresponding
transformations.
7. Find the equation of a straight line Y = mX + c in the x–y coordinate system
if the X–Y coordinate system results from a 90° rotation of the x–y coordinate
system.
8. What is the composite transformation if one shear x ′ = ax + by is followed by another
shear y ′ = bx + ay, where a and b are constants.
9. Find the transformation, which uses the rectangle A(1, 1), B(5, 3), C(4, 5), and D(0, 3)
as a window and a viewport with coordinates (0, 0), (1, 0), (1, 1), and (0, 1).
10. Find the new coordinates of a square with vertices (0, 0), (1, 0), (1, 1), and (0, 1) under
−9 −8 7 −9 −8 7
the following transformations: (a) 6 −5 −4 and (b) 6 −5 −4 .
0 0 1 3 2 1
Chapter 5
Spline Properties
5.1 INTRODUCTION
This chapter discusses few common properties of splines and how these can be calcu-
lated from spline equations (Mathews, 2004). First, it discusses the critical points namely
minimum and maximum of spline curves. Additionally, for splines of degree 3 or more
the point of inflection (POI) is of interest, which is where the curvature changes from
concave to convex or vice versa. Next, it discusses how the tangent and normal to a spline
curve can be calculated. The tangent to a curve is the derivative of the curve equation,
while the normal is the line perpendicular to the tangent. The tangent and the normal
can be represented by line equations or as specific vectors if they are calculated for a
specific point on the curve. Computations of the tangent and normal are discussed both
for a parametric curve as well as an implicit curve. The third property is calculation of
length of a spline curve between any two given points. This can be calculated both from
a spatial curve and a parametric curve. The length of the curve segment is approximated
by summing over a number of small line segments. The fourth property is to calculate
the area under a curve. This is discussed both for curves of the form y = f ( x ) and also
x = f ( y ). The area is calculated by considering a very thin rectangular area under the
curve and then summing over all such rectangular areas. An extension to this is calcula-
tion of area bounded by two curves. This is the area under the upper curve minus the area
under the lower curve. The fifth property is the centroid of an area under a curve, which
is computed using moments. The next section deals with various ways of interpolation
and curve fitting. Interpolation is done when we are interested in some intermediate
value between some given data points. Interpolation can either be linear by consider-
ing straight lines connecting adjacent data points, or non-linear by considering higher
degree curves connecting the data points. This section also mentions built-in MATLAB®
functions for the purpose. Curve fitting is trying to fit a polynomial curve to given data
points and estimating the coefficients of the polynomial. This enables representation of
arbitrary data by polynomial functions. This chapter ends by reviewing few 2D plotting
171
172 ◾ Fundamentals of Graphics Using MATLAB®
functions included in MATLAB. These functions are broadly categorized into two types:
one using symbolic variables and the other using a collection of values. These functions
can also be either explicit or implicit or parametric. Arguments to some of these func-
tions can be used to specify color and transparency values.
5.2 CRITICAL POINTS
Critical points denote the maximum and minimum points of a spline and Point of
Inflection POI for splines of degree 3 or above (see Figure 5.1). For both minimum and
maximum points, the slope of the curve is zero as the line is horizontal. Both points of the
curve y = f ( x ) can be found by computing roots of f ′( x ) = 0.
Let r be a root of the equation f ′( x ) = 0. To determine whether the root corresponds to
a minimum or maximum, consider a small displacement δ to the left and right of r. For a
minimum point, left-hand side (LHS) slope should be negative and right-hand size (RHS)
slope should be positive. This implies:
Alternatively, since the slope changes from negative to positive, rate of change of slope is
positive i.e.
For a maximum point, LHS slope should be positive and RHS slope should be negative.
This implies:
Alternatively, since the slope changes from positive to negative, rate of change of slope is
negative i.e.
5 5 5 (-)
-5 -5 -5 (+)
A POI is where curvature changes from positive (convex) to negative (concave) or vice
versa. The necessary conditions to be satisfied are that the curvature should be zero at
the POI and the sign of curvature on either side should be opposite. Let r be a root of the
equation f ′′( x ) = 0. Consider a small displacement δ to the left and right of r. Conditions
necessary for a POI to exist are:
f ′′( x ) = 0
(5.5)
sign{ f ′′(r − δ )} ≠ sign{ f ′′(r + δ )}
Example 5.1
Find the critical points for the cubic curve: y = 2 + 13x − 31x 2 + 18x 3 .
From the given equation:
f ( x ) = 2 + 13x − 31x 2 + 18 x 3
f ′( x ) = 13 − 62 x + 54 x 2
s = roots(d2p);
s1 = polyval(d2p, s - 0.1);
s2 = polyval(d2p, s + 0.1);
b3 = polyval(p, s);
if (sign(s1) = = sign(s2)), fprintf('no poi');
else fprintf('poi : (%.2f, %.2f)\n', s, b3); end;
%plotting
syms x;
y = p(1)*x^3 + p(2)*x^2 + p(3)*x + p(4);
X = [r(1), r(2), s]; Y = [b1, b2, b3];
xx = linspace(min([r(1), r(2), s]), max([r(1), r(2), s]));
yy = subs(y, x, xx);
plot(xx, yy, 'b-', 'LineWidth', 1.5); hold on;
scatter(X, Y, 20, 'r', 'filled');
plot(X, Y, 'ko');
grid; xlabel('x'); ylabel('y');
%labeling
if m1 = = 0, text(r(1), b1+0.1, 'Minimum');
else text(r(1), b1+0.1, 'Maximum');
end;
if m2 = = 0, text(r(2), b2+0.1, 'Minimum');
else text(r(2), b2+0.1, 'Maximum');
end;
text(s, b3+0.1, 'POI');
hold off;
NOTE
polyder: differentiate polynomial
roots: find roots of polynomial equation
polyval: evaluate polynomial at the specified value
sign: returns sign of the argument +1, 0, or −1
Spline Properties ◾ 175
176 ◾ Fundamentals of Graphics Using MATLAB®
Maximum
3.5
3
y
POI
2.5
2
Minimum
1.5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
N (t ) = R(90) ⋅T (t ) (5.9)
Expanding:
0 −1 0 x ′(t ) − y ′(t )
N (t ) = 1 0 0 ⋅ y ′(t ) = x ′(t ) (5.10)
0 0 1 1 1
Spline Properties ◾ 177
y − y1 y ′(t )
= (5.13)
x − x1 x ′(t )
y − y1 − x ′(t )
= (5.14)
x − x1 y ′(t )
Example 5.2
For the circle C(t) = {cos(t), sin(t)}, find the unit tangent vector, unit normal vector,
and tangent line and normal line at point P(1/√2, 1/√2).
Given curve: C(t ) = {cos(t ), sin(t )}
Tangent vector: T (t ) = C ′(t ) = {x ′(t ), y ′(t )} = {− sin(t ), cos(t )} = unit tangent vector
Normal vector: R(90)* C ′(t ) = {− y ′(t ), x ′(t )} = {− cos(t ), − sin(t )} = unit normal vector
Now at point P(1/√2, 1/√2), solving for cos(t ) = 1/ √ 2 we must have t = π /4
Unit tangent vector at P: {− sin(π /4), cos(π /4)} = {−1/ √ 2, 1/ √ 2}
Unit normal vector at P: {− cos(π /4), − sin(π /4)} = {−1/ √ 2, −1/ √ 2}
y − y1 y ′(t )
Tangent line at P: =
x − x1 x ′(t )
y − 1/ √ 2 cos(π /4)
Substituting values: = = −1
x − 1/ √ 2 − sin(π /4)
Simplifying: x + y – √ 2 = 0
y − y1 − x ′(t )
Normal line at P: =
x − x1 y ′(t )
y − 1/ √ 2 sin(π /4)
Substituting values: = =1
x − 1/ √ 2 cos(π /4)
Simplifying: x − y = 0 (Figure 5.3)
178 ◾ Fundamentals of Graphics Using MATLAB®
1.5
0.5
0
y
-0.5
-1
-1.5
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
x
syms X Y;
fprintf('Tangent Line at P\n');
Y1 = (TP(2)/TP(1))*(X - P(1))+P(2)
fprintf('Normal Line at P\n');
Y2 = (-TP(1)/TP(2))*(X - P(1))+P(2)
Spline Properties ◾ 179
%plotting
ezplot(x, y);hold on;
axis([-2 2 -2 2]);
xx = linspace(-2,2);
yy1 = subs(Y1, X, xx);
yy2 = subs(Y2, X, xx);
plot(xx, yy1, 'r-', xx, yy2, 'r-');
scatter(P(1), P(2), 20, 'r', 'filled');
grid; hold off;
NOTE
diff: calculates derivatives and partial derivatives
solve: generates solution of equations
If the equation of the curve is expressed in implicit form f ( x , y ) = 0, then the equation
of the tangent line to the curve at P ( x 0 , y0 ) is derived by first computing the partial deriva-
tive of the curve equation at the given point, which gives us the normal vector at that point.
∂f ∂f
N ( x , y ) = , (5.15)
∂ x ∂ y
∂ fp ∂ fp
Np = , (5.16)
∂ x ∂ y
Equation of the tangent line at P is computed as:
∂ fp ∂ fp
Tp : ⋅( x − x 0 ) + ⋅ ( y − y0 ) (5.17)
∂x ∂y
Example 5.3
Find the normal vector and tangent line to the curve x 3 + 2xy + y 2 = 9 at point P(1, 2).
Here, f = x 3 + 2 xy + y 2 − 9
∂f ∂f
Partial derivatives: f x ( x , y ) = = 3x 2 + 2 y and f y ( x , y ) = = 2x + 2 y
∂x ∂y
Normal vector: N ( x , y ) = f x ( x , y ), f y ( x , y ) = 3x 2 + 2 y , 2 x + 2 y
Normal vector at point P: N (1, 2) = [7, 6]
2
y
-2
-4
-6
-6 -4 -2 0 2 4 6
x
% plotting
ezplot(f); hold on; grid;
ezplot(t);
quiver(p(1), p(2), n(1), n(2));
scatter(p(1), p(2), 20, 'r', 'filled');
hold off;
NOTE
dot: calculates vector dot product
quiver: depicts vectors as arrows with direction and magnitude
Spline Properties ◾ 181
5.4 LENGTH OF A CURVE
Length of curve y = f ( x ) in spatial domain between points x = a and x = b is given by:
b 2
dy
L=
∫ a
1 + dx (5.18)
dx
The derivation of this expression assumes that a curve can be approximated by a num-
ber of small line segments (see Figure 5.5). For such a small line segment if δx be the
horizontal distance and δy be the vertical distance then the length of the segment can be
approximated by:
δ r = (δ x )2 + (δ y )2 (5.19)
The length of the entire curve is the sum of these small segment distances between x = a
and x = b. Integrating:
b
L=
∫ a
(dx )2 + (dy )2 (5.20)
Simplifying:
b 2 2 b 2
dx dy dy
L=
∫
a
+ dx =
dx dx ∫ a
1 + dx (5.21)
dx
120
100
80 y = f(x)
60
y
r
40
y
20
x
0
-6 -4 -2 0 2 4 6
x
Example 5.4
Find the length of the curve y = 3x + 2 between x = 1 and x = 5.
dy
Here, =3
dx
From Equation (5.18) we get:
b 2 5 5
dy
L=
∫a
1 + dx =
dx ∫ 1
1 + (3)2 dx =
∫
1
10 dx = 4 10 = 12.65
Verification: y(1) = 5, y(5) = 17, P1 = (1, 5), P2 = (5, 17), distance (P1, P2) = 16 + 144 = 12.65
NOTE
int: integrate symbolic expression
If the equation of the curve is expressed in parametric form i.e. x (t ) and y(t ), then from
Equation (5.19):
dr = (dx )2 + (dy )2
Integrating:
b 2 2
dx dy
L=
∫a
+ dt (5.22)
dt dt
Example 5.5
Determine the length of the parametric curve x = 3 ⋅ sint , y = 3 ⋅ cost, for 0 ≤ t ≤ 2 π
dx dy
Here, = 3 ⋅ cos(t ) and = −3 ⋅ sin(t )
dt dt
Spline Properties ◾ 183
b 2 2 2π 2π
dx dy
L=
∫
a
+ dt =
dt dt ∫0
(3cos t )2 + (−3sin t )2 dt =
∫0
3 dt = 6π
20
18
16
14 y = f(x)
12
10
Y
6 y
2
0
-6 -4 -2 0 2 dx 4 6
X
Example 5.6
Find the area under the curve y = 10 – x 2 between the values x = –1 and x = 2.
From Equation (5.23) we get (Figure 5.7):
b 2
A=
∫ a
f ( x ) dx =
∫ (10 − x )dx = 27
−1
2
%plotting
xx = linspace(-4, 4);
yy = subs(y, x, xx);
plot(xx, yy);
xlabel('x'); ylabel('y');
grid; hold on;
axis([-5 5, 0 12]);
text(-3, 9, 'y = 10 - x^2');
%filling
x = linspace(-1, 2);
Spline Properties ◾ 185
y1 = 10 - x.^2;
y2 = zeros(1,100);
X = [x,fliplr(x)];
Y = [y1,fliplr(y2)];
fill(X,Y,'g');
alpha(0.25);
hold off;
NOTE
zeros: generates a matrix filled with zeros
fliplr: flip array in left–right direction
fill: fills a polygon with color
alpha: sets transparency values
The above area computed is actually the area between the curve f ( x ) and the x-axis.
However, if the curve itself lies below the x-axis then the area computed by the above for-
mula comes out as negative. In such cases, we need to take the absolute value of the area.
It is, therefore, always best to sketch the curve before finding areas under curves.
Example 5.7
Find the area under the curve y = x 3 between the values x = −1 and x = 2
From Equation (5.23) we get (Figure 5.8):
0 2
b 0 2
x4 x4 1 16 17
A=
∫a
f ( x ) dx =
∫
−1
3
x dx +
∫
0
3
x dx =
4 −1
+ = + = = 4.25
4 0 4 4 4
If the area is required between a curve and the y-axis then by an extension of earlier ideas
we first express the curve in the form x = f ( y ) and then by drawing a series of small rectan-
gle of width dy and height x = f ( y ), we integrate over the specified limits c to d. In this case:
d
A=
∫
c
f ( y ) dy (5.24)
Spline Properties ◾ 187
Example 5.8
Find the area under the curve y = x + 1 between the values y = 3 and y = 5.
Rewriting: x = f ( y ) = y 2 − 1
From Equation (5.24) we get (Figure 5.9):
d 5
A=
∫ c
f ( y ) dy =
∫ ( y − 1) dy = 30.67
3
2
%plotting
yy = linspace(0, 6);
xx = subs(x, y, yy);
plot(yy, xx);
xlabel('y'); ylabel('x');
hold on;
X = [3, 3]; Y = [0, subs(x, y, 3)]; plot(X, Y, 'r');
X = [5, 5]; Y = [0, subs(x, y, 5)]; plot(X, Y, 'r');
188 ◾ Fundamentals of Graphics Using MATLAB®
Area bounded by two curves f ( x ) and g ( x ) is the area between the upper curve and the
x-axis minus the area between the lower curve and x-axis:
b
A=
∫ a
f ( x ) − g ( x ) dx (5.25)
Example 5.9
Find the area bounded by the curves y = x 3 and y = x between the values x = −1
and x = 1
From Equation (5.25) we get (Figure 5.10):
b 1
A=
∫a
f ( x ) − g ( x ) dx =
∫
−1
x 3 − x dx = 0.5
%plotting
xx = linspace(-2,2);
yy1 = subs(y1, x, xx);
yy2 = subs(y2, x, xx);
plot(xx, yy1, xx, yy2);
xlabel('x'); ylabel('y');
axis([-2 2 -2 2]); hold on; grid;
plot([-2, 2], [0, 0], 'k--');
plot([0, 0], [-2, 2], 'k--');
text(-1.5, -0.5, 'y = x^3');
text(-0.5, -0.75, 'y = x');
x = linspace(-1, 1);
y1 = x;
y2 = x.^3;
X = [x,fliplr(x)];
Y = [y1,fliplr(y2)];
fill(X,Y,'y'); alpha(0.25);
hold off;
5.6 CENTROID
The earth exerts gravitational force on each particle of a solid object. If all these forces are
replaced by a single equivalent force then this force will act through a single point called
center of gravity. Assuming the density of the object is uniform, the center of gravity will
coincide with the centroid of mass. If the object of uniform density is a thin plate then
the center of mass will coincide with the centroid of the area. The centroid of the plate is,
therefore, a single point through which the entire weight of the plate can be balanced. It is
obvious that for rectangular or circular shapes, the centroid is exactly at the center. Before
finding the centroid of an arbitrary shape, let us first consider a polygon of some random
dimensions as shown in Figure 5.11.
To find the centroid of the polygon, it is divided into two rectangles and the centroid
of each rectangle is at its center. The left rectangle is of width 4 and height 4 so its area is
A1 = 4 × 4 = 16 and its center is at (x1, y1) = (−3, 1). The right rectangle is of width 5 and
height 6 so its area is A2 = 5 × 6 = 30 and its center is at (x2, y2) = (1.5, 2). To find the centroid
of the polygon, we compute moments of the component rectangles from both the X- and
Y-axes and equate their sum with the moment of the entire area. Let x and y denote the
coordinates of the centroid and A be its entire area. Equating moments with respect to the
X- and Y-axes we get:
190 ◾ Fundamentals of Graphics Using MATLAB®
A2
3
A1
2
(x2, y2)
(xc, yc)
1
(x1, y1)
-1
-2
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5
A1 ⋅ x1 + A2 ⋅ x 2 = A ⋅ x
A1 ⋅ y1 + A2 ⋅ y 2 = A ⋅ y
A1 ⋅ x1 + A2 ⋅ x 2 16(−3) + 30(1.5)
x= = = −0.065
A 16 + 30
A1 ⋅ y1 + A2 ⋅ y 2 16(1) + 30(2)
y= = = 1.652
A 16 + 30
From the above, we can formulate a general rule that centroid of an area is equal to the sum
of the moments of its component parts divided by the whole area.
Figure 5.12 depicts the graph of a function y = f ( x ) and it is required to find the centroid
of an area below the curve bounded by the lines x = a and x = b. Consider a very thin strip
of width dx at a distance of x from the Y-axis and of height y = f ( x ) so that the area of
the strip is y·dx. Moment of the strip along x-direction is thus (y·dx)·x and moment of the
y
strip along y-direction is ( y ⋅ dx ) ⋅ since the center of the strip is exactly at the middle
2
Spline Properties ◾ 191
y = f(x)
y/2
x
O
a dx b
X
along the height of the strip. So equating the moments of all such strips within the area and
dividing by the area itself we obtain the centroid of the area as given below:
b
x=
∫ x ⋅ f ( x ) ⋅ dx
a
b
∫ f (x )⋅dx
a
(5.26)
b
y=
∫ { f (x )} ⋅dx
a
b
2
∫
2 f ( x ) ⋅ dx
a
Example 5.10
Find the centroid of the area bounded by the curves y = x 3, x = 0, and x = 2.
b 2
From Equation (5.23), area:
∫
a
f ( x ) ⋅ dx =
∫ x ⋅dx = 4
0
3
x=
∫ x ⋅ x ⋅dx = 1.6
0
3
4
2
y=
∫ x ⋅ dx
0
= 2.29
6
2×4
192 ◾ Fundamentals of Graphics Using MATLAB®
a = int(y, 0, 2);
eval(a);
fprintf('Area : %f\n', eval(a));
m1 = int(x*y, 0, 2);
m2 = int(0.5*y^2, 0, 2);
xc = m1/a;
yc = m2/a;
If the region is bounded by two curves f(x) and g(x) on the interval [a, b] then the centroid
of the bounded region is given as follows:
b
x=
∫ x ⋅ { f ( x ) − g ( x )} ⋅ dx
a
b
∫ { f (x ) − g (x )} ⋅d
a
(5.27)
b
y=
∫ { f (x )} − {g (x )} ⋅ dx
a
b
2 2
∫
2 { f ( x ) − g ( x )} ⋅ dx
a
(a)
linear linear
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-4 -2 0 2 4 0 2 4 6
nearest nearest
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-4 -2 0 2 4 0 2 4 6
(b)
previous previous
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-4 -2 0 2 4 0 2 4 6
next next
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-4 -2 0 2 4 0 2 4 6
FIGURE 5.13 Interpolation using interp1 with options (a) linear and nearest neighbor (b) previous
neighbor and next neighbor
The corresponding code for generating the plots is given below:
subplot (221)
plot(x,y,'o',t,interp1(x,y,t, 'linear'), 'r-', 'LineWidth', 2); title('linear');
subplot (223)
plot(x,y,'o',t,interp1(x,y,t, 'nearest'), 'r-', 'LineWidth', 2); title('nearest');
x = 0:2*pi;
y = sin(x);
t = 0:.01:2*pi;
194 ◾ Fundamentals of Graphics Using MATLAB®
subplot (222)
plot(x,y,'o',t,interp1(x,y,t, 'linear'), 'r-', 'LineWidth', 2); title('linear');
subplot (224)
plot(x,y,'o',t,interp1(x,y,t, 'nearest'), 'r-', 'LineWidth', 2); title('nearest');
figure
x = -3:3;
y = [-1 -1 -1 0 1 1 1];
t = -3:.01:3;
subplot (221)
plot(x,y,'o',t,interp1(x,y,t, 'previous'), 'r-', 'LineWidth', 2); title('previous');
subplot (223)
plot(x,y,'o',t,interp1(x,y,t, 'next'), 'r-', 'LineWidth', 2);title('next');
x = 0:2*pi;
y = sin(x);
t = 0:.01:2*pi;
subplot (222)
plot(x,y,'o',t,interp1(x,y,t, 'previous'), 'r-', 'LineWidth', 2); title('previous');
subplot (224)
plot(x,y,'o',t,interp1(x,y,t, 'next'), 'r-', 'LineWidth', 2);title('next');
NOTE
interp1: performs 1-D interpolation
Spline Properties ◾ 195
196 ◾ Fundamentals of Graphics Using MATLAB®
0.5
0.5
0 0
-0.5 -0.5
-1
-2 0 2 0 2 4 6
The second function is “pchip,” which stands for “Piecewise Cubic Hermite Interpolating
Polynomial.” It provides a piecewise polynomial form of a shape-preserving piecewise
cubic Hermite interpolate to the values. For each sub-interval, it interpolates between the
end points and also maintains that the slopes at the endpoints be continuous. This is illus-
trated in Figure 5.14 for the step function on the left and the sine function on the right.
The corresponding code for generating the plots is given below:
subplot (121)
plot(x,y,'o',t, pchip(x,y,t), 'r-', 'LineWidth', 2);
axis tight; axis square;
subplot (122)
plot(x,y,'o',t, pchip(x,y,t), 'r-', 'LineWidth', 2);
axis tight; axis square;
The third function is “spline,” which stands for “Piecewise Cubic Spline Interpolating
Polynomial.” It provides a piecewise polynomial form of a cubic spline over the data values.
It is illustrated in Figure 5.15 for the step function and sine function.
The corresponding code for generating the plots is given below:
subplot (121)
plot(x,y,'o',t, spline(x,y,t), 'r-', 'LineWidth', 2);
axis tight; axis square;
subplot (122)
plot(x,y,'o',t,spline(x,y,t), 'r-', 'LineWidth', 2);
axis tight; axis square;
A comparison between the three is provided in Figure 5.16. The “interp1” function joins
data points by straight lines, the smoothest curve is provided by “spline” function while the
“pchip” function produces reduced oscillations at the end points.
For fitting a curve to a set of given data points, MATLAB provides a function called
“polyfit” that creates a fitting polynomial of a specified degree whose sum of square errors
from the data points is minimum, and returns the coefficients of the polynomial. The code
below illustrates the process. Two sets of data points have been generated and curves of
various degrees have been used to fit the data. The results are plotted by varying the value
of d. The coefficients of the polynomials are returned by the function. Figure 5.17 shows a
step function in the upper row and a sine function in the lower row. The columns indicate
the degree of the polynomials used for curve fitting i.e. 1, 3, and 9.
0.5 0.5
0 0
-0.5 -0.5
-1
-2 0 2 0 2 4 6
1
data data
interp1 interp1
pchip pchip
0.5 0.5
spline spline
0 0
-0.5 -0.5
-1
-3 -2 -1 0 1 2 3 0 1 2 3 4 5 6
1 1
1
0.5 0.5
0.5
0 0 0
0 0 0
-1 -1
-2 0 2 -2 0 2 -2 0 2
clear; clc;
x = -10:10;
y = [-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 1 1 1 1 1 1 1 1 1 1];
t = -10:.01:10;
subplot (231)
d = 1; pf = polyfit(x, y, d);
pv = polyval(pf, t);
plot(x,y,'o',t, pv, 'LineWidth', 2);
axis tight; axis square;
title('degree = 1');
subplot (232)
d = 3; pf = polyfit(x, y, d);
pv = polyval(pf, t);
plot(x,y,'o',t, pv, 'LineWidth', 2);
axis tight; axis square;
title('degree = 3');
subplot (233)
d = 9; pf = polyfit(x, y, d);
pv = polyval(pf, t);
plot(x,y,'o',t, pv, 'LineWidth', 2);
axis tight; axis square;
title('degree = 9');
Spline Properties ◾ 199
clear x y t pf pv;
x = -pi:pi;
y = sin(x);
t = -pi:.1:pi;
subplot (234)
d = 1; pf = polyfit(x, y, d);
pv = polyval(pf, t);
plot(x,y,'o',t, pv, 'LineWidth', 2);
axis tight; axis square;
subplot (235)
d = 3; pf = polyfit(x, y, d);
pv = polyval(pf, t);
plot(x,y,'o',t, pv, 'LineWidth', 2);
axis tight; axis square;
subplot (236)
d = 9; pf = polyfit(x, y, d);
pv = polyval(pf, t);
plot(x,y,'o',t, pv, 'LineWidth', 2);
axis tight; axis square;
axis([-pi pi -1 1]);
NOTE
polyfit: generates a polynomial to fit a given data
(a) ezplot: This function can be used to plot using symbolic variables:
1 - 2.25 t + 1.25 t 2
60
50
40
30
20
10
-6 -4 -2 0 2 4 6
t
x4 + y3 = 2 x y
6
0
y
-2
-4
-6
-6 -4 -2 0 2 4 6
x
(b) plot: This function can be used to plot using a vector of values:
(i) parametric equations (Figure 5.21):
t = 0:pi/50:10*pi; plot(t.*sin(t),t.*cos(t));
x = cos(t), y = sin(t)
0.8
0.6
0.4
0.2
0
y
-0.2
-0.4
-0.6
-0.8
-1 -0.5 0 0.5 1
x
30
20
10
-10
-20
-20 -10 0 10 20
(c) ezcontour: Contour plots, one version with only the edges and the other being a filled
version (Figure 5.23)
(d) fimplicit: Plots implicit functions (introduced from MATLAB version 2016). The ver-
sion of an installed MATLAB package can be checked by typing ver at the command
line (Figure 5.24).
202 ◾ Fundamentals of Graphics Using MATLAB®
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
-2.5
-3 -2 -1 0 1 2 3
2 2
1 1
0 0
y
-1 -1
-2 -2
-3 -3
-3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3
x x
10
8
6
4
2
0
-2
-4
-6
-8
-10
-10 -8 -6 -4 -2 0 2 4 6 8 10
(e) patch: Creates filled polygons given the vertices and colors (Figure 5.25)
x = [4 6 11 9];
y = [2 7 9 4];
c = [0 4 6 8];
colormap(jet);
patch(x,y,c);
colorbar;
hold;
v1 = [2 4; 2 10; 8 4];
patch('Vertices', v1, 'FaceColor', 'red', 'FaceAlpha', 0.3, 'EdgeColor', 'red');
axis([0 12 0 12]);
NOTE
colormap: specifies a color scheme using predefined color look-up tables
colorbar: creates a color bar by appending colors in the colormap
Spline Properties ◾ 203
204 ◾ Fundamentals of Graphics Using MATLAB®
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-6 -4 -2 0 2 4 6
5.9 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
5.10 REVIEW QUESTIONS
1. What is meant by critical points of a spline?
2. How are the minimum and maximum points differentiated by the gradients of a
curve?
3. How is the POI of a curve determined? Why does a cubic curve always have a
single POI?
4. Representing a parametric curve by an ordered pair, how is the tangent and normal
computed?
5. How is the expression for length of a curve between two given points determined?
6. How is the area between a curve and the x-axis bounded by two vertical lines
determined?
7. What convention is followed for computing area above and below the x-axis?
206 ◾ Fundamentals of Graphics Using MATLAB®
5.11 PRACTICE PROBLEMS
1. Find the length of y = x 1.5 between x = 0 and x = 5.
2. Determine the length of the curve y = ln(sec x ) for 0 ≤ x ≤ π /4.
3. Find the area between y = x 2 and x = y 2 from x = 0 to x = 1.
4. Find the area fully enclosed between the curves x = y + 1 and x = 0.5 y 2 − 3.
5. Find the minimum, maximum and inflection points for the curve: y = –2.67 x + 0.67 x 3.
6. Find the tangent vector, the normal vector, and equation of tangent line of the curve
(t, t2) at point (1, 1).
7. Find the unit tangent vector and unit normal vector to the parametric curve x = t 3,
y = t 2 at (−8, 4).
8. For the implicit curve x 3 + y 2 − x = 4, find the tangent line and normal vector at point
(−1, 2).
9. For the cycloid (t + sin t ,1– cos t ), find the tangent vector and normal vector at t = π /2.
10. Find the centroid of the area bounded by the curves y = x and y = x 3.
Chapter 6
Vectors
6.1 INTRODUCTION
Vectors involve both magnitude and direction. Two vectors are equal if they have the same
magnitude and direction. Vectors a and b are not equal even if they have same magnitude.
Vector –c is defined as having same magnitude but reverse direction as c. Multiplying a
vector by a scalar changes its magnitude but keeps direction same e.g. 2a. By default vec-
tors are “free” i.e. parallel shifting does not change their magnitude or direction. Vectors
can also be “bound” i.e. cannot be shifted e.g. position vector of point P with respect to
origin O (Olive, 2003) (see Figure 6.1).
Vector addition implies finding the resultant of two vectors. There are two methods to
do this, both of which are essentially equivalent. The triangle rule states that, if p and q
represents two sides of a triangle, then p + q is given by the third side. The parallelogram
rule states that if P and Q be two adjacent sides of a parallelogram, P + Q is given by its
diagonal (Shirley, 2002). If there are more than two vectors, then we use the polygon rule,
which says that addition of any number of vectors is obtained by arranging them end
to end and closing the final side of resulting polygon i.e. r = a + b + c + d (see Figure 6.2).
For 3D vectors, they are to be joined end to end in 3D space.
2a
-c P
a c
r
b
O
207
208 ◾ Fundamentals of Graphics Using MATLAB®
5 5 5
q
4 4 4
p+q p+q c d
p p
3 3 3
2 2 2 r
q
b
1 1 1 a
0 0 0
0 2 4 6 0 2 4 6 0 2 4 6
6.2 UNIT VECTOR
The magnitude of the vector R = ai + bj + ck is given by:
R = (a 2
)
+ b 2 + c 2 (6.1)
This is also known as the Euclidean length between the start and end points of the vector.
The unit vector along the direction of R is given by:
r=
R
=
(ai + bj + ck ) (6.2)
R (a + b + c )
2 2 2
Example 6.1
Find the magnitude and unit vector in the direction of a specified vector R = 2i – j + 2k.
(a)
5
P = 3i + 4j
4
4j
1
j
0
y
O i 3i
-1
-2
-3
-4
-5
-5 0 5
x
(b)
3.5
3
P = 2i + 3j + 2k
2.5
2
2k
1.5
z
1
k
0.5
3j 2i
-0.5
j i 4
-1 3
4 O 2
3 1
2
1 0
0 -1
-1 x
y
FIGURE 6.3 (a) and (b) Vector components (a) 2D plane (b) 3D space.
210 ◾ Fundamentals of Graphics Using MATLAB®
1.5
1
z
0.5
0
0
2
-0.5 1.5
1
0.5
y -1 0 x
NOTE
norm: calculates the magnitude or Euclidean length of a vector.
quiver3: depicts 3D vectors as arrows with direction and magnitude.
6.3 DIRECTION COSINES
Let A, B, and C be the angles made by the vector R = ai + bj + ck with the three primary
axes. The cosines of these three angles cos(A), cos(B), and cos(C) are known as direc-
tion cosines.
Vectors ◾ 211
a
cos A =
(a + b + c )
2 2 2
b
cos B = (6.3)
(a + b + c )
2 2 2
c
cos C =
(a + b + c )
2 2 2
Example 6.2
Find the direction cosines and angles, the vector R = 3i + 5 j − 2k makes with
coordinate axes.
From Equation (6.3)
a 3
cos A = = = 0.4867
(a + b + c )
2 2 2 38
b 5
cos B = = = 0.8111
(a + b + c )
2 2 2 38
c −2
cos C = = = −0.3244
(a + b + c )
2 2 2 38
A = arccos(0.4867) = 60.87°
B = arccos(0.8111) = 35.79°
C = arccos(−0.3244) = 108.93°
%alternatively
A = acosd(cosA)
B = acosd(cosB)
C = acosd(cosC)
NOTE
rad2deg: converts radian to degree values
acos: calculates inverse cosine in radians
acosd: calculates inverse cosine in degrees
6.4 DOT PRODUCT
Dot product of two vectors A = ai + bj + ck and P = pi + qj + rk with an angle θ between
them is given by the following (Shirley, 2002):
A • P = A P cos(θ ) (6.4)
cos(θ ) = ( A • P )/ ( A P ) (6.5)
Even though it is a product of two vector quantities, the product itself is a scalar number.
To see how this is possible, we expand Equation (6.4)
A • P = (ai + bj + ck ) ⋅ ( pi + qj + rk ) = ap + bq + cr (6.6)
Example 6.3
(a) Find if vectors A = 3i + 5 j – 2k and B = 2i – 2 j – 2k are perpendicular to each
other.
ind if vectors A = 3i + 5 j – 2k and B = 0.5i + (5/6 ) j –0.333k are parallel to
(b) F
each other.
(c) Find angle between vectors A = 2i – 3 j + k and B = 4i + j – 3k
(a)
From Equation (6.4)
A • B = 3 × 2 − 5 × 2 + 2 × 2 = 6 − 10 + 4 = 0
Vectors ◾ 213
A = √ (9 + 25 + 4) = √ 38
A B = √ 38 × √ 38/6 = 38/6 = A • B
cos(θ ) = ( A • B) ( A B )
A = √ (4 + 9 + 1) = √14
B = √ (16 + 1 + 9) = √ 26
( )
cos(θ ) = ( A • B) ( A B ) = 2 √14 √ 26 = 2/19.0788 = 0.1048
θ = arccos(0.1048) = 83.98°
% (a)
A = [3, 5, -2];
B = [2, -2, -2];
C = dot(A, B);
if C = = 0
fprintf('Perpendicular\n');
else
fprintf('Not perpendicular\n');
end
214 ◾ Fundamentals of Graphics Using MATLAB®
% (b)
clear all;
A = [3, 5, -2];
B = [0.5, 5/6, -0.333];
C = dot(A,B);
nA = norm(A);
nB = norm(B);
P = nA*nB;
if (C - P) < 0.001
fprintf('Parallel\n');
else
fprintf('Not parallel\n');
end
% (c)
clear all;
A = [2, -3, 1];
B = [4, 1, -3];
C = dot(A, B);
nA = norm(A);
nB = norm(B);
D = C/(nA*nB);
angle = acosd(D);
fprintf('Angle : %f deg\n', angle);
NOTE
dot: calculates dot product of vectors
6.5 CROSS PRODUCT
Cross (vector) product of two vectors A = ai + bj + ck and P = pi + qj + rk with an angle θ
between them is given by the following (Shirley, 2002):
A × P = A P sin(θ ) ⋅ n (6.7)
The resultant is a vector in the direction of n, which is a vector perpendicular to both A and
P. The positive direction of n is governed by the right-handed corkscrew rule, which says
that if a right-handed corkscrew is rotated from A to P then the positive direction of the n
will be that along which the corkscrew advances.
Substituting the components in the above expression we get:
A × P = (ai + bj + ck ) × ( pi + qj + rk )
Example 6.4
Find the vector perpendicular to both A = 3i + 5 j − 2k and B = 2i − 2 j − 2k
From Equation (6.8) we get:
%verification
dot(C,A) % should be 0
dot(C,B) % should be 0
NOTE
cross: calculates cross product of vectors
r = a + t ⋅ b (6.9)
Any point on the line AP corresponds to some value of the scalar t and should satisfy the
above equation (Shirley, 2002). Rewriting AP as (r − a) and plugging this in Equation (6.9)
we get: r = a + t ⋅ (r − a). Putting t = 0 gives us point A and putting t = 1 gives us point P.
Putting t = 0.5 say, gives us the mid-point of line segment AP.
To convert the vector equation to Cartesian equation, the following steps are taken:
Expanding Equation (6.9) in terms of 3D components, a = a1i + a2 j + a3k, b = b1i + b2 j + b3k,
and r = xi + yj + zk.
216 ◾ Fundamentals of Graphics Using MATLAB®
6 P
b
A
4
r
2
a
0
O
-2
-2 0 2 4 6 8 10 12 14
For this to be true the i , j , and k components should separately be equal. Thus
x = a1 + t ⋅ b1
y = a2 + t ⋅ b2
z = a3 + t ⋅ b3
Rearranging terms
x − a1 y − a2 z − a3
t= = = (6.10)
b1 b2 b3
Example 6.5
A straight line passes through points (6, 3, −5) and (2, 1, −4). Find its equation in
vector form and Cartesian form
Position vectors P = 6i + 3 j − 5k , Q = 2i + j − 4 k
Direction vector along QP: Q − P = (2i + j − 4 k ) − (6i + 3 j − 5k ) = −4i − 2 j + k
Vectors ◾ 217
x − 6
t = = − x /4 + 3/2
2 − 6
y − 3
t= = − y /2 + 3/2
1 − 3
z +5
t = = z + 5
−4 + 5
− x + 6 − y + 3 z + 5
Cartesian equation: = =
4 2 1
Verification:
P = [6, 3, -5];
Q = [2, 1, -4];
syms t;
fprintf('Vector equation : \n');
r = P + t*(Q - P)
x1 = P(1); x2 = Q(1);
y1 = P(2); y2 = Q(2);
z1 = P(3); z2 = Q(3);
dx = x2 - x1;
dy = y2 - y1;
dz = z2 - z1;
nx = (x - x1);
ny = (y - y1);
nz = (z - z1);
r = s ⋅ a + t ⋅ b (6.11)
where s and t are the scaling factors for vectors a and b, respectively. This is the vector
equation of the plane because any point P on the plane satisfies Equation (6.11) for different
values of s and t.
For the second case, if the plane does not pass through the origin, then an additional
vector c is required to define the position vector of point C, where the vectors a and b meet.
Then the position vector r of the arbitrary point P on the plane can be expressed as a com-
bination of scaled versions of a, b, and the vector c (see Figure 6.6b).
r = c + p = c + s ⋅ a + t ⋅ b (6.12)
To derive the Cartesian equation of the plane consider Figure 6.7, where ON is the perpen-
dicular from origin O onto the plane at N and let n be the unit vector along ON. Let length
of ON be D. As before, let P be any arbitrary point on the plane and let r be its position
vector. Also let the angle between ON and OP be θ.
Now from Equation (6.4)
In words, the above equation means that the dot product of the position vector of any point
P on the plane and the unit normal vector equals the perpendicular distance D of the plane
from the origin. Expanding into components if n = n1i + n2 j + n3k and r = xi + yj + zk then
r • n = x ⋅ n1 + y ⋅ n2 + z ⋅ n3 = D (6.14)
The general form of the Cartesian equation of a plane is given by (Hearn and Baker, 1996):
Ax + By + Cz = D (6.15)
A = n1
B = n2 (6.16)
C = n3
Hence, the coefficients of the Cartesian equation of a plane are equal to the components of
the unit normal vector from the origin onto the plane.
Vectors ◾ 219
(a)
5
3
a P
1
r
0
O
-1
b
-2
-3
-2 -1 0 1 2 3 4 5 6 7
(b)
1.5
P
1
a p
C b
0.5
c r
0
10
10
5
O
5
0
0
-5 -5
FIGURE 6.6 (a) and (b) Deriving vector equations of a plane (a) passing through origin (b) not
passing through origin.
220 ◾ Fundamentals of Graphics Using MATLAB®
1.5
P
1
0.5
n r
0
10
10
5
O
5
0
0
-5 -5
Example 6.6
Vector equation of a plane is r = c + as + bt, where c = 2i + 4 j − 3k, a = −3i + 3 j − 3k,
b = 4i + 2 j + 3k, and s, t are the scalars. Find the normal vector N and the normal
distance D from the origin and Cartesian equation of the plane.
Let P be any point on the plane with OP = r = xi + yj + zk
Let N be normal vector of the plane
Then N = a × b = 15i − 3 j − 18k
Magnitude N = √{225 + 9 + 324} = 23.622
Unit vector n = N N = 0.6350i − 0.1270 j − 0.7620k
Hence, for the plane Ax + By + Cz = D, we have A = 0.6350, B = −0.1270, and
C = −0.7620
Now for any point C on the plane, we must have c • n = D as per Equation (6.13)
Hence, D = (2i + 4 j − 3k )•(0.6350i − 0.1270 j − 0.7620k ) = 3.048
Thus, Cartesian equation of the plane is 0.6350 x − 0.1270 y − 0.7620 z = 3.048
Verification: since C(2, 4, −3) is a point on the plane it should satisfy the plane
equation i.e.
%verification
x = c(1); y = c(2); z = c(3);
f = n1*x + n2*y + n3*z - d; % should be zero
Vectors ◾ 221
222 ◾ Fundamentals of Graphics Using MATLAB®
Example 6.7
For each of the following vectors, find the angle it makes with the positive X-axis
and the rotation matrix to align it with the positive X-axis: (a) 3i + 4 j, (b) −3i + 4 j,
(c) −3i − 4 j, and (d) 3i − 4 j. Also for each case verify, the alignment by multiplying the
vector with the rotation matrix.
(a)
P = 3i + 4 j = [3, 4]
θ = arctan(4/3) = 53.13°
R1 = R(−53.13°)
Verification: Q = R1*P = [5, 0, 1]T
4 P = -ai + bj P = ai + bj
3
-X, +Y +X, +Y
2
1
Y-axis
-1
O (0,0)
-2
-X, -Y +X, -Y
-3
-4
P = -ai - bj P= ai - bj
-5
-5 -4 -3 -2 -1 0 1 2 3 4 5
X-axis
(b)
P = −3i + 4 j = [−3, 4]
θ = 180° − arctan(4/3) = 126.87°
R1 = R(−126.87°)
Verification: Q = R1*P = [5, 0, 1]T
(c)
P = −3i − 4 j = [−3, −4]
θ = 180° + arctan(4/3) = 233.13°
R1 = R(−233.13°)
Verification: Q = R1*P = [5, 0, 1]T
(d)
P = 3i − 4 j = [3, −4]
θ = −arctan(4/3) = −53.13°
R1 = R(53.13°)
Verification: Q = R1*P = [5, 0, 1]T
P = [-3, -4]
B = pi + atan(P(2)/P(1));
ang = rad2deg(B)
A = -B;
R = [cos(A) -sin(A) 0 ; sin(A) cos(A) 0 ; 0 0 1];
Q = R * [P, 1]';
P = [3, -4]
B = -atan(abs(P(2))/abs(P(1)));
ang = rad2deg(B)
A = -B;
R = [cos(A) -sin(A) 0 ; sin(A) cos(A) 0 ; 0 0 1];
Q = R * [P, 1]';
224 ◾ Fundamentals of Graphics Using MATLAB®
Apart from the +X axis angles of orientation can also be calculated with respect to the
other primary axes. If θ be the angle with the +X axis, then angle with +Y axis will be
(90 − θ), angle with the −X axis will be (180 − θ), and angle with the −Y axis will be
(90 + θ). The signs are calculated by observing whether we are moving in the CW or
CCW direction. For example, for vector (ai + bj ) which is in Q1 quadrant, when angle
is measured from +X axis we are moving in CCW direction hence +θ, when measuring
from −X axis we are moving in CW direction hence −(180 − θ), when measuring from +Y
axis we are moving in CW direction hence −(90 − θ), when measuring from −Y axis we
are moving in CCW direction hence +(90 + θ). Given in Table 6.1 are the possible cases
for a and b positives. Column 1 shows the vectors, column 2 shows the quadrant in which
the vector lies, and columns 3 to 6 indicate the angle and direction values (CCW +ve
and CW −ve). It is to be noted that for actually aligning the vector to the corresponding
axis, the sign of the angle will be just the negative to that given in the table. For example,
for (ai + bj ) the angle from −X-axis is −(180 − θ) as it is measured in CW direction but
to align the vector with the −X-axis, the vector needs to be rotated in the CCW direc-
tion, so angle for alignment is +(180 − θ). Similar for −ai − bj which lies in Q3 the angle
for aligning with the +Y axis is −(90 + θ) since the vector would need to be rotated in
CW direction.
Example 6.8
For each of the following vectors, find the rotation matrix to align it with the primary
axis specified (a) 3i − 4 j with positive Y-axis and (b) −3i − 4 j with negative X-axis.
Also for each case, verify the alignment by multiplying the vector with the rotation
matrix.
(a)
P = 3i − 4 j = [3, −4]
θ = arctan(4/3) = 53.13°
To align with +Y axis, the vector needs to be rotated by (90 + 53.13)° = 143.13°
in CCW direction.
R1 = R(143.13°)
Verification: Q = R1*P = [0, 5, 1]T
(b)
P = −3i − 4 j = [−3, −4]
θ = arctan(4/3) = 53.13°
Vectors ◾ 225
θ = 180° + arctan(4/3) = 233.13°
To align with −X axis, the vector needs to be rotated by 53.13° CW direction.
R1 = R(−53.13°)
Verification: Q = R1*P = [−5, 0, 1]T
P = [-3, -4]
B = atan(abs(P(2))/abs(P(1)));
A = -B;
R = [cos(A) -sin(A) 0 ; sin(A) cos(A) 0 ; 0 0 1];
fprintf('verification \n');
Q = R * [P, 1]'
NOTE
Vector alignment in 3D has been dealt with in the next chapter.
aX + bY + cW = 0 (6.17)
This is called homogeneous line equation (Marsh, 2005) and the vector = (a, b, c ) is
denoted as line vector. If P( X ,Y ,W ) be a point on the line, we must have:
i P = (a, b, c ) i ( X , Y , W ) = aX + bY + cW = 0 (6.18)
P = 1 × 2 (6.20)
Example 6.9
Solve the following using vector-based methods: (a) Find which of the points (0, 5),
(2, 1), and (10/3, 0) lies on the line 3x + 4 y − 10 = 0 . (b) Find the equation of the
line passing through the points (1, 8) and (6, −7). (c) Find the point where the
lines x − 2 y + 3 = 0 and 4x − 5 y + 6 = 0 intersects.
(a)
Here, L = [3, 4, −10], P1 = [0, 5, 1], P2 = [2, 1, 1], and P3 = [10/3, 0, 1]
From Equation (6.18),
L • P1 = (3)(0) + (4)(5) + (−10)(1) = 0 + 20 − 10 ≠ 0
Hence, point (0, 5) does not lie on line 3x + 64 − 10 = 0
L • P2 = (3)(2) + (4)(1) + (−10)(1) = 6 + 4 − 10 = 0
Hence, point (2, 1) lies on line 3x + 64 − 10 = 0
L • P3 = (3)(10/3) + (4)(0) + (−10)(1) = 10 + 0 − 10 = 0
Hence, point (10/3, 0) lies on line 3x + 64 − 10 = 0
(b)
Here, P1 = [1, 8, 1] and P2 = [6, −7, 1]
From Equation (6.19),
L = P1 × P2 = {(8)(1) − (1)(7)}i + {(1)(6) − (1)(1)} j + {(1)(−7) − (8)(6)} k = 15i + 5 j − 55k
Required equation of line: 15 x + 5 y − 55 = 0
Verification: P1 and P2 are points on this line: 15(1) + 5(8) − 55 = 0,
15(6) + 5(−7) − 55 = 0
(c)
Here, L1 = [1, −2, 3] and L2 = [4, −5, 6]
From Equation (6.20), P = L1 × L2 = [3,6,3] (in homogeneous coordinates)
Converting to Cartesian coordinates, intersection point P = (3/3, 6/3) = (1, 2)
Verification: P satisfies both lines: 1(1) + (−2)(2) + 3 = 0, 4(1) + (−5)(2) + 6 = 0
MATLAB Code 6.9
%(a)
L = [3, 4, -10];
P1 = [0, 5, 1];
P2 = [2, 1, 1];
P3 = [10/3, 0, 1];
D1 = dot(L, P1);
if D1 = = 0 fprintf('Point P1 lies on line L\n'),
else fprintf('Point P1 does not lie on line L\n'), end;
D2 = dot(L, P2);
if D2 = = 0 fprintf('Point P2 lies on line L\n'),
else fprintf('Point P2 does not lie on line L\n'), end;
D3 = dot(L, P3);
if D3 = = 0 fprintf('Point P3 lies on line L\n'),
else fprintf('Point P3 does not lie on line L\n'), end;
%(b)
clear all;
P1 = [1, 8, 1];
P2 = [6, -7, 1];
Vectors ◾ 227
L = cross(P1, P2);
fprintf('Equation of required line : (%.2f)x + (%.2f)y + (%.2f) = 0\n', L(1), L(2), L(3));
%(c)
clear all;
aX + bY + cZ + dW = 0 (6.21)
This is called homogeneous plane equation (Marsh, 2005) and the vector N = (a, b, c , d ) is
denoted as plane vector.
Let P( X , Y , Z , W ) be a point on the plane, hence we must have:
e1 e2 e3 e4
X1 Y1 Z1 W1 (6.23)
N=
X2 Y2 Z2 W2
X 3 Y3 Z3 W3
where e1 = (1, 0, 0, 0), e2 = (0, 1, 0, 0), e3 = (0, 0, 1, 0), and e4 = (0, 0, 0, 1) are the unit vectors
along the four orthogonal directions.
Let three non-parallel planes N1 (a1, b1 , c1 , d1 ), N 2 (a2 , b2 , c2 , d2 ), and N 3 (a3 , b3 , c3 , d3 )
have an intersection point P. Since P lies on all planes we must have N1 • P = 0, N 2 • P = 0,
and N 3 • P = 0, which implies that vector P is perpendicular to all. The condition for this to
occur is given by the following vector determinant, where e1, e2, e3, and e4 are the unit vec-
tors along the four orthogonal directions:
e1 e2 e3 e4
a1 b1 c1 d1
P= (6.24)
a2 b2 c2 d2
a3 b3 c3 d3
230 ◾ Fundamentals of Graphics Using MATLAB®
Example 6.10
Solve the following using vector-based methods: (a) Find the plane connecting the
three points (1, 2, 3), (−4, −5, −6), and (7, 8, 9) (b) Find the point of intersection of
the three planes x + 2 y – 3z + 4 = 0, 3x + 4 y – 2z + 1 = 0, and 5x + 6 y − 4z + 3 = 0 .
(a)
Here, P1 = [1, 2, 3], P2 = [−4, −5, −6], and P3 = [7, 8, 9];
Let e1, e2, e3, and e4 be the unit vectors along the four orthogonal directions.
From Equation (6.23),
e1 e2 e3 e4 e e2 e3 e4
1
X1 Y1 Z1 W1 = 1 2 3 1
N=
X2 Y2 Z2 W2 −4 −5 −6 1
X 3 Y3 Z3 W3 7 8 9 1
2 3 1
d1 = det −5 −6 1 = 12
8 9 1
1 3 1
d 2 = − det −4 −6 1 = −24
7 9 1
1 2 1
d 3 = det −4 −5 1 = 12
7 8 1
1 2 3
d 4 = − det −4 −5 −6 = 0
7 8 9
Equation of plane: 12 x − 24 y + 12 z = 0
Verification: P1, P2, and P3 all satisfy this equation
Vectors ◾ 231
(b)
Here, N1 = [1, 2, −3, 4], N2 = [3, 4, −2, 1], and N3 = [5, 6, −4, 3];
Let e1 , e2 , e3 , and e4 be the unit vectors along the four orthogonal directions.
From Equation (6.24),
e1 e2 e3 e4 e1 e2 e3 e4
a1 b1 c1 d1 1 2 −3 4
P
= =
a2 b2 c2 d2 3 4 −2 1
a3 b3 c3 d3 5 6 −4 3
2 −3 4
d1 = det 4 −2 1 = −2
6 −4 3
1 −3 4
d 2 = − det 3 −2 1 = −2
5 −4 3
1 2 4
d 3 = det 3 4 1 = −10
5 6 3
1 2 −3
d 4 = − det 3 4 −2 = −6
5 6 −4
syms e1 e2 e3 e4;
P1 = [1, 2, 3];
P2 = [-4, -5, -6];
P3 = [7, 8, 9];
N = [e1, e2, e3, e4 ; P1(1), P1(2), P1(3), 1 ; P2(1), P2(2), P2(3), 1 ; P3(1), P3(2), P3(3), 1];
d1 =
double(d1); d2 = double(d2); d3 = double(d3); d4 = double(d4);
fprintf('Equation of plane : (%.2f)x + (%.2f)y + (%.2f)z + (%0.2f) = 0\n', d1, d2, d3, d4)
%(b)
clear all;
syms e1 e2 e3 e4;
NOTE
det: calculates determinant of a matrix
Vectors ◾ 233
234 ◾ Fundamentals of Graphics Using MATLAB®
dg ∂ f dx ∂ f dy
= . + . = 0 (6.25)
dt ∂ x P dt t0 ∂ y P dt t0
∂f ∂ f dx dy
, • , = 0 (6.26)
∂ x P ∂ y P dt t0 dt t0
The second term in the above equation gives the tangent vector and since the dot product
is zero the gradient vector is the normal vector perpendicular to the curve. Note that the
tangent vector can also be obtained by rotating the normal vector by 90°.
Example 6.11
For the curve x 2 + y 2 = 4 , find the normal vector and tangent vector at a point on the
curve. Also derive the equation of the tangent line through that point.
( )
Let f ( x , y ) = x 2 + y 2 − 4 and let P 1, 3 be a point on the curve.
∂f ∂f
Differentiating, ∇f = , = (2 x ,2 y )
∂ x ∂ y
Gradient at P: ∇f P ( )
= 2, 2 3 = N P (n1 , n2 ) which is the normal vector at P i.e.
N P = 2i + 3.46 j
The tangent vector is obtained by rotating the normal by 90° i.e.
0 −1 0 n1 −n2 −2 3
TP = R(90)* N = 1 0 0 n2 = n1 = 2 i.e. TP = −3.46i + 2 j
0 0 1 1 1 1
The tangent line equation can be obtained as the line through P with slope same as TP
( y − y0 ) = n1
( x − x0 ) −n2
%plotting
ezplot(f); hold on; grid;
plot(p(1), p(2), 'ro');
quiver(p(1), p(2), nv(1), nv(2));
ezplot(tl);
quiver(p(1), p(2), tv(1), tv(2));
axis equal; hold off;
NOTE
sind: calculates sine of an angle in degrees
cosd: calculates cosine of an angle in degrees
Vectors ◾ 235
236 ◾ Fundamentals of Graphics Using MATLAB®
0
y
-2
-4
-6
-6 -4 -2 0 2 4 6
x
∂f ∂f ∂ f dx dy dz
, , • , , = 0 (6.27)
∂ x P ∂ y P ∂ z P dt t0 dt t0 dt t0
The first set of terms in the above equation gives the normal vector N (n1 , n2 , n3 )
and the second set of terms gives the tangent vector T (t1 , t 2 , t3 ) at point ( x 0 , y0 , z 0 ),
whose dot product is zero. The equation of the tangent plane through P ( x 0 , y0 , z 0 ) is
given by:
n1 ( x − x 0 ) + n2 ( y − y0 ) + n3 ( z − z0 ) = 0 (6.28)
Example 6.12
For the surface x 2 + y 2 + z 2 = 12, find the normal vector and tangent plane at a point
on the surface.
Let f ( x , y ) = x 2 + y 2 + z 2 − 12 and P(2, 2, 2) be a point on the surface.
∂f ∂f ∂f
Differentiating, ∇f = , , = (2 x , 2 y , 2z )
∂ x ∂ y ∂z
Vectors ◾ 237
4( x − 2) + 4( y − 2) + 4( z − 2) = 0
%plotting
fimplicit3(f, ‘FaceColor’, ‘y’, ‘FaceAlpha’,0.3);
axis square; hold on;
plot3(p(1), p(2), p(3), ‘ro’)
quiver3(p(1), p(2), p(3), n(1), n(2), n(3));
238 ◾ Fundamentals of Graphics Using MATLAB®
NOTE
fimplicit3: generates a 3D plot of an implicit function
plot3: creates 3D graphical plots from a set of values
view: specifies the horizontal and vertical angles for viewing a 3D scene
Vectors ◾ 239
6.12 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
6.13 REVIEW QUESTIONS
1. For two vectors to be equal what conditions need to be fulfilled?
2. How can multiple vectors be combined to produce a resultant?
3. How can the magnitude and direction of a vector be computed?
4. How can the dot product of two vectors be used to determine whether they are
orthogonal?
240 ◾ Fundamentals of Graphics Using MATLAB®
6.14 PRACTICE PROBLEMS
1. Find relation between the vector p = ai + bj + ck and = (cos A)i + (cos B) j + (cos C )k,
where A, B, and C are the angles made by the vector p with the three primary axes.
2. Convert the Cartesian equation of the line 3x + 4 y = 12 to a vector equation.
3. Consider two lines A = i – j + 4k + s(i – j + k ) and B = 2i + 4 j + 7 k + t (2i + j + 3k ), where
s and t are the scaling factors. For what values of s and t do the lines intersect?
4. Find Cartesian equation of a line with vector equation r = (2i + 3 j – 4 k ) + t (3i – j + 2k ).
5. Consider the plane 3x + 4 y + 5 z = 12. Find the equation of the line going through the
point of intersection of the plane with the X- and Y-axes.
6. Find out if the line L : r = (3, 2, 5) + a ⋅ (5, − 5, 1) is perpendicular to the plane
P : r = (3, − 2, 5) + b ⋅ (3, 2, − 5) + c ⋅ (2, 3, 5), where a, b, and c are the scalars.
7. Where does the line r = (1, 3, 5) + t (2, − 4, 6) meet the plane x − 2 y + 3z = −4 ?
Find out if the line L : r = (1, 3, 8) + u(−2, 5, 7) is parallel to the plane
8.
P : r = (0.3, 0.25, − 0.5) + s(4, − 1, 2) + t (6, − 15, − 21).
9.
Find vector equation of line along which two planes
P1 : r = (2, 0, 0) + s1 (2, − 3, 0) + t1 (2, 0, − 4) and P2 : r = (5, 0, 0) + s2 (5, 1, 0) + t 2 (5, 0, 4)
meet.
10. Find the transformation that aligns the position vector (a) −4i + 5 j with positive
X-axis and (b) 4i + 5 j with negative Y-axis.
Chapter 7
3D Transformations
7.1 INTRODUCTION
Three dimensional transformations enable us to change the location, orientation, and
shapes of splines in 3D space. These transformations are translation, rotation, scaling,
reflection, and shear applied individually or in combination of two or more. Given known
coordinates of a point, each of these transformations is represented by a matrix which
when multiplied to the original coordinates give us a new set of coordinates. Similar to
the case of 2D transformations, we use homogeneous coordinates to derive transfor-
mation matrices. Coordinates of points are measured using a right-handed coordinate
system. Here, the location of each point is measured by three numbers representing coor-
dinates along an X-, Y-, and Z-axes mutually at right angles or 90°. The positive directions
of the axes are defined using the right-handed rule, which states that if the thumb, the
fore-finger, and the middle-finger of the right-hand are stretched so that they are mutu-
ally at right angles to each other, then the thumb denotes the positive direction of the
X-axis, the fore-finger denotes the positive direction of the Y-axis, and the middle-finger
denotes the positive direction of the Z-axis (O’Rourke, 2003). Angles are considered posi-
tive when measured in the counter-clockwise (CCW) direction observed from the tip of a
primary axis and negative in the clockwise (CW) direction. Along with the three primary
axes, there are three primary planes which together divide the coordinate space into eight
octants (see Figure 7.1). The X–Y plane (shown in green) is located at Z = 0 and divides the
space into top and bottom segments, the Y–Z plane (shown in red) is located at X = 0 and
divides the space into left and right segments, the X–Z plane (shown in yellow) is located at
Y = 0 and divides the space into front and rear segments. The three primary axes and the
three primary planes meet at the origin.
7.2 TRANSLATION
A translation operation changes the location of points and graphic objects by adding transla-
tion factors (t x , t y , t z ) to the X-, Y-, Z-coordinates of each point of the object (Hearn and Baker,
1996), (Shirley, 2002). If the factors are positive, the object moves along the positive direction
of coordinate axes, if they are negative, the object moves along the negative direction.
241
242 ◾ Fundamentals of Graphics Using MATLAB®
x2 1 0 0 t x x1
y2 = 0 1 0 t y y1 (7.1)
z2 0 0 1 t z z1
1 0 0 0 1 1
The inverse transformation is computed by taking the inverse of the matrix as below:
−1
x1 1 0 0 tx x2
y1 0 1 0 t y y2 (7.2)
=
z1 0 0 1 tz z 2
1 0 0 0 1 1
It can be verified that the inverse of the matrix is equal to the negative of the arguments.
−1
1 0 0 tx 1 0 0 −t x
0 1 0 ty 0 1 0 −t y
= (7.3)
0 0 1 tz 0 0 1 −t z
0 0 0 1 0 0 0 1
3D Transformations ◾ 243
T ′ (t x , t y , t z ) = T ( −t x , − t y , − t z )
As before, this is the convention followed throughout this book i.e. the operations them-
selves would be denoted by single letters such as T, S, R, and so on for translation, scal-
ing, and rotation while a specific matrix would be denoted with a letter with a subscript
e.g. T1. For example:
1 0 0 3
0 1 0 −4
T1 = T (3, − 4, 5) =
0 0 1 5
0 0 0 1
Example 7.1
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1) is translated by amounts (−2, −1, 3).
Find its new vertices.
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
1 0 0 −2
0 1 0 −1
Translation matrix: T1 = T (−2, − 1, 3) =
0 0 1 3
0 0 0 1
New coordinates are (−3, 0, 4), (−1, 0, 4), (−1, −2, 4), (−3, −2, 4), (−3, 0, 2), (−1, 0, 2),
(−1, −2, 2), and (−3, −2, 2) (Figure 7.2).
MATLAB® Code 7.1
clear all; clc;
p1 = [-1,1,1];
p2 = [1,1,1];
p3 = [1,-1,1];
p4 = [-1,-1,1];
p5 = [-1,1,-1];
p6 = [1,1,-1];
p7 = [1,-1,-1];
p8 = [-1,-1,-1];
C = [p1' p2' p3' p4' p5' p6' p7' p8' ;
1 1 1 1 1 1 1 1 ]
tx = -2; ty = -1; tz = 3;
T1 = [1 0 0 tx ; 0 1 0 ty ; 0 0 1 tz ; 0 0 0 1];
D = T1*C
244 ◾ Fundamentals of Graphics Using MATLAB®
%plotting
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
D = T1*C;
original
new
2
z
1
-1
1 1
0 0
-1
-1
-2
y -2 -3 x
7.3 SCALING
A scaling operation alters the size of graphic objects by multiplying the X-, Y-, Z-coordinates
of each point of the object by scaling factors s x , s y , sz. If scaling factors are less than 1, they
reduce the size of the object, if they are more than 1, they increase the size and if they are
equal to 1 they keep the size unaltered (Hearn and Baker, 1996), (Shirley, 2002). If the
factors are positive, the size increases along the positive direction of coordinate axes, if
they are negative, the size increases along the negative direction. If all factors are equal
then scaling is uniform otherwise non-uniform.
A point P ( x1 , y1 , z1 ) when scaled by amounts ( s x , s y , sz ) has new coordinates
Q ( x 2 , y 2 , z 2 ) given by:
x2 sx 0 0 0 x1
y2 = 0 sy 0 0 y1 (7.4)
z2 0 0 sz 0 z1
1 0 0 0 1 1
It can be verified that the inverse of the matrix is equal to the reciprocal of the arguments.
1
0 0 0
sx
−1
sx 0 0 0
1
0 sy 0 0 = 0 sy
0 0
0 0 sz 0 (7.5)
1
0 0 0 1 0 0 0
sz
0 0 0 1
246 ◾ Fundamentals of Graphics Using MATLAB®
1 1 1
Symbolically: S′ ( s x , s y , sz ) = S , ,
s x s y sz
The scaling operation pertaining to the above matrix is always with respect to the origin.
Example 7.2
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1) is scaled by amounts (2, 1, 3). Find
its new vertices.
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
2 0 0 0
0 1 0 0
Scaling matrix: S1 = S(2, 1, 3) =
0 0 3 0
0 0 0 1
From Equation (7.4), new coordinate matrix:
−2 2 2 −2 −2 2 2 −2
1 1 −1 −1 1 1 −1 −1
D = S1 * C =
3 3 3 3 −3 −3 −3 −3
1 1 1 1 1 1 1 1
New coordinates are (−2, 1, 3), (2, 1, 3), (2, −1, 3), (−2, −1, 3), (−2, 1, −3), (2, 1, −3),
(2, −1, −3), and (−2, −1, −3) (Figure 7.3).
original
new
0
z
-1
-2
-3 2
1
0
0
-1 -2 x
y
p1 = [-1,1,1];
p2 = [1,1,1];
p3 = [1,-1,1];
p4 = [-1,-1,1];
p5 = [-1,1,-1];
p6 = [1,1,-1];
p7 = [1,-1,-1];
p8 = [-1,-1,-1];
C = [p1' p2' p3' p4' p5' p6' p7' p8' ;
1 1 1 1 1 1 1 1 ]
sx = 2; sy = 1; sz = 3;
S1 =[sx 0 0 0 ; 0 sy 0 0 ; 0 0 sz 0 ; 0 0 0 1];
D = S1*C
%plotting
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
D = S1*C;
plot3(C(1,:), C(2,:), C(3,:), 'b'); hold on;
plot3(D(1,:), D(2,:), D(3,:), 'r');
xlabel('x'); ylabel('y'); zlabel('z');
3D Transformations ◾ 247
7.4 ROTATION
A rotation operation moves a point along the circumference of a circle centered at the
origin and radius equal to the distance of the point from the origin. Rotation is considered
positive when it is in the CCW direction and negative along the CW direction. Unlike the
2D case where there is a single rotation matrix, for 3D there are three different rotation
matrices depending on which of the three primary axes is the axis of rotation (Hearn and
Baker, 1996), (Shirley, 2002).
Rotation about X-axis:
1 0 0 0
0 cosθ − sinθ 0
Rx (θ ) = (7.6)
0 sinθ cosθ 0
0 0 0 1
cosθ 0 sinθ 0
0 1 0 0
R y (θ ) = (7.7)
− sinθ 0 cosθ 0
0 0 0 1
Rotation about Z-axis:
cosθ − sinθ 0 0
sinθ cosθ 0 0
Rz (θ ) = (7.8)
0 0 1 0
0 0 0 1
Rotation by default is always with respect to the origin around any of the three primary
axes.
Example 7.3
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1) is rotated by 45° about the X-axis.
Find its new vertices.
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
3D Transformations ◾ 249
Rotation matrix:
1 0 0 0 1 0 0 0
0 cos45 − sin 45 0 0 0.71 −0.71 0
Rx (45) = =
0 sin 45 cos45 0 0 0.71 0.71 0
0 0 0 1 0 0 0 1
New coordinate matrix:
−1 1 1 −1 −1 1 1 −1
0 0 −1.41 −1.41 1.41 1.41 0 0
D = Rx (45)* C =
1.41 1.41 0 0 0 0 −1.41 −1.41
1 1 1 1 1 1 1 1
New vertices: (−1.00, 0, 1.41), (1.00, 0, 1.41), (1.00, −1.41, 0), (−1.00, −1.41, 0), (−1.00,
1.41, 0), (1.00, 1.41, 0), (1.00, 0, −1.41), and (−1.00, 0, −1.41) (Figure 7.4)
original
new
0.5
0
z
-0.5
-1
1
0.5
0 1
-0.5
0
-1
y -1 x
p1 = [-1,1,1];
p2 = [1,1,1];
p3 = [1,-1,1];
p4 = [-1,-1,1];
p5 = [-1,1,-1];
p6 = [1,1,-1];
p7 = [1,-1,-1];
p8 = [-1,-1,-1];
C = [p1' p2' p3' p4' p5' p6' p7' p8' ;
1 1 1 1 1 1 1 1 ];
A = deg2rad(45);
R1 = [1 0 0 0 ; 0 cos(A) -sin(A) 0 ; 0 sin(A) cos(A) 0 ; 0 0 0 1];
D = R1*C;
250 ◾ Fundamentals of Graphics Using MATLAB®
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
D = R1*C;
7.5
FIXED-POINT SCALING
As mentioned previously, a scaling operation is by default about the origin. For a general
scaling operation with respect to a fixed point ( x f , y f , z f ), the following steps are taken:
Example 7.4
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1) is scaled by amounts (2, 1, 3) with
respect to its vertex (−1, −1, −1). Find its new vertices.
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
1 0 0 1
0 1 0 1
Forward translation: T1 = T (1, 1, 1) =
0 0 1 1
0 0 0 1
2 0 0 0
0 1 0 0
Scaling: S1 = S(2, 1, 3) =
0 0 3 0
0 0 0 1
1 0 0 −1
0 1 0 −1
Reverse translation: T2 = T (−1, −1, −1) =
0 0 1 −1
0 0 0 1
2 0 0 1
0 1 0 0
Composite transformation: M = T2 ⋅ S1 ⋅T1 =
0 0 3 2
0 0 0 1
252 ◾ Fundamentals of Graphics Using MATLAB®
original
new
2
z
-1 3
1 2
1
0
0
-1 -1 x
y
7.6
FIXED-POINT ROTATION
As mentioned previously, a rotation operation is by default with respect to the origin. For
a general rotation operation with respect to a fixed point ( x f , y f , z f ), the following steps
are taken:
Example 7.5
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1) is rotated by 45° with respect to its
vertex (−1, −1, −1) around Z-axis. Find its new vertices.
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
1 0 0 1
0 1 0 1
Forward translation: T1 = T(1, 1, 1) =
0 0 1 1
0 0 0 1
cos45 − sin 45 0 0
sin 45 cos45 0 0
Rotation about Z-axis: R1 = Rz (θ ) =
0 0 1 0
0 0 0 1
1 0 0 −1
0 1 0 −1
Reverse translation: T2 = T (−1, −1, −1) =
0 0 1 −1
0 0 0 1
original
new
0.5
0
z
-0.5
-1
1
1
0
0 -1
y -2
-1 x
Example 7.6
A point C(1, 1, 1) is to be rotated by 180° around a line parallel to the Y-axis joining
points P(5, 2, 3) and Q(5, 0, 3). Find its new coordinates.
Original coordinate matrix: C = [1, 1, 1, 1]T
Forward translation to coincide with Y-axis: T1 = T (−5, 0, −3)
Rotation around Y-axis: R1 = R y (180)
Reverse translation to original location: T2 = T (5, 0, 3)
Composite transformation: M = T (5, 0, 3) ⋅ R y (180) ⋅T (−5, 0, −3)
New coordinate matrix D = M · C = [9, 1, 5, 1]T
New coordinates: (9, 1, 5) (Figure 7.8)
3D Transformations ◾ 257
P (a, b, c )
3
z
2
Q (a, 0, c )
0
5
4 5
3 O (0, 0, 0 ) 4
2 3
2
1
1
y 0 0 x
original
new
3
z
2 8
6
1
2 4
1
2 x
0
y
%plotting
258 ◾ Fundamentals of Graphics Using MATLAB®
NOTE
line: Draws a line from one point to another
3D Transformations ◾ 259
3 P (a, b, c)
z
0
Q (a, b, 0)
5
4 5
3 O (0, 0, 0) 4
2 3
2
1 1
y 0 0 x
Next consider a straight line parallel to the Z-axis joining points P(a, b, c ) and Q(a, b, 0)
(see Figure 7.9). To derive the matrix for rotation by angle θ in the CCW direction about
this line, the following steps are followed:
Example 7.7
A point C(1, 1, 1) is to be rotated by 180° around a line parallel to the Z-axis joining
points P(5, 2, 3) and Q(5, 2, 0). Find its new coordinates.
Original coordinate matrix: C = [1, 1, 1, 1]T
Forward translation to coincide with Y-axis: T1 = T (−5, −2, 0)
Rotation around Y-axis: R1 = Rz (180)
Reverse translation to original location: T2 = T (5, 2, 0)
Composite transformation: M = T (5, 2, 0) ⋅ Rz (180) ⋅T (−5, −2, 0)
New coordinate matrix D = M · C = [9, 3, 1, 1]T
New coordinates: (9, 3, 1) (Figure 7.10)
MATLAB Code 7.7
clear all; clc;
p = [1,1,1];
C = [p' ; 1];
P = [5, 2, 3]; Q = [5, 2, 0];
tx = -Q(1); ty = -Q(2); tz = -Q(3);
T1 = [1 0 0 tx ; 0 1 0 ty ; 0 0 1 tz ; 0 0 0 1];
A = 180;
R1 = [cosd(A) -sind(A) 0 0 ; sind(A) cosd(A) 0 0 ; 0 0 1 0 ; 0 0 0 1];
T2 = inv(T1);
M = T2*R1*T1;
D = M*C;
%plotting
260 ◾ Fundamentals of Graphics Using MATLAB®
original
new
2
z
1 8
6
0
3 4
2 2
1 x
y
Finally, consider a straight line parallel to the X-axis joining points P(a, b, c ) and Q(0, b, c )
(see Figure 7.11). To derive the matrix for rotation by angle θ in the CCW direction about
this line, the following steps are followed:
3 P (a, b, c)
z
Q (0, b, c)
1
0
5
4 5
3 O (0, 0, 0) 4
2 3
2
1 1
y 0 0 x
original
new
3
z
1 4
3 3
2
2
1
1 0 x
y
Example 7.8
A point C(1, 1, 1) is to be rotated by 180° around a line parallel to the X-axis joining
points P(5, 2, 3) and Q(0, 2, 3). Find its new coordinates.
Original coordinate matrix: C = [1, 1, 1, 1]T
Forward translation to coincide with Y-axis: T1 = T (0, −2, −3)
Rotation around Y-axis: R1 = Rx (180)
Reverse translation to original location: T2 = T (0, 2, 3)
Composite transformation: M = T (0, 2, 3) ⋅ Rx (180) ⋅T (0, −2, −3)
New coordinate matrix: D = M · C = [1, 3, 5, 1]T
New coordinates: (1, 3, 5) (Figure 7.12)
MATLAB Code 7.8
clear all; clc;
p = [1,1,1];
C = [p' ; 1];
P = [5, 2, 3]; Q = [0, 2, 3];
tx = -Q(1); ty = -Q(2); tz = -Q(3);
T1 = [1 0 0 tx ; 0 1 0 ty ; 0 0 1 tz ; 0 0 0 1];
A = deg2rad(180);
R1 = [1 0 0 0 ; 0 cos(A) -sin(A) 0 ; 0 sin(A) cos(A) 0 ; 0 0 0 1];
T2 = inv(T1);
M = T2*R1*T1;
D = M*C;
%plotting
plot3(C(1,:), C(2,:), C(3,:), 'bo', 'MarkerFaceColor', 'b'); hold on;
plot3(D(1,:), D(2,:), D(3,:), 'ro', 'MarkerFaceColor', 'r'); grid;
line([5, 0], [2, 2], [3, 3], 'LineWidth', 2);
plot3(P(1), P(2), P(3), 'ko');
plot3(Q(1), Q(2), Q(3), 'ko');
xlabel('x'); ylabel('y'); zlabel('z');
legend('original', 'new'); axis equal; hold off;
3D Transformations ◾ 263
264 ◾ Fundamentals of Graphics Using MATLAB®
6
Y
5
4 C (0, b, c) P (a, b, c)
3
d e
2 b
1 b D (a, 0, 0)
a
0
O
-1 c c X
-2 B (0, 0, c) A (a, 0, c)
-3 Q (a, 0, d)
-4 Z
-5
-5 -4 -3 -2 -1 0 1 2 3 4 5
Since, OP and OQ are projected parallel to X–Z plane onto the Y–Z plane at OC and OB,
angle BOC is also α .
Since, C has coordinates (0, b, c ) and B has coordinates (0, 0, c ), length BC equals b.
Also since, B has coordinates (0, 0, c ) length OB equals c.
Let length OC be d = b 2 + c 2
Thus in triangle OBC, cos(α ) = OB/OC = c /d and sin(α ) = BC /OC = b/d
1 0 0 0 1 0 0 0
0 cos α − sin α 0 = 0 c /d −b/d 0
Hence Rx (α ) =
0 sin α cos α 0 0 b/d c /d 0
0 0 0 1 0 0 0 1
1 0 0 0 a a
0 c /d −b/d 0 b 0
Coordinates of Q = Rx (α ) ⋅ P = = i.e. (a, 0, d )
0 b/d c /d 0 c d
0 0 0 1 1 1
Step 2:
Rotate vector OQ by angle ϕ CW around Y-axis to coincide with Z-axis at R: R y (−ϕ ).
Note: CW rotation is considered negative (see Figure 7.14).
Construction: To find the value of ϕ in terms of (a, b, c ), the following set of construc-
tions are done.
d /e 0 −a/e 0 a 0
0 1 0 0 0 0
Coordinates of R = R y (−ϕ ) ⋅ Q = = i.e. (0, 0, e )
a/e 0 d /e 0 d e
0 0 0 1 1 1
266 ◾ Fundamentals of Graphics Using MATLAB®
D (a, 0,) 0
O X
c
e
B (0, 0,) c A (a, 0,) c
a
S (0, 0,) d Q (a, 0,) d
R (0, 0,) e
This matches our expectation since the length of vector OP is a 2 + b 2 + c 2 = e hence when
it is aligned along the Z-axis the tip of the vector should have coordinates (0, 0, e ).
Composite transformation: M = R y (−ϕ ) ⋅ Rx (α )
It can be verified that coordinates of the final vector OR can also be obtained by multi-
plying the original vector OP with the composite transformation matrix M i.e. R = M ⋅ P.
This is left as an exercise for the readers.
Example 7.9
Find the transformation for aligning the vector P = 2i + j + 2k with the positive
Z-axis. Also find the new vector after alignment.
For the given problem,
b c
sin(α ) = = 0.4472, cos(α ) = = 0.8944
d d
3D Transformations ◾ 267
a d
sin(ϕ ) = = 0.6667, cos(ϕ ) = = 0.7454
e e
1 0 0 0 1 0 0 0
0 cos α − sin α 0 0 0.8944 −0.4472 0
Rx (α ) = =
0 sin α cos α 0 0 0.4472 0.8944 0
0 0 0 1 0 0 0 1
P = [2; 1; 2; 1];
a = P(1);
b = P(2);
c = P(3);
d = sqrt(b^2 + c^2);
A = asin(b/d);
A = acos(c/d);
R1 = [1 0 0 0; 0 cos(A) -sin(A) 0; 0 sin(A) cos(A) 0; 0 0 0 1];
e = sqrt(a^2 + d^2);
B = asin(a/e);
R2 = [cos(B) 0 -sin(B) 0; 0 1 0 0; sin(B) 0 cos(B) 0; 0 0 0 1];
fprintf('Transformation matrix : \n');
M = R2*R1
fprintf('New vector : \n');
Q = M*P
268 ◾ Fundamentals of Graphics Using MATLAB®
In a similar fashion alignment with the X- and Y-axes can be likewise analyzed. These are left
for the reader as exercises. The final results are summarized below in Table 7.1 for convenience.
NOTE
asin: calculates inverse sine in radians.
Example 7.10
Find the transformation for aligning the following vectors with the positive X-axis.
Also find the new vector after alignment. (a) 2i + j + 2k and (b) 2i − j − 2k.
(a)
a = 2, b = 1, c = 2
d = b 2 + c 2 = 2.2361, e = a 2 + b 2 + c 2 = 3
d = b 2 + c 2 = 2.2361; e = a 2 + b 2 + c 2 = 3
3D Transformations ◾ 269
%(b)
clear all;
P = [2 ; -1 ; -2 ; 1];
a = 2; b = -1; c = -2;
d = sqrt(b^2 + c^2); e = sqrt(a^2 + b^2 + c^2);
sinA = b/d; cosA = c/d;
sinB = d/e; cosB = a/e;
Rx = [1 0 0 0 ; 0 cosA -sinA 0 ; 0 sinA cosA 0 ; 0 0 0 1];
Ry = [cosB 0 sinB 0 ; 0 1 0 0 ; -sinB 0 cosB 0 ; 0 0 0 1];
fprintf('Transformation matrix : \n');
M = Ry*Rx
fprintf('New vector : \n');
Q = M*P
270 ◾ Fundamentals of Graphics Using MATLAB®
Step 1: Align the vector along a primary axis (see Section 7.8)
Step 2: Rotate around that primary axis by the specified angle (see Section 7.4)
Step 3: Reverse align vector to its original location
Example 7.11
A point P(1, 2, 3) is to be rotated around vector V = 12i + 3 j + 4k by 90° in CCW
irection. Find its new coordinates. Verify the result by aligning the vector with each
d
of the three primary axes.
d = b 2 + c 2 = 5
b 3 c 4 d 5 a 12
sin α = = , cos α = = , sinϕ = = , cosϕ = =
d 5 d 5 e 13 e 13
1 0 0 0 cosϕ 0 sinϕ 0
0 cos α − sin α 0 , R y (ϕ ) = 0 1 0 0 ,
Rx (α ) =
0 sin α cos α 0 − sinϕ 0 cosϕ 0
0 0 0 1
0 0 0 1
1 0 0 0
Rx (θ ) = 0 cosθ − sinθ 0
0 sinθ cosθ 0
0 0 0 1
M = Rx (−α ) ⋅ R y (−ϕ ) ⋅ Rx (θ ) ⋅ R y (ϕ ) ⋅ Rx (α )
Q = M ⋅ P → (2.2071, −1.9290,2.3254)
d = a 2 + c 2 = 4 10
3D Transformations ◾ 271
c 1 a 3 d 4 10 b 3
sin α = = , cos α = = , sinϕ = = , cosϕ = =
d 10 d 10 e 13 e 13
cosθ 0 sinθ 0
0 1 0 0
R y (θ ) =
− sin θ 0 cosθ 0
0 0 0 1
M = R y (−α ) ⋅ Rz (−ϕ ) ⋅ R y (θ ) ⋅ Rz (ϕ ) ⋅ R y (α )
Q = M ⋅ P → (2.2071, −1.9290,2.3254)
b2 + c 2 = 5
b 3 c 4 a 12 d 5
sin α = = , cos α = = , sinϕ = − = − , cosϕ = =
d 5 d 5 e 13 e 13
1 0 0 0 cosϕ 0 sinϕ 0
0 cos α − sin α 0 , R y (−ϕ ) = 0 1 0 0 ,
Rx (α ) =
0 sin α cos α 0 − sin ϕ 0 cosϕ 0
0 0 0 1
0 0 0 1
cosθ − sinθ 0 0
Rz (θ ) = sinθ cosθ 0 0
0 0 1 0
0 0 0 1
M = Rx (−α ) ⋅ R y (−ϕ ) ⋅ Rz (θ ) ⋅ R y (ϕ ) ⋅ Rx (α )
Q = M ⋅ P → (2.2071, −1.9290,2.3254)
272 ◾ Fundamentals of Graphics Using MATLAB®
d = sqrt(b^2 + c^2);
sinA = b/d; cosA = c/d;
sinB = d/e; cosB = a/e;
R1 = [1 0 0 0 ; 0 cosA -sinA 0 ; 0 sinA cosA 0 ; 0 0 0 1];
R2 = [cosB 0 sinB 0 ; 0 1 0 0 ; -sinB 0 cosB 0 ; 0 0 0 1];
Rx = [1 0 0 0 ; 0 cos(C) -sin(C) 0 ; 0 sin(C) cos(C) 0 ; 0 0 0 1];
R4 = inv(R2);
R5 = inv(R1);
Mx = R5*R4*Rx*R2*R1;
fprintf('New coordinates : \n');
Qx = Mx*P
d = sqrt(b^2 + c^2);
sinA = b/d; cosA = c/d;
sinB = -a/e; cosB = d/e;
R1 = [1 0 0 0 ; 0 cosA -sinA 0 ; 0 sinA cosA 0 ; 0 0 0 1];
R2 = [cosB 0 sinB 0 ; 0 1 0 0 ; -sinB 0 cosB 0 ; 0 0 0 1];
Rz = [cos(C) -sin(C) 0 0 ; sin(C) cos(C) 0 0 ; 0 0 1 0 ; 0 0 0 1];
R4 = inv(R2);
R5 = inv(R1);
Mz = R5*R4*Rz*R2*R1;
fprintf('New coordinates : \n');
Qz = Mz*P
d = sqrt(a^2 + c^2);
sinA = c/d; cosA = a/d;
sinB = d/e; cosB = b/e;
R1 = [cosA 0 sinA 0 ; 0 1 0 0 ; -sinA 0 cosA 0 ; 0 0 0 1];
R2 = [cosB -sinB 0 0 ; sinB cosB 0 0 ; 0 0 1 0 ; 0 0 0 1];
Ry = [cos(C) 0 sin(C) 0 ; 0 1 0 0 ; -sin(C) 0 cos(C) 0 ; 0 0 0 1];
R4 = inv(R2);
R5 = inv(R1);
3D Transformations ◾ 273
My = R5*R4*Ry*R2*R1;
fprintf('New coordinates : \n');
Qy = My*P
Step 1: Translate line so that one end coincides with origin (see Section 7.2)
Step 2: Align the resulting vector along a primary axis (see Section 7.8)
Step 3: Rotate around that primary axis by the given amount (see Section 7.4)
Step 4: Reverse align vector to its original location
Step 5: Reverse translate line to original location
Example 7.12
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), (−1, −1, −1) is rotated by 45° CCW around an arbitrary
line joining points P(2, 1, −2) and Q(3, 3, 2) along a plane perpendicular to the line.
Find its new vertices.
Here, x1 = 2, y1 = 1, z1 = −2, x 2 = 3, y 2 = 3, z 2 = 2, angle of rotation θ = 45°
Translate axis of rotation such that P coincides with the origin:
Translation matrix T1 = T ( − x1 , − y1 , − z1 )
Align the resulting vector along X-axis:
The coordinates of the vector tip: a = x 2 − x1 = 1, b = y 2 − y1 = 2, c = z 2 − z1 = 4
Then d = b 2 + c 2 = 4.4721, e = a 2 + b 2 + c 2 = 4.5826
b c d a
sin α = = 0.4472, cos α = = 0.8944, sinϕ = = 0.9759, cosϕ = = 0.2182
d d e e
1 0 0 0 cosϕ 0 sinϕ 0
0 cos α − sin α 0 , R2 = R y (ϕ ) = 0 1 0 0
R1 = Rx (α ) =
0 sin α cos α 0 − sinϕ 0 cosϕ 0
0 0 0 1
0 0 0 1
1 0 0 0
0 cosθ − sinθ 0
Rotate about the origin around X-axis by angle θ: Rx (θ ) =
0 sinθ cosθ 0
0 0 0 1
274 ◾ Fundamentals of Graphics Using MATLAB®
original
new
axis
0
z
-2
-1
3
0 2
1 1
0
2
-1
x 3 -2 y
x1 = 2; y1 = 1; z1 = -2;
x2 = 3; y2 = 3; z2 = 2;
a = x2 - x1 ; b = y2 - y1 ; c = z2 - z1;
d = sqrt(b^2 + c^2);
e = sqrt(a^2 + b^2 + c^2);
N = pi/4;
tx = -x1; ty = -y1 ; tz = -z1;
T1 = [1, 0, 0, tx ; 0, 1, 0, ty ; 0, 0, 1, tz ; 0, 0, 0, 1];
D = M*C;
fprintf('New vertices : \n')
for i=1:8
fprintf('(%.2f, %.2f, %.2f) \n',D(1,i), D(2,i), D(3,i));
end
%plotting
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
D = M*C;
plot3(C(1,:), C(2,:), C(3,:), 'b'); hold on;
plot3(D(1,:), D(2,:), D(3,:), 'r'); grid;
xlabel('x'); ylabel('y'); zlabel('z');
plot3([x1, x2],[y1, y2],[z1, z2], 'b', 'LineWidth', 2);
plot3(x1, y1, z1, 'bo', x2, y2, z2, 'bo');
legend('original', 'new', 'axis'); axis equal;
276 ◾ Fundamentals of Graphics Using MATLAB®
7.11 REFLECTION
Reflection about a primary plane reverses the coordinate value along an axis perpendicu-
lar to the plane (Hearn and Baker, 1996). Reflection along X-axis is equivalent to reflec-
tion about YZ-plane and reverses the x-coordinate of a point. The corresponding matrix
is given by:
−1 0 0 0
0 1 0 0
Fx = (7.9)
0 0 1 0
0 0 0 1
Reflection along Y-axis is equivalent to reflection about XZ-plane and reverse the
y-coordinate of a point. The corresponding matrix is given by:
1 0 0 0
0 −1 0 0
Fy = (7.10)
0 0 1 0
0 0 0 1
Reflection along Z-axis is equivalent to reflection about XY-plane and reverse the
z-coordinate of a point. The corresponding matrix is given by:
1 0 0 0
0 1 0 0
Fz = (7.11)
0 0 −1 0
0 0 0 1
If the plane of reflection is not a primary plane then it is first made to coincide with one of
the primary planes and then the above formulas are applied.
Example 7.13
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), (−1, −1, −1) is to be reflected about a plane z = 3
parallel to the X–Y plane along the Z-axis. Find its new vertices.
Translate plane so as to coincide with XY-plane: T1 = T (0, 0, −3)
Reflect about XY-plane i.e. along Z-axis: Fz
Reverse translate plane to original location: T2 = T (0, 0, 3)
Composite transformation: M = T2 ⋅ Fz ⋅T1
278 ◾ Fundamentals of Graphics Using MATLAB®
original
7 new
3
z
2
-1
1
0 1
0
-1 -1
y x
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
New coordinate matrix: D = M ⋅ C
New coordinates: (−1, 1, 5), (1, 1, 5), (1, −1, 5), (−1, −1, 5), (−1, 1, 7), (1, 1, 7), (1, −1, 7),
and (−1, −1, 7) (Figure 7.16).
MATLAB Code 7.13
clear all; clc;
p1 = [-1,1,1];
p2 = [1,1,1];
p3 = [1,-1,1];
p4 = [-1,-1,1];
p5 = [-1,1,-1];
p6 = [1,1,-1];
p7 = [1,-1,-1];
p8 = [-1,-1,-1];
C = [p1' p2' p3' p4' p5' p6' p7' p8' ; 1 1 1 1 1 1 1 1 ];
tx = 0; ty = 0; tz = -3;
T1 = [1, 0, 0, tx ; 0, 1, 0, ty ; 0, 0, 1, tz ; 0, 0, 0, 1];
Fz = [1, 0, 0, 0 ; 0, 1, 0, 0 ; 0, 0, -1, 0 ; 0, 0, 0, 1];
T2 = inv(T1);
M = T2 * Fz * T1;
D = M*C;
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
D = M*C;
plot3(C(1,:), C(2,:), C(3,:), 'b'); hold on;
plot3(D(1,:), D(2,:), D(3,:), 'r'); grid;
xlabel('x'); ylabel('y'); zlabel('z')
3D Transformations ◾ 279
7.12 SHEAR
Involves changing coordinate values along one axis by adding an amount proportional to
coordinate values along another axis (Hearn and Baker, 1996). Shear along each axis can
be of two types:
Shear along X-axis can be parallel to Y = 0 plane (front and back faces) governed by the
relation x 2 = x1 + h ⋅ y , where h is the constant of proportionality. The corresponding trans-
formation matrix is:
1 h 0 0
0 1 0 0
H xy = (7.12)
0 0 1 0
0 0 0 1
Shear along X-axis can also be parallel to Z = 0 plane (top and bottom faces) governed by
the relation x 2 = x1 + h ⋅ z . The corresponding transformation matrix is:
1 0 h 0
0 1 0 0
H xz = (7.13)
0 0 1 0
0 0 0 1
Figure 7.17 illustrates the two types of shear along X-axis. These types are, however, not
mutually exclusive, both can occur simultaneously. The transformation matrix in that case
becomes:
1 h1 h2 0
0 1 0 0
Hx = (7.14)
0 0 1 0
0 0 0 1
FIGURE 7.17 Shear along X-axis parallel to (a) front and back faces and (b) top and bottom faces.
3D Transformations ◾ 281
In a likewise manner, shear along Y-axis can be along X = 0 plane and Z = 0 plane.
The general transformation matrix is of the form:
1 0 0 0
h1 1 h2 0
Hy = (7.15)
0 0 1 0
0 0 0 1
Shear along Z-axis can be along X = 0 plane and Y = 0 plane. The general transformation
matrix is of the form:
1 0 0 0
0 1 0 0
Hz = (7.16)
h1 h2 1 0
0 0 0 1
Example 7.14
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1), (−1,
1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1) is subjected to a shear along the Z-axis
with parameters (1.2, 2.3). Find its new vertices.
1 0 0 0
0 1 0 0
From Equation (7.16), H z =
1.2 2.3 1 0
0 0 0 1
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
New coordinate matrix: D = H z ⋅ C
New vertices: (−1.00, 1.00, 2.10), (1.00, 1.00, 4.50), (1.00, −1.00, −0.10), (−1.00, −1.00,
−2.50), (−1.00, 1.00, 0.10), (1.00, 1.00, 2.50), (1.00, −1.00, −2.10), and (−1.00, −1.00,
−4.50) (Figure 7.18)
MATLAB Code 7.14
clear all; clc;
h1 = 1.2; h2 = 2.3;
H = [1, 0, 0, 0 ; 0, 1, 0, 0 ; h1, h2, 1, 0 ; 0, 0, 0, 1];
p1 = [-1,1,1];
p2 = [1,1,1];
p3 = [1,-1,1];
p4 = [-1,-1,1];
p5 = [-1,1,-1];
p6 = [1,1,-1];
p7 = [1,-1,-1];
p8 = [-1,-1,-1];
C = [p1' p2' p3' p4' p5' p6' p7' p8' ; 1 1 1 1 1 1 1 1 ];
D = H*C;
for i=1:8
fprintf('(%.2f, %.2f, %.2f) \n',D(1,i), D(2,i), D(3,i));
end
%plotting
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
D = H*C;
plot3(C(1,:), C(2,:), C(3,:), 'b--'); hold on;
plot3(D(1,:), D(2,:), D(3,:), 'b-', 'LineWidth', 1.5); grid;
xlabel('x'); ylabel('y'); zlabel('z');
legend('original', 'new');
axis([-5 5 -5 5 -5 5]); hold off;
3D Transformations ◾ 283
original
new
0
z
-5
5
4 5
2 3
0 1
-1 0
-3 -2
y -5 -5 -4
x
7.13 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
• Rotation around a vector involves aligning the vector to a primary axis, rotating
about that axis and reverse aligning the vector to its original location.
• Rotation around an arbitrary line involve translating it to the origin, aligning it to a
primary axis, rotating about that axis, reverse aligning, and reverse translating to its
original location.
• Reflection about a primary plane reverses the coordinate value along an axis perpen-
dicular to the plane.
• Shear involves changing coordinate values along one axis by adding an amount
proportional to coordinate values along other axes.
7.14 REVIEW QUESTIONS
1. What is meant by the right-handed coordinate system for 3D space?
2. Why are there three different rotation matrices for 3D space?
3. What are meant by positive and negative translation factors?
4. What are meant by positive and negative scaling factors?
5. What is considered as a positive direction of rotation around any of the primary axes?
6. How are scaling and rotations with respect to an arbitrary point calculated?
7. How is the rotation matrix parallel to a primary axis computed?
8. How is a vector aligned to a primary axis?
9. How is reflection parallel to a primary plane calculated?
10. Why does shear operation along a primary axis involve two different options?
7.15 PRACTICE PROBLEMS
1. A point with coordinates (2, 2, 2) is to be rotated by 45° about the Y-axis and then by
60° about the Z-axis. Find its new coordinates. Check if the final position of the point
is same or different if it is first rotated by 60° about the Z-axis and then by 45° about
the Y-axis.
2. Find the new coordinates of a point P(k , − k , k ) when rotated clockwise about the
origin on the X–Z plane by 30°, where k is a constant.
3. A line is parallel to the Y-axis and joins point P(k , − k , k ) and Q(k , 0, k ), where k is
a constant. Derive the transformation matrix for rotation by angle θ about the line.
Assume sin(θ) = 0.76 and cos(θ) = 0.65.
4. A point P(1, 2, 3) is to be rotated by 77° CW about a line joining points M (2, 1, 0) and
N (3, 3, 1). Calculate the new coordinates of the point.
3D Transformations ◾ 285
5. Find transformation matrix of reflection with respect to the plane passing through
the origin and having normal vector N = i + j + k.
6. Find transformation matrix of reflection with respect to the plane: 2 x – y + 2z – 2 = 0.
7. Consider a cube with a vertex at the origin and length of side 1. It is first subjected to
a shear x 2 = x1 + ay1 and then to another shear y 2 = y1 + bx1, where a = 2 and b = 3. Find
the final coordinates of the vertices of the cube.
8. Find the transformations that align the following vectors with the positive Z-axis:
(a) 2i + j + 2k, (b) 2i − j + 2k, and (c) 2i − j − 2k.
9. Obtain a transformation that aligns the vector k with the vector i + j + k.
10. Show that new coordinates of a point P(k , −2k , √ 3k ) when rotated about Y-axis by
30° and then about Z-axis by −30° is given by the following, where k is a constant:
Q(k /2, −3 √ 3k /2, k ).
Chapter 8
Surfaces
8.1 INTRODUCTION
Surfaces define the shape and contour of the 3D graphical objects and models. The most
basic type of surface is a flat surfaces commonly represented by a plane. Planes have been
discussed in Chapter 6. In this chapter, we will focus on curved surfaces. Curved surfaces
are usually modeled using two splines u and v in orthogonal directions. Depending on the
types of splines used surfaces may be named accordingly e.g. Bezier surfaces or B-spline
surfaces. Spline-based surfaces often have a grid of control points associated with them
using which the shape of the surface may be modified (Foley et al., 1995). Figure 8.1 shows
a surface composed of a quadratic curve along u and a cubic curve along v. Apart from the
surface structure, this chapter also takes a look at surface appearances. Surface appear-
ances are determined by texture and lighting. Texture mapping provides a realistic look
15
10
-5
-10
2
1 2
0 1
0
-1
-1
v -2 -2 u
287
288 ◾ Fundamentals of Graphics Using MATLAB®
to a surface and is extensively used in 3D graphical models e.g. wooden table or metal
plate. Lighting determine the brightness and shading aspects of a surface. Surfaces can be
categorized into two broad types based on their mathematical representation: implicit and
parametric. Implicit surfaces have equations of the form f ( x , y , z ) = 0 while parametric
surfaces are represented using parametric variables viz. {x (u, v ), y(u, v ), z (u, v )}. Surfaces
can also be categorized based on methods of generation e.g. extrusion and revolution.
8.2 PARAMETRIC SURFACES
Recall from Chapter 1 that a 2D curve in parametric form is represented as C(t ) = {x (t ), y(t )},
where t is the parametric variable defined over a specified interval. This essentially implies
that as t takes on different values over this range, functions x (t ) and y(t ) generate values
along the X- and Y-axes of a 2D graph, both of which together determine the locus of the
curve C(t ). For example, C(t ) = {r ⋅ cos t , r ⋅ sin t } represents a circle of radius r on a 2D plane
as t varies from 0 to 2π. A parametric surface is an extension of this concept to 3D. Instead
of a single variable t now we have two parametric variables u, v and instead of two func-
tions x (t ), y(t ), we have three functions x (u, v ), y(u, v ), and z (u, v ), which together gener-
ate a surface S(u, v ) in 3D space. In some books, the parametric variables are referred to as
s and t instead of u and v.
Any point on the surface is therefore decided by three values along three orthogonal axes.
If the functions are of degree 1 then the resulting surface is flat; otherwise, they are curves.
To visualize a parametric surface consider the functions: x = u + v , y = u − v , z = abs(u + v ).
A 2D plot of the X–Y plane is shown in Figure 8.2a for the range −2 ≤ u, v ≤ 2, which depicts
a plane surface in the shape of a quadrilateral. For each point on the plane surface, if the
third value is plotted along the Z-axis then the resulting 3D plot is shown in Figure 8.2b.
It depicts two planes intersecting on the X–Y plane.
If the functions are of degree more than 1 then the resulting surface will in gen-
eral be curves. Figure 8.3 shows the surface generated from the parametric functions:
x = u 2 , y = 2uv , z = v 2 .
Example 8.1
(a) x = u - v, y = u + v, z = abs(u + v)
4
0
y
-1
-2
-3
-4
-4 -3 -2 -1 0 1 2 3 4
x
(b) x = u - v, y = u + v, z = abs(u + v)
4
2
2
z
0 0
4
2
-2
0
x
-2
y -4
-4
x = u2, y = 2 u v, z = v2
2
z
0
4
10
3
5
2
0
-5 1
y x
-10 0
u − v
u + v
Original curve: P = 2
u − v 2
1
u − v − 5
u − v + 3
Modified curve: Q = M * P = 2
u − v 2 + 4
1
Original
40
20
0
z
-20
-40
10
-5
10 15
y -10 5
-5 0
-15 -10
x
New
50
0
z
-50
20
10
5 10
y 0
-10 -5
-10 -15
-20 x
view(-13, 48);
NOTE
ezmesh: creates a mesh for the function z = f (x, y)
Surfaces ◾ 293
8.3 BEZIER SURFACES
A Bezier surface is created from Bezier splines and is essentially a parametric surface
(Hearn and Baker, 1996). However, Bezier surfaces are most often defined based on their
associated control points. A Bezier curve using a degree 1 polynomial can be represented
P0
as: f (t ) = (1 − t ) ⋅ P0 + t ⋅ P1 = [ 1 − t , t ] .
P1
A bi-linear Bezier surface is generated using two first degree Bezier splines along
orthogonal directions and has the equation shown, below where P00 and P01 are the control
points of the first spline and P10 and P11 are the control points of the second spline.
P00 P01 1− v
S(u, v ) = [ 1 − u, u ] (8.2)
P10 P11 v
Example 8.2
Find the equation of a bi-linear Bezier surface using the following control points:
P00 = (0, 0, 1), P01 = (1, 1, 1), P10 = (1, 0, 0), P11 = (0, 1, 0)
From Equation (8.2),
0 1 1− v
x (u, v ) = [ 1 − u, u ] = u + v − 2uv
1 0 v
0 1 1− v
y(u, v ) = [ 1 − u, u ] = v
0 1 v
1 1 1− v
z (u, v ) = [ 1 − u, u ] = 1 − u
0 0 v
0.8
0.6 P00
z
0.4
0.2
0 P10
1
P11
1
0.8
0.5 0.6
0.4
0.2
y 0 0 x
syms u v;
P0
A degree 2 Bezier curve is given by the equation: (1 − t )2 ,
2t (1 − t ), t P1 . 2
P2
A bi-quadratic Bezier surface is, therefore, given by the following where a grid of 3 by 3 or
9 control points need to be specified.
Example 8.3
A bi-quadratic Bezier surface has the following control points: P00 = (1, −1, 0);
P01 = (4, 3, 0); P02 = (5, −2, 0); P10 = (1, 1, 3); P11 = (3, 2, 3); P12 = (5, 1, 3); P20 = (1, −1, 5);
P21 = (4, 3, 5); P22 = (5, −2, 5). Find its equation.
From Equation (8.3),
1 4 5 (1 − v )2
1
x (u, v ) = (1 − u)2 , 2u(1 − u), u2 3 5 2v(1 − v )
1 4 5 v2
−1 3 −2 (1 − v )2
1
y(u, v ) = (1 − u)2 , 2u(1 − u), u2 2 1 2v(1 − v )
−1 3 −2 v2
0 0 0 (1 − v )2
3
z (u, v ) = (1 − u)2 , 2u(1 − u), u2 3 3 2v(1 − v ) = −u(u − 6)
5 5 5 v2
The required surface equation is S(u, v ) = {x (u, v ), y(u, v ), z (u, v )} (Figure 8.6).
MATLAB Code 8.3
clear all; clc;
P00 = [1, -1, 0]; P01 = [4 3, 0]; P02 = [5, -2, 0];
P10 = [1, 1, 3]; P11 = [3 2, 3]; P12 = [5, 1, 3];
P20 = [1, -1, 5]; P21 = [4 3, 5]; P22 = [5, -2, 5];
syms u v;
= = =
x = [(1 - u)^2, 2*u*(1 - u), u^2]*[x00 x01 x02 ; x10 x11 x12; x20 x21 x22]...
*[(1 - v)^2 ; 2*v*(1 - v) ; v^2] ; x = simplify (x)
y = [(1 - u)^2, 2*u*(1 - u), u^2]*[y00 y01 y02 ; y10 y11 y12; y20 y21 y22]...
*[(1 - v)^2 ; 2*v*(1 - v) ; v^2] ; y = simplify (y)
z = [(1 - u)^2, 2*u*(1 - u), u^2]*[z00 z01 z02 ; z10 z11 z12; z20 z21 z22]...
*[(1 - v)^2 ; 2*v*(1 - v) ; v^2] ; z = simplify (z)
ezmesh(x, y, z, [0,1,0,1]); hold on;
%plotting control points
plot3([P00(1) P01(1) P02(1)], [P00(2) P01(2) P02(2)], [P00(3) P01(3) P02(3)], 'b-')
plot3([P00(1) P01(1) P02(1)], [P00(2) P01(2) P02(2)], [P00(3) P01(3) P02(3)], 'bo')
plot3([P10(1) P11(1) P12(1)], [P10(2) P11(2) P12(2)], [P10(3) P11(3) P12(3)], 'b-')
plot3([P10(1) P11(1) P12(1)], [P10(2) P11(2) P12(2)], [P10(3) P11(3) P12(3)], 'bo')
plot3([P20(1) P21(1) P22(1)], [P20(2) P21(2) P22(2)], [P20(3) P21(3) P22(3)], 'b-')
plot3([P20(1) P21(1) P22(1)], [P20(2) P21(2) P22(2)], [P20(3) P21(3) P22(3)], 'bo')
plot3([P00(1) P10(1) P20(1)], [P00(2) P10(2) P20(2)], [P00(3) P10(3) P20(3)], 'b-')
plot3([P02(1) P12(1) P22(1)], [P02(2) P12(2) P22(2)], [P02(3) P12(3) P22(3)], 'b-')
plot3([P01(1) P11(1) P21(1)], [P01(2) P11(2) P21(2)], [P01(3) P11(3) P21(3)], 'b-')
view(157, -14);
hold off;
Surfaces ◾ 297
298 ◾ Fundamentals of Graphics Using MATLAB®
3
z
4
0 2
5 4 0
3 2 1 -2
y
x
8.4 IMPLICIT SURFACES
Apart from parametric surfaces, the other type of surfaces most frequently encountered
in graphics is called implicit surfaces and their equations are of the form f ( x , y , z ) = 0.
The second-degree implicit equations are also called quadric surfaces (Hearn and Baker,
1996), (Rovenski, 2010) and have a general form shown below, where A to J are constants
(Figures 8.7–8.11):
Ellipsoid: ax 2 + by 2 + cz 2 = k (8.5)
ellipsoid
0
z
-1
-2
2
2
0
0
y -2 -2 x
elliptic cone
0
z
-1
-2
-3
2
2
0
0
-2 -2
y x
hyperboloid 1 sheet
0
z
-1
-2
-3
2
2
0
0
-2 -2
y x
hyperboloid 2 sheets
0
z
-1
-2
-3
2
2
0
0
-2 -2
y x
elliptic paraboloid
2
z
1
1
0
0
-1
-1
y x
hyperbolic paraboloid
0
z
-1
-2
-3
2
2
0
0
-2 -2
y x
elliptic cylinder
0
z
-1
-2
-3
2
2
0
0
y -2 -2
x
hyperbolic cylinder
0
z
-1
-2
-3
2 2
0 0
-2
y -2 x
parabolic cylinder
0
z
-1
-2
-3
2
1
0 0
-1
-2
y -2 -3 x
parabolic cone
0
z
-1
-2
-3
2
2
0 0
-2 -2
y x
Example 8.4
Find the point(s) of intersection between the hyperboloid 2x 2 + 3 y 2 − z 2 = 16 and the
line (t − 4, t – 5, t + 10)
For any point on the line: x = t − 4, y = t –5, z = t + 10
At point of intersection this also should satisfy the equation of the surface
Substituting: 2(t − 4)2 + 3(t –5)2 − (t + 10)2 = 16
Simplifying: 4t 2 – 66t − 9 = 0 whose solutions are t = −0.1353, 16.6353
Substituting, points of intersection are (−4.135255, −5.135255, 9.864745) and
(12.635255, 11.635255, 26.635255)
Verification (Figure 8.12):
40
50
20
0
z
-50
-20
-60
-40
-20 -40
0 y
20 -60
x 40
NOTE
ezplot3: plots symbolic variables directly in 3D environments
306 ◾ Fundamentals of Graphics Using MATLAB®
Surfaces ◾ 307
8.5 EXTRUDED SURFACES
Extruded surfaces are created when a 2D plane curve is moved along a straight line
in a direction perpendicular to the plane (O’Rourke., 2003). If the generating curve is
represented as C(u) = {x (u), y(u)} then the resulting surface is expressed in parametric
form:
Example 8.5
Find the extruded surface produced from the generating curves: (a) x = sin(u),
y = cos(u) and (b) x = sin(2u), y = sin(u)
x1 = sin(u); y1 = cos(u); z1 = v;
C1 = [x1, y1]
S1 = [x1, y1, z1]
x2 = sin(2*u); y2 = sin(u); z2 = v;
C2 = [x2, y2]
S2 = [x2, y2, z2]
8.6 SURFACES OF REVOLUTION
Surfaces of revolution are created when a 2D plane curve is rotated about an axis (O’Rourke.,
2003), (Rovenski, 2010). If the generating curve is represented as C(u) = {x (u), y(u)},
then the resulting surface is expressed in parametric form as:
Example 8.6
Show that the revolution of a straight line can be used to produce a cylinder and
cone, and the revolution of a circle can be used to produce a sphere and torus. Find
the equations of resulting surfaces. Also generate a surface from a rotating sinusoid.
Cylinder:
Curve: x = 2, y = u, C(u) = {x (u), y(u)}
Surface: S(u, v ) = {2 ⋅ cos(v ), 2 ⋅ sin(v ), u}
Cone:
Curve: x = u, y = u, C(u) = {x (u), y(u)}
Surface: S(u, v ) = {u ⋅ cos(v ), u ⋅ sin(v ), u}
Surfaces ◾ 309
Sphere:
Curve: x = cos(u), y = sin(u), C(u) = {x (u), y(u)}
Surface: S(u, v ) = {cos(u) × cos(v ), cos(u) ⋅ sin(v ), sin(u)}
Torus:
Curve: x = 2 + cos(u), y = 2 + sin(u), C(u) = {x (u), y(u)}
Surface: S(u, v ) = {(2 + cos(u)) ⋅ cos(v ), (2 + cos(u)) ⋅ sin(v ), (2 + sin(u))}
Sinusoid
Curve: x = 2 + sin(u), y = u, C(u) = {x (u), y(u)}
Surface: S(u, v ) = {(2 + sin(u)) ⋅ cos(v ), (2 + sin(u)) ⋅ sin(v ), u}
Curve: x = 2 + cos(u), y = u, C(u) = {x (u), y(u)}
Surface: S(u, v ) = {(2 + cos(u)) ⋅ cos(v ), (2 + cos(u)) ⋅ sin(v ), u}
(Figures 8.14–8.16)
FIGURE 8.14 Plots for Example 8.6: (a) cylinder; (b) cone.
310 ◾ Fundamentals of Graphics Using MATLAB®
FIGURE 8.15 Plots for Example 8.6: (a) sphere; (b) torus
% cylinder
x = 2; y = u;
C = [x, y];
S = [x*cos(v), x*sin(v), y];
figure,
subplot(121), ezplot(C(1), C(2)); axis square;
subplot(122), ezmesh(S(1), S(2), S(3)); axis equal;
colormap(jet);
% cone
x = u; y = u;
C = [x, y];
S = [x*cos(v), x*sin(v), y];
figure,
subplot(121), ezplot(C(1), C(2)); axis square;
Surfaces ◾ 311
FIGURE 8.16 Plots for Example 8.6: (a) sine; (b) cosine.
% sphere
x = cos(u); y = sin(u);
C = [x, y];
S = [x*cos(v), x*sin(v), y];
figure,
subplot(121), ezplot(C(1), C(2)); axis square;
312 ◾ Fundamentals of Graphics Using MATLAB®
% torus
x = 2 + cos(u); y = 2 + sin(u);
C = [x, y];
S = [x*cos(v), x*sin(v), y];
figure,
subplot(121), ezplot(C(1), C(2)); axis ([0 4 0 4]); axis square;
subplot(122), ezmesh(S(1), S(2), S(3)); axis equal;
colormap(jet);
% sinusoid
x = 2 + sin(u); y = u;
C = [x, y];
S = [x*cos(v), x*sin(v), y];
figure,
subplot(121), ezplot(C(1), C(2)); axis square;
subplot(122), ezmesh(S(1), S(2), S(3)); axis equal;
colormap(jet);
x = 2 + cos(u); y = u;
C = [x, y];
S = [x*cos(v), x*sin(v), y];
figure,
subplot(121), ezplot(C(1), C(2)); axis square;
subplot(122), ezmesh(S(1), S(2), S(3)); axis equal;
colormap(jet);
∂f ∂f ∂f
N ( x , y , z ) = , , (8.18)
∂ x ∂ y ∂z
∂f ∂f ∂f ∂ fp ∂ fp ∂ fp
N p = N (a, b, c ) = , , = , , (8.19)
∂ x ∂ y ∂z (a , b , c ) ∂ x ∂ y ∂z
Surfaces ◾ 313
If this plane needs to be the tangent plane then the plane should also pass through the point
p on the surface. The equation of the tangent plane through p can therefore be written
as (Rovenski, 2010):
∂ fp ∂ fp ∂ fp
Tp : ⋅ ( x − a) + ⋅ ( y − b) + ⋅ (z − c ) = 0 (8.20)
∂x ∂y ∂z
Example 8.7
Find the normal vector and tangent plane to the surface f = x 3 + 3xy 2 − z = 0 at
point p(1, 2, 13).
Here, f = x 3 + 3xy 2 − z
From Equation (8.18), normal N is given by
∂f ∂f ∂f
N ( x , y , z ) = , ,
∂ x ∂ y ∂z
( )
= 3x 2 + 3 y 2 , 6 xy , −1
NOTE
The z- coefficient for the vector N p is negative, which means that the vector is p ointing
toward the negative Z-direction. Sometimes, if we are asked to calculate the upward p
ointing
normal then we should simply choose the opposite facing vector toward the positive
Z-direction i.e. (−15, −12, 1)
%plotting
fimplicit3(f); hold on;
axis([-5 5 -5 5 -15 15]);
fimplicit3(T, ‘MeshDensity’, 2, ‘FaceColor’, ‘y’, ‘FaceAlpha’,0.6);
plot3([0 1], [0 2], [0 13], ‘ro’);
quiver3(p(1), p(2), p(3), Np(1), Np(2), Np(3), ‘LineWidth’, 1, ‘color’, ‘r’, ‘MarkerSize’, 10)
quiver3(p(1), p(2), p(3), -Np(1), -Np(2), -Np(3), ‘LineWidth’, 1, ‘color’, ‘b’, ‘MarkerSize’, 10)
xlabel(‘x’); ylabel(‘y’); zlabel(‘z’);
view(-170,64); hold off;
Surfaces ◾ 315
If the surface equation is in parametric form S = {x (u, v ), y(u, v ), z (u, v )}, to compute
the normal we first need to find out the component curves of the surface i.e. the curves
along u and v, which has resulted in the surface being generated. The component curves are
computed using partial derivatives of the surface (Rovenski, 2010).
∂S
ru (u, v ) =
∂u
(8.21)
∂S
rv (u, v ) =
∂v
Since the component curves lie on the surface, the normal is obtained as a cross product
of these.
To compute the normal vector at a given point p = (u0 , v0 ) the corresponding Cartesian
coordinates are obtained from S p = { x 0 , y0 , z 0 }. The component curves at the given point
are ru (u0 , v0 ) and rv (u0 , v0 ) and the normal vector at the given point is
Tp : a ( x − x 0 ) + b ( y − y0 ) + c ( z − z 0 ) = 0 (8.24)
Example 8.8
{
Find the normal vector and tangent plane to the surface S = u ⋅ sinv , u 2 , 2u ⋅ cos v }
at point (u = 1, v = π ) .
{
Given surface: S = u ⋅ sin v , u 2 , 2u ⋅ cos v }
At u = 1, v = π we get by substitution, point p on the surface S p = { x 0 , y0 , z 0 } = {0, 1, −2}
The component curves at p: ru (1, π ) = (0, 2, −2) and rv (1, π ) = (−1, 0, 0)
From Equation (8.22), normal vector at point p: N p = ru (1, π ) × rv (1, π ) = (0, 2, 2)
i.e. 2 j + 2k
From Equation (8.24), tangent plane at p: Tp : 2( y − 1) + 2(z + 2) = 0 i.e. y + z = −1
(Figure 8.18)
MATLAB Code 8.8
clear all; clc;
syms u v x y z;
S = [u*sin(v), u^2, 2*u*cos(v)];
ru = diff(S, u);
rv = diff(S, v);
N = cross(ru, rv);
up = 1; vp = pi;
p = subs(S, [u, v], [up, vp]);
rup = subs(ru, [u, v], [up, vp]);
rvp = subs(rv, [u, v], [up, vp]);
fprintf('Normal vector : \n');
Np = cross(rup, rvp)
fprintf('Tangent plane : \n');
Tp = Np(1)*(x - p(1)) + Np(2)*(y - p(2)) + Np(3)*(z - p(3))
316 ◾ Fundamentals of Graphics Using MATLAB®
%plotting
Np = 10*Np; %scaling for visualization
ezmesh(S(1), S(2), S(3)); hold on;
f = @(x,y,z) Np(1)*(x - p(1)) + Np(2)*(y - p(2)) + Np(3)*(z - p(3));
fimplicit3(f, ‘MeshDensity’, 2, ‘FaceColor’, ‘y’, ‘FaceAlpha’,0.6)
axis([-10 10 -10 40 -15 15]);
plot3(p(1), p(2), p(3), ‘ro’);
quiver3(p(1), p(2), p(3), Np(1), Np(2), Np(3), 1, 'color', 'r');
quiver3(p(1), p(2), p(3), -Np(1), -Np(2), -Np(3), 1, 'color', 'b');
xlabel('X'); ylabel('Y'); zlabel('Z');
view(50,34); hold off;
Surfaces ◾ 317
The above formula is derived by dividing the function into very thin slices each of arc
2
dy
length 1 + and rotating each along a circle of circumference 2πy (See Section 5.4).
dx
The area of the whole surface is then obtained by integrating the individual areas within
the specified limits.
The volume of the solid is derived by dividing the solid into very thin discs each of area
πy 2 since the radius of each disc is simply the value of the function y measured from the
X-axis. The total volume is obtained by integrating the areas of all discs over the specified
limits and is given by:
b
V=
∫ πy dx (8.26)
a
2
If the region is bounded by two curves y1 and y 2 then the volume of the bounded region is
b
∫ π( y )
2
given by: V = 1 − y 2 2 dx
a
Example 8.9
Find the area and volume of the surface generated when part of the curve
y = 9 − x 2 between x = −2 and x = 2 is rotated about the X-axis.
Given curve: y = 9 − x 2
dy
Differentiating: = −x 9 − x 2
dx
2
dy 3
Thus: 1 + =
dx 9− x2
2
3
From Equation (8.25), surface area: A =
∫
9− x2
−2
2π 9 − x 2 ⋅
= 24 π
2
92π
From Equation (8.26), volume: V =
−2 ∫ (
π 9 − x 2 dx =
3
)
(Figure 8.20)
3
3
2.9
2
2.8
1
2.7
0
z
2.6
y
-1
2.5
-2
2.4 -3
2.3 2
0 10
5
2.2 0
-2 -1 0 1 2 -2 -5
y -10
x
x
B = pi*y^2;
fprintf('Volume : \n');
V = int(B, x, -2, 2)
%plotting
C = [x, y];
figure,
subplot(121), ezplot(C(1), C(2), [-2 2]); axis square;
subplot(122), ezmesh(C(1), C(2)*cos(v), C(2)*sin(v), [-5 5 -10 10]);
axis square;
view(-30, 23);
If the given curve is represented as x = f ( y ) in the interval y ∈[c , d] that needs to be rotated
about the Y-axis to generate a surface of revolution, the area is derived by multiplying thin
2
dx
slices of arc length 1 + by the circumference of 2πx and then integrating over the
specified limits. dy
2
d dx
A =
∫ c
2πx ⋅ 1 + dy (8.27)
dy
The volume of the solid is derived by dividing the solid into very thin discs each of area
πx 2 since the radius of each disc is simply the value of the function x measured from the
Y-axis. The total volume is obtained by integrating the areas of all discs over the specified
limits and is given by:
d
V=
∫c
πx 2 dy (8.28)
If the region is bounded by two curves x1 and x 2 then the volume of the bounded region is
d
given by: V =
∫ π(x
c
1
2
)
− x 2 2 dy
320 ◾ Fundamentals of Graphics Using MATLAB®
Example 8.10
Find the area and volume of the surface generated when part of the curve y = 3 x
between y = 1 and y = 2 is rotated about the Y-axis.
Given curve: x = y 3
dx
Differentiating: = 3y2
dy
2
dx
Thus: 1 + = 1 + 9 y 4
dy
2
From Equation (8.26), surface area: A =
∫ 2πy ⋅
1
3
1+ 9y4
145
∫
From Equation (8.27), surface area: A = 2πy 3 ⋅ 1 + 9 y 4
1
du
dy =
∫
10
2π ⋅ u ⋅
36
= 199.48
2
127 π
From Equation (8.28), volume: V =
∫ πy dy =
1
6
7
= 56.99
8.9 TEXTURE MAPPING
Texture mapping is a process of applying an image, referred to as the texture, onto a
surface, usually for the purpose of making the surface more realistic in appearance
(Shirley, 2002) (see Figure 8.21).
The texture being an image is a rectangular 2D matrix of pixel values, usually
represented using (u, v ) coordinate values while the surface, generally in 3D space, can be
of any arbitrary shape and is represented using ( x , y , z ) coordinate values. To determine
which part of the image is applied to which part of the surface a mapping between the (u, v )
and the ( x , y , z ) values is necessary to maintain homogeneous integrity (O’Rourke., 2003).
Surfaces ◾ 321
(a)
15
10
-5
-10
2
1 2
0 1
0
-1
-1
v -2 -2 u
(b)
FIGURE 8.21 Surface (a) before and (b) after applying texture map.
We will study the mapping process and derive the mathematical transformations
required for a 2D surface. The concepts could readily be extended for a 3D surface by the
reader. There are broadly two types of mapping transformations: affine and perspective.
An affine transformation can be generated when the mapping of the texture on the surface
keeps parallel lines intact. Consider a texture described by normalized (u, v ) coordinates in
the range [0, 1] and the surface described by ( x , y ) coordinates (see Figure 8.22).
The corners of the rectangular texture image have (u, v ) coordinates of (0, 0), (1, 0),
(1, 1), (0, 1) and these points are mapped to the surface having ( x , y ) coordinates of
( x0 , y0 ) , ( x1 , y1 ) , ( x 2 , y2 ) , ( x3 , y3 ). It is required to derive a mapping transformation for
the operation. Let the shape of the surface has the following constraints: ( x1 − x 0 ) = ( x 2 − x 3 )
322 ◾ Fundamentals of Graphics Using MATLAB®
(0, 1) (1, 1)
15
(x2, y2)
(x3, y3)
10
v
y
(x1, y1)
5
(x0, y0)
0
(0, 0) u (1, 0) 0 5 10 15
x
and ( y1 − y0 ) = ( y 2 − y3 ). This implies that the surface has the shape of a parallelogram and
hence the mapping transformation will be affine in nature. An affine transformation is
given by the following:
x a b c u
y = d e f v (8.29)
1 0 0 1 1
x x1 − x 0 x3 − x0 x0 u
y = y1 − y0 y3 − y0 y0 v (8.31)
1 0 0 1 1
Surfaces ◾ 323
Example 8.11
Derive a transformation for mapping a texture with (u, v ) coordinates
(0, 0), (1, 0), (1, 1), (0, 1) to a surface with ( x , y ) coordinates of
(5, 5), (10, 7 ), (12, 12), (7 , 10). Also for the texture point (u = 0.6 , v = 0.7 ), find the
corresponding point on the surface.
Surface coordinates: ( x 0 , y0 ) = (5, 5), ( x1 , y1 ) = (10, 7), ( x 2 , y 2 ) = (12, 12),
( x3 , y3 ) = (7, 10)
Now ( x1 − x 0 ) = 5, ( x 2 − x 3 ) = 5, ( y1 − y0 ) = 2, ( y 2 − y3 ) = 2
Since ( x1 − x 0 ) = ( x 2 − x 3 ) and ( y1 − y0 ) = ( y 2 − y3 ) the mapping transformation is
affine in nature.
x1 − x 0 x3 − x0 x0
From Equation (8.31), transformation matrix M = y1 − y0 y3 − y0 y0
0 0 1
5 2 5
= 2 5 5
0 0 1
From Equation (8.30),
x = 5u + 2v + 5
y = 2u + 5v + 5
Verification:
x (0, 0) = 5, y(0, 0) = 5, x (0, 1) = 7, y(0, 1) = 10, x (1, 0) = 10, y(1, 0) = 7, x (1, 1) = 12, y(1, 1) = 12
(Figure 8.23):
1 12
0.8
10
0.6
8
0.4
6
0.2
0 4
0 0.5 1 4 6 8 10 12
x0 = 5; y0 = 5;
x1 = 10; y1 = 7;
x2 = 12; y2 = 12;
x3 = 7; y3 = 10;
d1 = x1 - x0; d2 = x2 - x3;
if d1 == d2
fprintf('Transformation is affine\n');
else
fprintf('Transformation is perspective\n');
end
U = [0 1 1 ; 0 0 1 ; 1 1 1];
X = [x0 x1 x2 ; y0 y1 y2 ; 1 1 1];
fprintf('Transformation matrix : \n');
M = X*inv(U)
subplot(121)
x = [0 1 1 0];
y = [0 0 1 1];
c = [0 4 6 8];
colormap(jet);
patch(x,y,c); hold on;
scatter(0.6, 0.7, 20, 'r', 'filled');
axis square; hold off;
subplot(122)
x = [5 10 12 7];
y = [5 7 12 10];
c = [0 4 6 8];
colormap(jet);
patch(x,y,c); hold on;
scatter(X1(1), X1(2), 20, 'r', 'filled');
axis square; grid; hold off;
x′ a b c u
y′ = d e f v (8.32)
w g h 1 1
x ′ au + bv + c
x= =
w gu + hv + 1
(8.33)
y ′ du + ev + f
y= =
w gu + hv + 1
Plugging in the given BC, we can solve for the unknown coefficients a, b, c , d , e , f , g , h as
follows:
The above eight equations can be solved to find out the values of the eight unknown
coefficients. The solution of the above equations is given by the following:
∆x1 = x1 − x 2
∆x 2 = x 3 − x 2
∆x 3 = x 0 − x1 + x 2 − x 3
∆y1 = y1 − y 2
∆y 2 = y3 − y 2
∆y3 = y0 − y1 + y 2 − y3
326 ◾ Fundamentals of Graphics Using MATLAB®
∆x 3 ∆x 2
det
∆y3 ∆y 2
g=
∆x1 ∆x 2
det
∆y1 ∆y 2
∆x1 ∆x 3
det
∆y1 ∆y3
h=
∆x1 ∆x 2
det
∆y1 ∆y 2
a = x 1 − x 0 + g ⋅ x1
b = x3 − x0 + h ⋅ x3
c = x0
d = y1 − y0 + g ⋅ y1
e = y3 − y0 + h ⋅ y3
f = y0
Example 8.12
Derive a transformation for mapping a texture with (u, v ) coordinates (0, 0),
(1, 0), (1, 1), (0, 1) to a surface with ( x , y ) coordinates of (5, 5), (10, 7 ), (10, 14),
(7 , 10). Specify mapping relations and hence for the texture point (u = 0.6 , v = 0.7 ) ,
find the corresponding point on the surface.
Surface coordinates: ( x 0 , y0 ) = (5, 5), ( x1 , y1 ) = (10, 7), ( x 2 , y 2 ) = (10, 14),
( x3 , y3 ) = (7, 10)
Now ( x1 − x 0 ) = 5, ( x 2 − x 3 ) = 3, ( y1 − y0 ) = 2, ( y 2 − y3 ) = 7
Since ( x1 − x 0 ) ≠ ( x 2 − x 3 ) and ( y1 − y0 ) ≠ ( y 2 − y3 ) the mapping transformation is
perspective in nature.
Here,
∆x1 = x1 − x 2 = 0
∆x 2 = x 3 − x 2 = −3
∆x 3 = x 0 − x1 + x 2 − x 3 = −2
∆y1 = y1 − y 2 = −7
Surfaces ◾ 327
∆y 2 = y3 − y 2 = −4
∆y3 = y0 − y1 + y 2 − y3 = 2
∆x 3 ∆x 2
det
∆y3 ∆y 2
g= = −0.6667
∆x1 ∆x 2
det
∆y1 ∆y 2
∆x1 ∆x 3
det
∆y1 ∆y3
h= = 0.6667
∆x1 ∆x 2
det
∆y1 ∆y 2
a = x1 − x 0 + g ⋅ x1 = −1.6667
b = x 3 − x 0 + h ⋅ x 3 = 6.6667
c = x 0 = 5
d = y1 − y0 + g ⋅ y1 = −2.6667
e = y3 − y0 + h ⋅ y3 = 11.6667
f = y 0 = 5
a b c
From Equation (8.32), transformation matrix M = d e f
g h 1
−1.6667 6.6667 5
= −2.6667 11.6667 5
−0.6667 0.6667 1
From Equation (8.33), mapping relations are:
au + bv + c (−1.6667)u + (6.6667)v + 5
x= =
gu + hv + 1 (−0.6667)u + (0.6667)v + 1
du + ev + f (−2.6667)u + (11.6667)v + 5
y= =
gu + hv + 1 (−0.6667)u + (0.6667)v + 1
For (u = 0.6, v = 0.7),
328 ◾ Fundamentals of Graphics Using MATLAB®
x (0.6, 0.7) = 8.6667
y(0.6, 0.7) = 11.5667
Verification:
x (0, 0) = 5, y(0, 0) = 5, x (0, 1) = 7, y(0, 1) = 10, x (1, 0) = 10, y(1, 0) = 7, x (1, 1) = 10, y(1, 1) = 14
x0 = 5; y0 = 5;
x1 = 10; y1 = 7;
x2 = 10; y2 = 14;
x3 = 7; y3 = 10;
d1 = x1 - x0; d2 = x2 - x3;
if d1 = = d2
fprintf('Transformation is affine\n');
else
fprintf('Transformation is perspective\n');
end
dx1 = x1 - x2;
dx2 = x3 - x2;
dx3 = x0 - x1 + x2 - x3;
dy1 = y1 - y2;
dy2 = y3 - y2;
dy3 = y0 - y1 + y2 - y3;
% Verification
fprintf('\n Verification : \n');
u=0; v=0; x = (a*u + b*v + c)/(g*u + h*v + 1);
fprintf('u = %d, v = %d, x = %d \n', u, v, x)
8.10 SURFACE ILLUMINATION
Surface illumination determines the brightness of a surface given the parameters regarding
the light sources such as the light intensity, angle of incidence light, angle of the observer
looking at the surface, and also the reflectance properties of the surface. An illumination
model takes these parameters as input and produces an output regarding the brightness of
the surface. Since the brightness depends on the angle of reflected light and the viewpoint
of the observer in some cases, we need to derive a mathematical model for this purpose
(Shirley, 2002).
In Figure 8.24, let PA indicate the direction of an incident light ray striking the surface
at P making an angle θ with the normal along PC and let PD indicate the direction of the
reflected ray such that PA = PD. Let AC be parallel to PD and CD be parallel to PA intersect-
ing at C. Project PA onto N at B and extend PB to C.
Writing in terms of vectors, let L and R represent the unit vectors for incidence and
reflected rays and N is the unit vector along the normal. The laws of reflection dic-
tate that angles of incidence and reflection have to be equal and also the incident ray,
reflected ray and surface normal should lie on the same plane. According to the triangle
rule for vectors, R = PC + CD. The projection of L onto N is given by (L·cos θ) N = PB.
Hence PC = 2PB = 2(|L|·cos θ) N. Also CD = −L. Combining these expressions, we get the
following, remembering that vectors L, N, and R are the unit vectors and have magnitude 1:
330 ◾ Fundamentals of Graphics Using MATLAB®
12
10
B
4 A D
N
2 L R
-2
-6 -4 -2 0 2 4 6
FIGURE 8.24 Relation between incident ray, reflected ray, and normal to a surface.
R = PC + CD = 2 PB + CD = 2 ( L ⋅ cosθ ) N − L = 2 ( L N ⋅ cosθ ) N − L = 2( L ⋅ N )N − L
(8.34)
Example 8.13
Light is incident along L = – i + 2 j – k on a surface with normal N = j . Calculate the
reflected ray and angle of incidence. Verify that angles of incidence and reflection
are equal.
Here, L = – i + 2 j – k and N = j
L
Lu = = (−0.4082 0.8165 − 0.4082)
L
N
Nu = = (0,1,0)
N
%plotting
quiver3(0, 0, 0, L(1), L(2), L(3)); hold on;
quiver3(0, 0, 0, R(1), R(2), R(3));
quiver3(0, 0, 0, N(1), N(2), N(3));
plot3(0, 0, 0, 'bo'); grid on;
view(17, 53);
xlabel('i'); ylabel('j'); zlabel('k');
text(L(1), L(2), L(3), 'L');
text(N(1), N(2), N(3), 'N');
text(R(1), R(2), R(3), 'R');
hold off;
Surfaces ◾ 331
332 ◾ Fundamentals of Graphics Using MATLAB®
R
N
0.4 L
0.2 1
0 0.8
k
-0.2 0.6
-0.4 0.4
-0.4
-0.2 0.2
0 j
0.2 0
i 0.4
Intensity at any point on the surface depends on three types of reflections: ambient
reflection, diffuse reflection, and specular reflection (Hearn and Baker, 1996). Ambient
reflection assumes that all objects receive the same amount of light from all directions
and simulates a constant background illumination. Intensity I a at a point P due to a mbient
light is given by the following, where La is the intensity of ambient light, and ka is the
ambient reflection coefficient of a surface i.e. what percentage of the incident ambient light
the surface reflects 0 ≤ ka ≤ 1 (Foley et al., 1995)
I a = La ⋅ ka (8.35)
Diffuse reflection assumes the surface is perfectly diffusing i.e. light is scattered equally in
all directions as it strikes a surface. In this case, the intensity of a point would depend on
the angle of reflection θ. If the angle is very small, then most of the incident light would
bounce back along the same path and the observed brightness would be high. However as
the angle increase, light would be scattered in other directions so that the percentage of
reflected light coming to the observer would be smaller. This would reduce the observed
intensity of the surface. Intensity I d at a point P due to diffused reflection is given by the
following, where Lp is the intensity of the light source P, θ is the angle of reflection, and kd
is the diffuse reflection coefficient of a surface i.e. what percentage of the incident diffused
light the surface reflects 0 ≤ kd ≤ 1, and remembering that L and N are the unit vectors
(Foley et al., 1995).
Surfaces ◾ 333
I d = Lp ⋅ kd ⋅ cosθ = Lp ⋅ kd ⋅ ( L • N ) (8.36)
Specular reflection occurs when light is reflected at a certain angle from a shiny surface.
A shiny surface behaves like an imperfect mirror and produces specular highlights, which
is actually the image of the light source itself reflected from the surface. Unlike diffuse
reflection, specular reflection is dependent on position of viewer. Let V be the unit vec-
tor along the viewing direction and φ be the angle between V and R (see Figure 8.26).
Observed intensity would be highest when the observer views the surface exactly along the
reflected ray i.e. φ = 0 but intensity would reduce as φ increases and the observer moves
further away from the reflected light, intensity I s at a point P due to specular reflection is
given by the following, where Lp is the intensity of the light source P, φ is the angle between
R and V, m is a positive number dependent on the material of the surface, and ks is the
specular reflection coefficient of a surface, 0 ≤ ks ≤ 1, and remembering that R and V are
the unit vectors (Foley et al., 1995).
I s = Lp ⋅ ks ⋅ (cosϕ )m = Lp ⋅ ks ⋅ ( R • V )m (8.37)
Hence, total intensity of reflected light at a point on a surface is the combined effect of all
the above factors:
12
10
4 R
N
2 L V
0
P
-2
-6 -4 -2 0 2 4 6
FIGURE 8.26 Relation between incident ray, reflected ray, normal, and viewing direction.
334 ◾ Fundamentals of Graphics Using MATLAB®
Example 8.14
Light is incident along L = – i + 2 j – k at a point on a surface with normal
N = j . Calculate the intensity at the point if the source light is 10 times more
intense than the ambient light. Assume viewing direction as V = i + 1.5 j + 0.5 k and
ka = 0.15, kd = 0.4 , ks = 0.8 , m = 5.
Incident ray: L = – i + 2 j – k
Normal: N = j
Reflected ray: R = i + 2 j + k (see previous example)
Viewing direction: V = i + 1.5 j + 0.5 k
(– i + 2 j – k )
Lu = = (−0.4082 0.8165 − 0.4082)
6
j
Nu = = (0 1 0)
1
(i + 2 j + k )
Ru = = (0.4082 0.8165 0.4082)
6
i + 1.5 j + 0.5 k
Vu = = (0.5345 0.8018 0.2673)
1.8708
Lu • N u = 0.8165
Ru • Vu = 0.9820
I a = La ⋅ ka = 1 × 0.15 = 0.15
I = I a + I d + I s = 6.7567
Thus the intensity on the surface is approximately 67.5% of the source intensity.
La = 1;
Lp = 10;
Surfaces ◾ 335
ka = 0.15;
kd = 0.25;
ks = 0.5;
m = 5;
(a) ezplot3 & fplot3: Used to plot functions using parametric variables:
ezplot3('cos(t)','t*sin(t)','sqrt(t)')
2.5
1.5
z
0.5
0
2
0 1
-2 0.5
0
-4
-0.5
y -6 -1 x
0.8
0.6
0.4
0.2
0
40
20 40
0 20
0
-20
-20
-40 -40
(c) ezmesh & ezsurf: Generates a mesh plot and surface plot using symbolic variables
ezmesh('x.*y.*exp(-x.^2 - y.^2)')
ezsurf('x.*y.*exp(-x.^2 - y.^2)')
(d) ezcontour: Generates a contour plot and filled contour plot using symbolic
variables
ezcontour('x.*y.*exp(-x.^2 - y.^2)')
ezcontourf('x.*y.*exp(-x.^2 - y.^2)')
ezsurfc('x.*y.*exp(-x.^2 - y.^2)')
Views on graphs can be changed by specifying the view function, which takes in two
arguments: the first for the horizontal rotation angle (azimuth) and the second for the verti-
cal rotation angle (elevation). The colormap function can be used to change the color scheme.
0.15
0.1
0.05
-0.05
-0.1
-0.15
3
2
3
1
2
0 1
-1 0
-1
-2
y -2
-3 -3 x
0.15
0.1
0.05
-0.05
-0.1
-0.15
3
2
3
1
2
0 1
-1 0
-1
-2
y -2
-3 -3 x
y
-1
-2
-3
-3 -2 -1 0 1 2 3
x
0
y
-1
-2
-3
-3 -2 -1 0 1 2 3
x
(f) mesh & surf: Generates a surface using a matrix of values. In the first step, a grid
of points using function “meshgrid” is created on the X–Y plane. For each point a Z
value is defined using a specific function. The “mesh” or “surf” function is used to
create a surface by plotting the value of Z for each point on the X–Y grid and joining
the values of Z by colored lines.
(a)
0.1
-0.1
-0.2
2
2
0
0
-2 -2
y x
(b)
0.1
-0.1
2
0
-0.2
-3 -2 -2
-1 0 1 2 y
3
x
FIGURE 8.31 (a) Plotting with ezsurfc; (b) using view to change orientation.
(g) patch: Creates filled polygons given the vertices and colors
x = [4 6 11 9];
y = [2 7 9 4];
z = [3 5 10 6];
c = [0 4 6 8];
colormap(jet);
patch(x,y,z, c);
colorbar;
hold; grid;
v1 = [2 4 5; 2 12 6; 8 4 1];
patch('Vertices', v1, 'FaceColor', 'red', 'FaceAlpha', 0.3, 'EdgeColor', 'red');
axis([0 12 0 12 0 12]);
view(-164,-56);
xlabel('x'); ylabel('y'); zlabel('z');
340 ◾ Fundamentals of Graphics Using MATLAB®
Surfaces ◾ 341
0.2
0.1
-0.1
-0.2
2
2
0 1
0
-1
-2 -2
0.2
0.1
-0.1
-0.2
2
2
0 1
0
-1
-2 -2
-5
2 5
1.5
1 0
0.5
0 -5
(i) fimplicit3: This function has been introduced from MATLAB version 2016 and
takes as an argument the implicit function
h = ezsurf('sin(sqrt(x^2+y^2))/sqrt(x^2+y^2)',[-4*pi,4*pi]);
view(0,75);
figure
h = ezsurf('sin(sqrt(x^2+y^2))/sqrt(x^2+y^2)',[-4*pi,4*pi]);
view(0,75);
lightangle(-45,30);
h.AmbientStrength = 0.3;
h.DiffuseStrength = 0.8;
h.SpecularStrength = 0.9;
h.SpecularExponent = 25;
(k) warp: Used to map a texture image over a surface with a known equation
I = imread('peppers.png');
[X,Y] = meshgrid(-10:10,-10:10);
Z = -sqrt(X.^2 + Y.^2 + 10);
surf(X,Y,Z)
figure; warp(X,Y,Z,I);
344 ◾ Fundamentals of Graphics Using MATLAB®
(a)
10
y
-5
1
0.5
-10
0
-10 -5 0 5 10
x
(b)
FIGURE 8.36 Plotting a surface (a) without and (b) with lightangle.
Surfaces ◾ 345
(a)
-4
-6
-8
-10
-12
-14
10
5 10
0 5
0
-5
-5
-10 -10
(b)
FIGURE 8.37 Plotting a surface (a) without and (b) with texture mapping using warp.
(l) set: Used to set object properties, which can change over time and thereby generate
animations
perspective transform
0
z
-1
-2
2
1 2
0 1
0
-1
-1
y -2 -2 x
x^2+y^2-z^2 = 10
10
0
z
-5
-10
10
5 10
0 5
0
-5
-5
y -10 -10 x
(m) affine2d and projective2d: Used to create affine and perspective (or
projective) transformations of images
A = checkerboard(10);
M = [5 2 5 ; 2 5 5 ; 0 0 1];
tform = affine2d(M');
B = imwarp(A, tform);
figure, imshow(B); title('affine transform');
M = [5 2 5 ; 2 5 5 ; 0.01 0.01 1];
tform = projective2d(M');
C = imwarp(A, tform);
figure, imshow(C); title('perspective transform');
(o) ezimplot3: This function is mentioned on the MathWorks File Exchange web site
(https://in.mathworks.com/matlabcentral/fileexchange/300-implot-m). Generates a
surface from an implicit equation of the form f ( x , y , z ) = 0.
f = 'x^2+y^2+z^2-5' ; ezimplot3(f);
NOTE
imshow: displays an image in a figure window
imwarp: applies geometric transformation to an image for mapping it to a surface
8.12 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
• The surface illumination is determined based on light sources and surface reflection
properties.
• Surface reflection can be either ambient or diffuse or specular.
8.13 REVIEW QUESTIONS
1. What is the difference between parametric surfaces and implicit surfaces?
2. How is a Bezier surface generated from two Bezier splines?
3. Specify how commonly used quadric surfaces are represented using implicit
equations?
4. How are surfaces categorized based on creation methods?
5. What is the difference between extruded surface and surfaces of revolution?
6. How is the normal vector and tangent plane of a surface calculated?
7. How is the area and volume of a surface of revolution computed?
8. How is an affine texture mapping transformation computed?
9. How is a perspective texture mapping transformation computed?
10. What is an illumination model? How can it used to calculate brightness at a
surface point?
8.14 PRACTICE PROBLEMS
( )
1. A parametric surface s 2 , 2s , t is translated using T(1, 0, −1) and then scaled using
S(−1, 0, 1). Find the parametric representation of the resulting surface.
2. Find the parametric representation of the elliptic paraboloid x = 5 y 2 + 2z 2 –10 that is
in front of the YZ-plane.
1
3. A bi-quadratic Bezier surface has the following control points: P00 (0, 0, 0), P01 0, , 0 ,
2
1 1 1 1 1 1 1 1 1
P02 0, 1, , P10 , 0, 0 , P11 , , 0 , P12 , 1, , P20 1, 0, , P21 1, , ,
b 2 2 2 2 b a 2 a
1 1
P22 1, 1, + Find the surface equation.
a b
4. Find the equation of the tangent plane of the cone x 2 + y 2 = z 2 at point (0.6, 0.8, 1).
( )
5. Find the normal to the surface S(u, v ) = 2u, v , u 2 + v 2 at point (4, 3, 13).
6. Find the volume of the solid obtained by rotating about the x-axis the region bounded
by the curve y = x 2 − 2 x + 3, x = 0, x = 3.
Surfaces ◾ 351
7. Find the volume of the solid obtained by rotating about the y-axis the portion of
the region bounded by the curves y = x 1/3 and y = x /4 , in the first quadrant between
x = 0, x = 2.
( )
8. A light source positioned at P(0, 10, 20) is shining on the surface S(u, v ) = u, v , − u 2 − v 2
for 0 ≤ u, v ≤ 1. Determine the incident ray, the reflected ray at point Q = (½, ½, −½)
on the surface and angle of reflection.
9. Light along direction L = 2i + j + 3k falls on a surface with normal N = i − 2 j + k .
Calculate the reflected ray and incidence angle. Also verify that angle of incidence is
equal to angle of reflection.
10. Light is striking the plane P: –2 x – 8 y + 10z –10 = 0 along the direction L = k , and
the observer viewpoint is along V = (i + j + k ). Assuming the ambient intensity to be
1/6th the source intensity and m = 2, ka = 0.2, kd = 0.3, ks = 0.4, determine intensity of
reflected light at a point on the plane as a percentage of the source intensity.
Chapter 9
Projection
9.1 INTRODUCTION
Projection is used to map a higher-dimensional object to a lower-dimensional view i.e.
from 2D to 1D or from 3D to 2D. The lower-dimensional entity is called a viewline or view-
plane, respectively. In this chapter, we will mostly discuss about projection of 3D objects
on to a 2D viewplane but we will introduce the concepts using a 2D projection on a 1D
viewline and then extend the concepts for the 3D case.
Projection can be of two types: parallel and perspective. In parallel projection, projec-
tion lines are parallel to each other. This type of projection produces unrealistic views in
the sense that it is not what a viewer in the physical world would see as the apparent size
of an object would not depend on its distance from the viewer, and all things near and far
would appear at their true sizes. However, parallel projection is useful as it keeps intact the
true sizes and angles of objects. In perspective projection, the projection lines appear to
converge to a point called projection reference point (PRP). This is actually how we see in
the real-world as parallel lines appear to converge at a distance to our eyes. This is known
as the perspective effect and happens because the size of objects depend on their distances
from the observer—as objects move farther their apparent sizes reduce. Although perspec-
tive projection produces realistic views of scenes; however, it distorts the true lengths and
angles of lines and surfaces. For 2D projection, points are projected on a line called the
viewline, while for 3D projection points are projected on a plane called the viewplane.
Parallel projection can again be of two types: orthographic and oblique. In parallel ortho-
graphic projection, the projection lines are perpendicular to the view plane. In parallel
oblique projection, the projection lines can be oriented at any arbitrary angles to the view
plane (Hearn and Baker, 1996) (see Figure 9.1).
Usually for 3D projection, parallel orthographic projection can also be sub-divided into
two types: multi-view and axonometric. In multi-view projection, the projection occurs
on the primary planes i.e. XY-, YZ-, or XZ-planes. Such views are called top, side, and
front views, and display only one face of the object. These are used in engineering and
architectural drawings as length and angles can be accurately measured. In axonometric
projection, the projection occurs on an arbitrary plane that does not coincide with any of
353
354 ◾ Fundamentals of Graphics Using MATLAB®
the primary planes. In this case, more than one face of the object can be viewed. The ratios
of the actual lengths of the object along the three axes to their projected lengths are called
foreshortening factors.
K = V × P (9.1)
Q = L × K = L × (V × P ) (9.2)
( )
Now using the vector identity A × ( B × C ) = (C • A)B –( A • B)C = BT ⋅ A – BAT ⋅ I ⋅ C we
get:
( )
Q = L × (V × P ) = V T ⋅ L – VLT ⋅ I ⋅ P = M ⋅ P (9.3)
Projection ◾ 355
Here, I is the identity matrix and M is the perspective projection matrix of point P onto
viewline L along direction V (Marsh, 2005).
As shown above the points and lines are represented using homogeneous vector nota-
tions of the form L = (a, b, c ) and V = ( v1 , v 2 , 1). For parallel projection V can be thought
to be at infinity so that projection lines are parallel (Figure 9.2b). To represent a point at
infinity in the direction of V we use the notation V = ( v1 , v 2 , 0 ). The vector from V towards
the viewline L along the direction of projection is called the projection vector.
356 ◾ Fundamentals of Graphics Using MATLAB®
Example 9.1
Consider a viewline L( x , y ): − 3x + 12 y − 5 = 0 and a point P(−8, −6). Determine the
projection matrix and projected coordinates in each case: (a) Perspective projection
of P with viewpoint (−3, 11) (b) Parallel oblique projection of P in the direction (3, −2)
(c) Parallel orthographic projection of P at right angles to the viewline
(a)
V = [−3, 11, 1], L = [−3, 12, −5], P = [−8, 6, 1]
From Equation (9.3)
−3
( )
Projection matrix: M = V ⋅ L – VL ⋅ I = 11
T T
−3 12 −5
1
−3 1 0 0
− −3 11 1 12 0 1 0
−5 0 0 1
−127 −36 15
Simplifying, M = −33 −4 −55 (in homogeneous coordinates)
−3 12 −141
−8
Original coordinates of point P = 6
1
815
Projected coordinates: Q = M ⋅ P = 185 (in homogeneous coordinates)
−45
−18.11
Projected coordinates: Q ′ = −4.11 (in Cartesian coordinates)
1
Verification: The projected point must lie on the view line: L(−18.11, −4.11) = 0
(Figure 9.3a).
(b)
V = [3, −2, 0], L = [−3, 12, −5], P = [−8, 6, 1]
From Equation (9.3)
Projection ◾ 357
(a) (b)
12 6 P
V
10 5
8 4
3
6 P
2
4
1 L
y
y
2 Q
L 0
0
-1
-2 -2 V
-4 Q -3
-6 -4
-20 -15 -10 -5 0 5 10 -10 -5 0 5 10
x x
(c)
7
6 P
5
4
3
2
y
1 L V
0
-1 Q
-2
-3
-10 -5 0 5 10
x
−3
T T
Projection matrix: M = V ⋅ L – VL ⋅ I = 2 ( )
−3 12 −5
0
−3 1 0 0
− −3 2
0 12 0 1 0
−5 0 0 1
24 36 −15
Simplifying, M = 6 9 10 (in homogeneous coordinates)
0 0 33
−8
Original coordinates of point P = 6
1
358 ◾ Fundamentals of Graphics Using MATLAB®
9
Projected coordinates: Q = M ⋅ P = 16 (in homogeneous coordinates)
33
0.2727
Projected coordinates: Q ′ = 0.4848 (in Cartesian coordinates)
1
Verification: The projected point must lie on the view line: L(0.2727, 0.4848) = 0
(Figure 9.3b).
(c)
a −3
Slope of line ax + by + c = 0 is given by – = − = 0.25
b 12
Tangent vector: t = [1, 0.25, 1]
Normal vector: n = R(90)* t = [−0.25, 1, 1]
Parallel orthographic projection should be along the direction of the normal vector
Hence, V = [−0.25, 1, 0], L = [−3, 12, −5], P = [−8, 6, 1]
From Equation (9.3)
−0.25
Projection matrix: T
( T
)
M = V ⋅ L – VL ⋅ I = 1
−3 12 −5
0
−3 1 0 0
− −0.25 1 0 12 0 1 0
−5 0 0 1
−12 −3 1.25
Simplifying, M = −3 −0.75 −5 (in homogeneous coordinates)
0 0 −12.75
79.25
Projected coordinates: Q = M ⋅ P = 14.5 (in homogeneous coordinates)
−12.75
−6.21
Projected coordinates: Q ′ = −1.14 (in Cartesian coordinates)
1
Verification: The projected point must lie on the view line: L(−6.21, −1.14) = 0
(Figure 9.3c).
Projection ◾ 359
m = -L(1)/L(2); % slope
t = [1, m, 1]; % tangent
R90 = [cosd(90) -sind(90) 0 ; sind(90) cosd(90) 0 ; 0 0 1];
n = R90*t'; % normal
V = [n(1), n(2), 0];
M = V'*L - V*L'*eye(3);
Q = M*P';
Qc = Q/Q(3)
%plotting
figure
syms x y;
y1 = (-L(1)*x - L(3))/L(2);
xx = -10:10;
yy = subs(y1, x, xx);
plot(xx, yy, 'b-', 'LineWidth', 1.5);
hold on; grid;
quiver(P(1), P(2), V(1), V(2));
quiver(V(1), V(2), V(1), V(2));
scatter(P(1), P(2), 20, 'r', 'filled');
scatter(V(1), V(2), 20, 'r', 'filled');
scatter(Qc(1), Qc(2), 20, 'r', 'filled');
plot([P(1), Qc(1)], [P(2), Qc(2)], 'k--');
text(Qc(1)+0.5, Qc(2), 'Q');
text(V(1)+0.5, V(2), 'V');
text(P(1)+0.5, P(2), 'P');
text(-2, 1, 'L', 'FontSize', 15);
xlabel('x'); ylabel('y'); axis square;
Projection ◾ 361
%verification
vrf3 = subs(f, [x, y], [Qc(1), Qc(2)]);
hold off;
NOTE
eye: generates an identity matrix of specified size
( )
Q = L × (V × P ) = V T ⋅ N – VN T ⋅ I ⋅ P = M ⋅ P (9.4)
Here, I is the identity matrix and M is the perspective transformation matrix (Marsh, 2005).
Example 9.2
Consider a viewplane F( x , y , z ): − x + 3 y + 2z − 4 = 0 and a point P(−4, 2, 2).
Determine the projection matrix and projected coordinates in each case: (a)
Perspective projection of P with viewpoint (2, −1, 1) (b) Parallel oblique projection of
P in the direction (1, 2, 1) (c) Parallel orthographic projection of P at right angles to
the viewplane
(a)
Point P = [−4, 2, 2, 1]
Normal N = [−1, 3, 2, −4]
Viewpoint V = [2, −1, 1, 1]
From Equation (9.4)
2
−1
Projection matrix (
M = V T ⋅ N – VN T ) ⋅ I =
1
−1
3 2 −4
1
−1 1 0 0 0
3 0 1 0 0
− 2 −1 1 1
2 0 0 1 0
−4 0 0 0 1
362 ◾ Fundamentals of Graphics Using MATLAB®
5 6 4 −8
1 4 −2 4
Simplifying, M =
−
1 3 9 −4
−1 3 2 3
−8
4
Projected coordinates: Qh = M ⋅ P = (homogeneous coordinates)
24
17
−0.47
0.23
Projected coordinates: Q = Qh 17 = (Cartesian coordinates)
1.41
1
Verification: F (−0.47, 0.23, 1.41) = 0 (Figure 9.4a)
(b)
Point P = [−4, 2, 2, 1]
Normal N = [−1, 3, 2, −4]
Viewpoint V = [1, 2, 1, 1]
From Equation (9.4)
1
2
Projection matrix (
M = V T ⋅ N – VN T ) ⋅ I =
1
−1
3 2 −4
1
−1 1 0 0 0
3 0 1 0 0
− 1 2 1 1
2 0 0 1 0
−4 0 0 0 1
−8 3 2 −4
−2 −1 4 −8
Simplifying, M =
−1 3 −5 −4
0 0 0 −7
38
6
Projected coordinates: Qh = M ⋅ P = (homogeneous coordinates)
−4
−7
Projection ◾ 363
−5.43
−0.85
Projected coordinates: Q = Qh (−7) = (Cartesian coordinates)
0.57
1
Verification: F (−5.43, −0.85, 0.57) = 0 (Figure 9.4b)
(c)
Point P = [−4, 2, 2, 1]
Normal N = [−1, 3, 2, −4]
Viewpoint V = N = [−1, 3, 2, −4]
From Equation (9.4)
−1
3
(
Projection matrix M = V T ⋅ N – VN T ) ⋅ I =
2
−1
3 2 −4
−4
−1 1 0 0 0
3 0 1 0 0
− −1 3 2 −4
2 0 0 1 0
−4 0 0 0 1
FIGURE 9.4 Plots for Example 9.2 (a) perspective projection (b) oblique projection (c) orthographic
projection.
364 ◾ Fundamentals of Graphics Using MATLAB®
−13 −3 −2 4
−3 −5 6 −12
Simplifying, M =
−2 6 −10 −8
0 0 0 −14
46
2
Projected coordinates: Qh = M ⋅ P = (homogeneous coordinates)
− 8
−14
−3.28
−0.14
Projected coordinates: Q = Qh (−14) = (Cartesian coordinates)
0.57
1
Verification: F (−3.28, −0.14, 0.57) = 0 (Figure 9.4c)
V = [1, 2, 1, 0];
M = V'*N - V*N'*eye(4);
Qh = M*P';
Q = Qh/Qh(4)
figure
plot3(P(1), P(2), P(3), 'ro');
hold on; grid; view(-66, 30);
quiver3(0, 0, 2, N(1), N(2), N(3));
quiver3(V(1), V(2), V(3), V(1), V(2), V(3));
plot3(Q(1), Q(2), Q(3), 'ro');
plot3(V(1), V(2), V(3), 'ro');
plot3([Q(1) P(1)], [Q(2) P(2)], [Q(3) P(3)], 'k--');
xlabel('x'); ylabel('y'); zlabel('z');
text(P(1), P(2), P(3)+0.5, 'P');
text(Q(1), Q(2), Q(3)+0.5, 'Q');
text(V(1), V(2), V(3)+0.5, 'V');
text(N(1), N(2), N(3)+2, 'N');
%verification
vrf2 = subs(f, [x, y, z], [Q(1), Q(2), Q(3)])
fimplicit3(f, 'MeshDensity', 2, 'FaceColor', 'y', 'FaceAlpha',0.3);
9.4 MULTI-VIEW PROJECTION
In parallel orthographic multi-view projection, the projection lines are parallel to each
other and perpendicular to the primary planes. Such views are called top, side, and
front views, and display only one face of the object (Foley et al., 1995). These are used in
engineering and architectural drawings as length and angles can be accurately measured.
If view plane coincides with primary planes then projection matrices are as shown below,
where the subscripts indicate projection on the XY-, XZ-, and YZ-planes.
1 0 0 0
0 1 0 0
Pxy = (9.5)
0 0 0 0
0 0 0 1
1 0 0 0
0 0 0 0
Pxz = (9.6)
0 0 1 0
0 0 0 1
0 0 0 0
0 1 0 0
Pyz = (9.7)
0 0 1 0
0 0 0 1
If the projection planes are parallel to the primary planes say at z = k then the following
steps are performed to derive the transformation matrix:
Example 9.3
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1). Derive a parallel projection of the
cube onto the z = 3 plane in a direction parallel to the Z-axis
Here, normal to viewplane N = [0, 0, 1, −3]
The viewpoint is located at infinity along the Z-axis, hence V = N
From Equation (9.4)
Projection ◾ 367
0
0
Projection matrix (
M = V T ⋅ N – VN T ) ⋅ I =
1
0 0 1 −3
−3
0 1 0 0 0
0 0 1 0 0
− 0 0 1 −3
1 0 0 1 0
−3 0 0 0 1
−1 0 0 0
0 −1 0 0
Simplifying, M =
0 0 0 −3
0 0 0 −1
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
New coordinate matrix:
1 −1 −1 1 1 −1 −1 1
−1 −1 1 1 −1 −1 1 1
Dh = M ⋅ C = (homogeneous coordinates)
−3 −3 −3 −3 −3 −3 −3 −3
−1 −1 −1 −1 −1 −1 −1 −1
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
D = Dh (−1) = (Cartesian coordinates)
3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1
New coordinates: (−1, 1, 3), (1, 1, 3), (1, −1, 3), (−1, −1, 3), (−1, 1, 3), (1, 1, 3), (1, −1, 3),
and (−1, −1, 3) (Figure 9.5)
p5 = [-1,1,-1];
p6 = [1,1,-1];
p7 = [1,-1,-1];
p8 = [-1,-1,-1];
C = [p1' p2' p3' p4' p5' p6' p7' p8' ;
1 1 1 1 1 1 1 1 ];
figure
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
Dh = M*C; D = Dh/Dh(4);
plot3(C(1,:), C(2,:), C(3,:), 'b'); hold on;
plot3(D(1,:), D(2,:), D(3,:), 'r');
xlabel('x'); ylabel('y'); zlabel('z');
legend('original', 'new'); axis equal;
grid; hold off;
original
new
3
2.5
1.5
1
z
0.5
-0.5
-1
1
1
0
0
y -1 -1
x
9.5 AXONOMETRIC PROJECTION
In parallel orthographic axonometric projection, the projection lines are parallel to each
other and perpendicular to the viewplanes but the viewplanes do not coincide with the
primary planes (Chakraborty, 2010). Unlike multi-view projection where only a single face
of the object is visible, in this case multiple views of the object can be viewed. To derive the
projection matrix the following steps are followed:
• Align the normal to the viewplane to coincide with one of the primary axes, say
the Z-axis
• Perform projection on the corresponding primary plane i.e. XY-plane
• Reverse align the viewplane to original location
Example 9.4
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1). Derive an axonometric projection of
the cube onto the plane having normal vector along i + j + k and passing through the
point (0, 0, 5).
The viewplane has normal i + j + k and passes through the point (0, 0, 5). Hence,
equation of the viewplane is ( x − 0) + ( y − 0) + (z − 5) = 0 . Thus N = [1, 1, 1, −5].
Also since the projection is parallel and orthographic the projection lines
are along the normal to the plane and the viewpoint is located at infinity. Thus
V = [1, 1, 1, 0].
From Equation (9.4)
1
1
Projection matrix: (
M = V T ⋅ N – VN T ) ⋅ I =
1
1
1 1 −5
0
1 1 0 0 0
1 0 1 0 0
− 1 1 1 0
1 0 0 1 0
−5 0 0 0 1
−2 1 1 −5
1 −2 1 −5
Simplifying, M =
1 1 −2 −5
0 0 0 −3
370 ◾ Fundamentals of Graphics Using MATLAB®
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
New coordinates: (0.33, 2.33, 2.33), (1.67, 1.67, 1.67), (2.33, 0.33, 2.33), (1, 1, 3),
(1, 3, 1), (2.33, 2.33, 0.33), (3, 1, 1), and (1.67, 1.67, 1.67) (Figure 9.6)
figure
syms x y z;
f = x + y + z - 5;
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
Dh = M*C; D = Dh/Dh(4);
plot3(C(1,:), C(2,:), C(3,:), 'b'); hold on; grid;
plot3(D(1,:), D(2,:), D(3,:), 'r');
plot3(0, 0, 5, 'ro');
quiver3(0, 0, 5, 2, 2, 2);
fimplicit3(f, 'MeshDensity', 2, 'FaceColor', 'y', 'FaceAlpha',0.3);
xlabel('x'); ylabel('y'); zlabel('z');
legend('original', 'new'); axis equal;
view(-70, 70); hold off;
9.6 FORESHORTENING FACTORS
Foreshortening factors are ratios of the projected lengths to the original lengths of compo-
nents of a vector along the three principle axes. Let a line PQ on a plane be projected on the
XY-plane at pq. Then the ratio of length (pq) to the ratio of length (PQ) is referred to as the
foreshortening scaling factor and defines how much the original line has been scaled due
to projection (Marsh, 2005). It turns out that this ratio is same for all lines on the plane and
does not depend on the actual coordinates of the line (Figure 9.7).
372 ◾ Fundamentals of Graphics Using MATLAB®
Expanding M,
Now consider a vector P = Ai + Bj + Ck. Its component vectors along the three principle
axes are given by Px = ( A, 0, 0), Py = (0, B, 0), Pz = (0, 0, C ). The lengths of the vectors are:
The projected components of P onto the primary axes are calculated as follows:
Qx = M ⋅ Px = ( A ⋅ cosϕ , 0, 0)
L (Qx ) = A ⋅ cosϕ
ff x = L (Qx ) L ( Px ) = cosϕ
Expanding M,
0 0 0 0
0 cos α − sin α 0
M= (9.13)
− sinϕ sin α ⋅ cosϕ cos α ⋅ cosϕ 0
0 0 0 1
374 ◾ Fundamentals of Graphics Using MATLAB®
The projected components of P onto the primary axes are calculated as follows:
Qx = M ⋅ Px = (0, 0, A ⋅ sinϕ )
L (Qx ) = A ⋅ sinϕ
ff x = L (Qx ) L ( Px ) = sinϕ
For projection along Z- and X-axes, angle α remains the same, but angle φ is complemen-
tary to angle φ in first case. Hence, substituting φ with 90-φ Equation (9.16) becomes
identical to Equation (9.12).
Example 9.5
Find the foreshortening factors for the plane whose normal vector is N = 3i + 4 j + 12k
Normal vector N = 3i + 4 j + 12k
Here, a = 3, b = 4, c = 12
Also d = b 2 + c 2 = 12.65, e = a 2 + b 2 + c 2 = 13
Thus cosα = c /d = 12/12.65 = 0.95, sin α = b/d = 4/12.65 = 0.32
cosϕ = a/e = 3/13 = 0.23, sinϕ = d /e = 12.65/13 = 0.97
From Equation (9.16):
Projection ◾ 375
ff x = sinϕ = 0.97
a = 3; b = 4; c = 12;
d = sqrt(b^2 + c^2);
e = sqrt(a^2 + b^2 + c^2);
cosA = c/d; sinA = b/d;
cosB = a/e; sinB = d/e;
R1 = [1, 0, 0, 0 ; 0, cosA, -sinA, 0 ; 0, sinA, cosA, 0 ; 0, 0, 0, 1];
R2 = [cosB, 0, sinB, 0 ; 0, 1, 0, 0 ; -sinB, 0, cosB, 0 ; 0, 0, 0, 1];
P_YZ = [0, 0, 0, 0 ; 0, 1, 0, 0 ; 0, 0, 1, 0 ; 0, 0, 0, 1];
M = P_YZ * R2 * R1;
ffx = (sinB)
ffy = (cosA^2 + cosB^2*sinA^2)^(1/2)
ffz = (cosA^2*cosB^2 + sinA^2)^(1/2)
Example 9.6
Show that axonometric projections of a unit cube at the origin, on the planes
x + y + z = 5, x + y + 2z = 5 , and x + 3 y + 2z = 5 are, respectively, isometric, dimetric,
and trimetric.
(a)
0 1 1 0 0 1 1 0
0 0 1 1 0 0 1 1
Original coordinate matrix: C =
0 0 0 0 1 1 1 1
1 1 1 1 1 1 1 1
P: x + y + z = 5
a = 1, b = 1, c = 1
N = [1, 1, 1, −5]
V = [1, 1, 1, 0]
From Equation (9.4)
Projection matrix:
1
1
(
M = V T ⋅ N – VN T ) ⋅ I =
1
1
1 1 −5
0
1 1 0 0 0
1 0 1 0 0
− 1 1 1 0
1 0 0 1 0
−5 0 0 0 1
−2 1 1 −5
1 −2 1 −5
Simplifying, M =
1 1 −2 −5
0 0 0 −1
New coordinate matrix:
−5 −7 −6 −4 −4 −6 −5 −3
−5 −4 −6 −7 −4 −3 −5 −6
Dh = M ⋅ C = (homogeneous coordinates)
−5 −4 −3 −4 −7 −6 −5 −6
−3 −3 −3 −3 −3 −3 −3 −3
1.67 2.33 2 1.33 1.33 2 1.67 1
1.67 1.33 2 2.33 1.33 1 1.67 2
New coordinate matrix: D =
1.67 1.33 1 1.33 2.33 2 1.67 2
(Cartesian coordinates)
1 1 1 1 1 1 1 1
Projection ◾ 377
d = b 2 + c 2 = 1.41, e = a 2 + b 2 + c 2 = 1.73
c b
cos α = = 0.71, sin α = = 0.71
d d
d a
cosϕ = = 0.82, sinϕ = = 0.58
e e
ff x = cosϕ = 0.82
Since all three foreshortening factors are equal, the projection is isometric (Figure 9.8a).
FIGURE 9.8 Plots for Example 9.6 (a) isometric projection (b) dimetric projection (c) trimetric
projection.
378 ◾ Fundamentals of Graphics Using MATLAB®
(b)
P : x + y + 2 z = 5
a = 1, b = 1, c = 2
N = [1, 1, 2, −5]
V = [1, 1, 2, 0]
−5 −10 −9 −4 −3 −8 −7 −2
−5 −4 −9 −10 −3 −2 −7 −8
Dh = M ⋅ C = (homogeneous coordinates)
−10 −8 −6 −8 −12 −10 −8 −10
−6 −6 −6 −6 −6 −6 −6 −6
d = b 2 + c 2 = 2.24, e = a 2 + b 2 + c 2 = 2.45
c b
cos α = = 0.89, sin α = = 0.45
d d
Projection ◾ 379
d a
cosϕ = = 0.91, sinϕ = = 0.41
e e
ff x = cosϕ = 0.91
Since two out of three foreshortening factors are equal, the projection is dimetric.
(Figure 9.8b).
(c)
P : x + 3 y + 2 z = 5
a = 1, b = 3, c = 2
N = [1, 3, 2, −5]
V = [1, 3, 2, 0]
1
3
( )
M = V T ⋅ N – VN T ⋅ I =
2
1 3 2 −5 −
0
1 1 0 0 0
1 3 2 0 3 0 1 0 0
2 0 0 1 0
−5 0 0 0 1
−13 3 2 −5
3 −5 6 −15
Simplifying, M =
2 6 −10 −10
0 0 0 −14
New coordinate matrix: Dh = M ⋅ C
−5 −18 −15 −2 −3 −16 −13 0
−15 −12 −17 −20 −9 −6 −11 −14
= (homogeneous
−10 −8 −2 −4 −20 −18 −12 −14
−14 −14 −14 −14 −14 −14 −14 −14
coordinates)
380 ◾ Fundamentals of Graphics Using MATLAB®
d = b 2 + c 2 = 3.60, e = a 2 + b 2 + c 2 = 3.74
c b
cos α = = 0.55, sin α = = 0.83
d d
d a
cosϕ = = 0.96, sinϕ = = 0.27
e e
ff x = cosϕ = 0.96
Since, none of the three foreshortening factors are equal to each other, the projection
is trimetric (Figure 9.8c).
% isometric projection
fprintf('Isometric projection : \n');
D = Dh/Dh(4);
d = sqrt(b^2 + c^2);
e = sqrt(a^2 + b^2 + c^2);
cosA = c/d; sinA = b/d;
cosB = d/e; sinB = a/e;
ffx = (cosB)
ffy = (cosA^2 + sinA^2*sinB^2)^(1/2)
ffz = (sinA^2 + cosA^2*sinB^2)^(1/2)
figure
syms x y z;
f = a*x + b*y + c*z - 5;
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
Dh = M*C; D = Dh/Dh(4);
plot3(C(1,:), C(2,:), C(3,:), 'b'); hold on; grid;
plot3(D(1,:), D(2,:), D(3,:), 'r');
fimplicit3(f, 'MeshDensity', 2, 'FaceColor', 'y', 'FaceAlpha',0.3);
xlabel('x'); ylabel('y'); zlabel('z');
axis equal; title('isometric projection');
view(-70, 80);
% dimetric projection
fprintf('Dimetric projection : \n');
%trimetric projection
fprintf('Trimetric projection : \n');
9.8 OBLIQUE PROJECTION
When the direction of parallel projection on the viewplane is oblique i.e. not perpendicular,
then it is called an oblique projection. The foreshortening factor of line segments parallel to
the viewplane is 1. When the viewing direction makes an angle of 45° with the viewplane
then the projection obtained is called cavalier projection. If V = [v1 , v 2 , v3 ] be the viewpoint
Projection ◾ 383
vector, then a cavalier projection satisfies the condition: v3 2 = v1 2 + v 2 2 . Here, the image of the
projected object looks much thicker than in reality as the foreshortening factor for faces per-
pendicular to the viewing plane is 1. To reduce the thickness, a projection type called cabinet
projection can be used, which reduces this factor to 0.5. This is possible when the viewpoint
vector makes an angle of 63.4° with the viewplane. If V = [v1 , v 2 , v3 ] be the viewpoint vector,
(
then a cabinet projection satisfies the condition: v3 2 = 4 v1 2 + v 2 2 . (Marsh, 2005) )
Example 9.7
Show that the oblique viewing directions (3, 4, 5) and (3, 4, 10) produce a cavalier and
cabinet projection of a unit cube on the z = 0 plane, respectively. Verify by computing
the foreshortening factor of a line perpendicular to the viewplane in each case.
(a)
The projections are on the z = 0 plane, hence N = [0, 0, 1, 0]
For a parallel projection, viewpoint vector V = [3, 4, 5, 0]
Since, it satisfies the identity v3 2 = v1 2 + v 2 2 the projection produced would be a
cavalier projection.
From Equation (9.4)
Projection matrix:
3
4
(
M = V T ⋅ N – VN T ) ⋅ I =
5
0
0 1 0
0
0 1 0 0 0
0 0 1 0 0
− 3 4 5 0
1 0 0 1 0
0 0 0 0 1
−5 0 3 0
0 −5 4 0
Simplifying, M =
0 0 0 0
0 0 0 −5
0 1 1 0 0 1 1 0
0 0 1 1 0 0 1 1
C=
0 0 0 0 1 1 1 1
1 1 1 1 1 1 1 1
384 ◾ Fundamentals of Graphics Using MATLAB®
0 −5 −5 0 3 −2 −2 3
0 0 −5 −5 4 −4 −1 −1
Dh = M ⋅ C =
0 0 0 0 0 0 0 0
−5 −5 −5 −5 −5 −5 −5 −5
(homogeneous coordinates)
3
4
(
Projection matrix: M = V T ⋅ N – VN T ) ⋅ I =
10
0 0 1 0
0
0 1 0 0 0
0 0 1 0 0
− 3 4 10 0
1 0 0 1 0
0 0 0 0 1
−10 0 3 0
0 −10 4 0
Simplifying, M =
0 0 0 0
0 0 0 −10
Projection ◾ 385
(a)
0.5 1
z
0.5
0
1
0
0.5
0
-0.5 x
-0.5
y
(b)
0.5
z
0 0.5
1
0.5 0
0 x
y
FIGURE 9.9 Plots for Example 8.9 (a) cavalier projection (b) cabinet projection.
0 1 1 0 0 1 1 0
0 0 1 1 0 0 1 1
C=
0 0 0 0 1 1 1 1
1 1 1 1 1 1 1 1
386 ◾ Fundamentals of Graphics Using MATLAB®
(homogeneous coordinates)
0 1 1 0 −0.3 0.7 0.7 −0.3
0 0 1 1 −0.4 −0.4 0.6 0.6
New coordinate matrix: D =
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
(Cartesian coordinates)
Consider a line segment joining the origin to the point (0, 0, 1), which is perpen-
dicular to the viewplane and has length 1. The projected coordinates of the point is
obtained by multiplying M with the point and equals (−0.3, −0.4, 0), which indicates
that after projection the line segment still has length 0.5. Hence, the foreshortening
factor is 0.5, which is as expected for a cabinet projection (Figure 9.9b).
% cavalier projection
fprintf('Cavalier projection : \n')
N = [0, 0, 1, 0];
V = [3, 4, 5, 0];
M = V'*N - V*N'*eye(4);
Dh = M*C;
D = Dh/Dh(4);
figure
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
Dh = M*C; D = Dh/Dh(4);
plot3(C(1,:), C(2,:), C(3,:), 'b'); hold on; grid;
Projection ◾ 387
%cabinet projection
fprintf('Cabinet projection : \n')
N = [0, 0, 1, 0];
V = [3, 4, 10, 0];
M = V'*N - V*N'*eye(4);
C = [p1' p2' p3' p4' p5' p6' p7' p8' ;
1 1 1 1 1 1 1 1 ];
Dh = M*C;
D = Dh/Dh(4);
figure
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
Dh = M*C; D = Dh/Dh(4);
plot3(C(1,:), C(2,:), C(3,:), 'b'); hold on; grid;
plot3(D(1,:), D(2,:), D(3,:), 'r');
xlabel('x'); ylabel('y'); zlabel('z')
axis equal;
view(-66, 40);
title('cabinet projection');
% verification
V1 = [0 ; 0 ; 1 ; 1];
V1p = M*V1; V1pc = V1p/(V1p(4));
V1pcl = sqrt(V1pc(1)^2+V1pc(2)^2+V1pc(3)^2);
ff = V1pcl
hold off;
9.9 PERSPECTIVE PROJECTION
Under perspective projection images of parallel lines in space appear to converge to a point
called the PRP or center of projection (COP). Let the point be A( x , y , z ) whose projection
P( x p , y p , 0) is required on a view plane coinciding with the XY-plane. Let R(0, 0, r ) be the
PRP along Z-axis and let O be the origin. It is required to obtain the values of xp and yp in
terms of x, y, z, and r.
Two cases are possible as follows (1) case-1: where A and R are on opposite side of the
view plane and (2) case-2: where A and R are on the same side (Foley et al., 1995).
388 ◾ Fundamentals of Graphics Using MATLAB®
In each case, x p = { }r
r−z
z
⋅ x = x 1− , yp =
r
r
r−z { }
z
⋅ y = y 1− , zp = 0
r
This can be represented in terms of homogeneous coordinates P = M xy ⋅ A as follows:
xp x
x
1 0 0 0
y
yp 0 1 0 0
y
= 0 = (9.17)
0 0 0 0 z
zp z
1 1 − 0 0 −1/r 1
1
r
Example 9.8
A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1) is projected on the plane z = 3 using
perspective projection with the reference point at z = 5. Find its new vertices.
Here, r = 5
Translate view plane to origin: T (0, 0, −3)
Apply perspective projection on XY-plane:
1 0 0 0 1 0 0 0
0 1 0 0 0 1 0 0
M xy = =
0 0 0 0 0 0 0 0
0 0 −1/r 1 0 0 −1/5 1
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Original coordinate matrix: C =
1 1 1 1 −1 −1 −1 −1
1 1 1 1 1 1 1 1
−1 1 1 −1 −1 1 1 −1
1 1 −1 −1 1 1 −1 −1
Dh = T (0, 0, 3) ⋅ M xy ⋅T (0, 0, −3) ⋅ C =
4.2 4.2 4.2 4.2 5.4 5.4 5.4 5.4
1.4 1.4 1.4 1.4 1.8 1.8 1.8 1.8
390 ◾ Fundamentals of Graphics Using MATLAB®
z
1
-1
1
1
0
0
-1 -1
y x
p1 = [-1,1,1];
p2 = [1,1,1];
p3 = [1,-1,1];
p4 = [-1,-1,1];
p5 = [-1,1,-1];
p6 = [1,1,-1];
p7 = [1,-1,-1];
p8 = [-1,-1,-1];
C = [p1' p2' p3' p4' p5' p6' p7' p8' ;
1 1 1 1 1 1 1 1 ];
Projection ◾ 391
tx = 0; ty = 0; tz = -k;
T=[1 0 0 tx ; 0 1 0 ty ; 0 0 1 tz ; 0 0 0 1];
M = [1 0 0 0 ; 0 1 0 0 ; 0 0 0 0 ; 0 0 -1/r 1];
Tr = inv(T);
Dh = Tr*M*T*C;
[rows, cols] = size(Dh);
for i=1:cols
D(:,i) = Dh(:,i)/Dh(4,i);
end
%Plotting
C = [p1' p2' p3' p4' p1' p5' p6' p7' p8' p5' p8' p4' p3' p7' p6' p2' ;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
Dh = Tr*M*T*C;
[rows, cols] = size(Dh);
for i=1:cols
D(:,i) = Dh(:,i)/Dh(4,i);
end
NOTE
size: returns the number of rows and columns of an array
9.10 CHAPTER SUMMARY
The following points summarize the topics discussed in this chapter:
9.11 REVIEW QUESTIONS
1. What is 2D and 3D projection?
2. What is the difference between parallel and perspective projection?
3. What is the difference between parallel orthographic and parallel oblique projection?
4. What is the difference between multi-view and axonometric projection?
5. What is meant by foreshortening factor? On what parameters does it depend?
6. What are the differences between isometric, dimetric, and trimetric projection views?
7. What are the differences between cavalier and cabinet projection views?
8. Explain the difference between viewline, viewpoint, viewplane, and PRP.
9. Discuss the two variations of perspective projection.
10. Explain how foreshortening factors can be used to differentiate between isometric,
dimetric, trimetric, cavalier, and cabinet projections.
9.12 PRACTICE PROBLEMS
1. Consider a vector A = 3i + 4 j + 12k. Obtain the ratios by which its projected lengths
have been reduced with respect to its original length, after orthographic projection
on the three principal planes.
2. A point P(2, 1, 5) is projected using axonometric projection onto the plane
2 x + 3 y + 4 z = 24 . Find its projected coordinates.
3. Determine the perspective projection of the triangle (3, 4), (5, 5), and (4, −1) onto the
line 5 x + y − 6 = 0 from the viewpoint (11, 2).
4. A cube with center at origin and vertices at (−1, 1, 1), (1, 1, 1), (1, −1, 1), (−1, −1, 1),
(−1, 1, −1), (1, 1, −1), (1, −1, −1), and (−1, −1, −1) is projected on a plane passing through
Projection ◾ 393
the origin perpendicular to the line joining points (2, 1, −2) and (3, 3, 2). Find its new
vertices.
5. A vector P = 3i + 4 j + 12k is projected on XY-plane using perspective projection. Find
projection coordinates if COP is at distance 2 from origin behind the view plane on
the Z-axis. Also find foreshortening factors.
6. Given the following points: A(3, 3, 4), B(6, 10, 10), C(7, 9, 12), D(4, 15, 15), and
E(5, 6, 8), find out for perspective projection, which points lie on the same projection
line joining A with COP at (1, 0, 0).
7. Derive the projection matrix in each case (1) perspective projection onto the
viewplane 5 x − 3 y + 2z – 4 = 0 from the viewpoint (2, −1, 1) and (2) parallel oblique
projection onto the viewplane 2 y + 3z + 4 = 0 in the direction of the vector (1, −2, 3).
8. The point (3, −2, 4) is projected onto the plane 4 x + 7 y − z = 10 such that the projection
lines are along the direction of the vector 5i + j − 6k. Find the projected coordinates.
9. Consider projection of a cube on the XY-plane using (1) α = 45°, ϕ = −35.264°,
(2) α = 45°, ϕ = −135°, and (3) α = 20°, ϕ = −70°. Compute projection matrix and
foreshortening factors in each case.
10. Obtain a parallel projection transformation matrix onto the X–Y plane if the
direction of the projection lines are along the vector ai + bj + ck.
Appendix I: MATLAB®
Function Summary
1.
%: signifies a comment line
2.
...: continues the current command or function call onto the next line
3. acos, acosd: calculates inverse cosine in radians and degrees
4. affine2d: generates a 2D affine transformation of an image
5. alpha: sets transparency values
6. asin, asind: calculates inverse sine in radians and degrees
7. axis: controls appearance of axes, specifies ordered range of values to display
8. clc: clears workspace of previous text
9. clear: clears memory of all stored variables
10. colorbar: creates a color bar by appending colors in the colormap
11. colormap: specifies a color scheme using predefined color look-up tables
12. cos, cosd: calculates cosine of an angle in radians and degrees
13. cross: calculates cross product of vectors
14. deg2rad: converts degree to radian values
15. det: calculates determinant of a matrix
16. diff: calculates derivatives and partial derivatives
17. disp: displays the symbolic expressions without additional line gaps
18. dot: calculates dot product of vectors
19. eval: evaluates an expression
20. eye: generates an identity matrix of specified size
21. ezcontour, ezcontourf: generates a contour plot with optional filling
395
396 ◾ Appendix I: MATLAB® Function Summary
Problem Answers
1.1 y = −3x − 12
1.2 −0.405 x 2 + 1.27 x
1.3 x = 11.67t 2 − 9.67t , y = 1.67t 2 − 5.67t + 2
1.4 x = 1.5t 2 + 2.5t − 2, y = −7.5t 2 + 5.5t + 1
1.5 y = −0.157 x 3 + 1.57 x 2 − 3.24 x + 1.83
1.6 x = 61.38t 3 − 125.3t 2 + 61.92t + 3, y = −142.3t 3 + 221t 2 − 80.68t + 2
1.7 y A = 0.0513x 3 + 0.377 x 2 + 0.92 x − 0.405,
y B = −0.0414 x 3 + 0.0986 x 2 + 0.642 x − 0.498,
y C = 0.381x 3 − 6.23x 2 + 32.3x − 53.2
1.8 y = k − 0.5 x − 1.5kx 2
1.9 k = 0.5
1.10 x A = t + 1, y A = 4.07t 3 − 10.1t 2 + 5t − 2,
x B = t + 2, y B = −0.2t 3 + 2.13t 2 − 2.93t − 3,
x C = t + 3, yC = −3.27t 3 + 1.53t 2 + 0.733t − 4
2.1 a = −3b/4
2.2 (i) x = −5.3t 3 + 8t 2 + 0.3t + 1, y = −3.9t 3 + 5.5t 2 + 0.4t + 1
(ii) x = 0.3t 3 + 2.7t + 1, y = −3.1t 3 + 1.5t 2 + 3.6t + 1
2.3 1
0 a −3
−a 2b −2 0
B=
b −c 3 −a
−2c a 0 −c
2.4 (−23.62, −112.92), (3, −4), (1,0), (73.34,120.35)
x = 70t 3 − 90t 2 + 17t + 3, y = 166t 3 − 233t 2 + 79t − 4
2.5 x = −2t 2 − 4t + 2, y = 6t 2 − 4t + 2
2.6 x = t 3 − 3t 2 − 3t + 3, y = 10t 3 − 30t 2 + 21t − 4
2.9 (1,4), (2, 6.5), (6,15)
2.10 (4, −45), (3, −4), (−2, −3), (−9, −22)
(Continued )
399
400 ◾ Appendix II: Answers to Practice Problems
Problem Answers
3.1 t (0 ≤ t < 1)
−2t + 3 (1 ≤ t < 2)
x (t ) =
2t − 5 (2 ≤ t < 3)
−t + 4 (3 ≤ t < 4)
0 (0 ≤ t < 1)
t −1 (1 ≤ t < 2)
y(t ) =
−2t + 5 (2 ≤ t < 3)
t−4 (3 ≤ t < 4)
3.2 t (0 ≤ t < 1)
B0,2 =
2−t (1 ≤ t < 2)
3.3 1 2
t (0 ≤ t < 1)
2
3
B0,3 = −t 2 + 3t − (1 ≤ t < 2)
2
1 2
(t − 3) (2 ≤ t < 3)
2
1 2
(t − 1) (1 ≤ t < 2)
2
3
B1,3 = −(t − 1)2 + 3(t − 1) − (2 ≤ t < 3)
2
1 2
(t − 4) (3 ≤ t < 4)
2
1 2
(t − 2) (2 ≤ t < 3)
2
3
B2,3 = −(t − 2)2 + 3(t − 2) − (3 ≤ t < 4)
2
1 2
(t − 5) (4 ≤ t < 5)
2
(Continued )
Appendix II: Answers to Practice Problems ◾ 401
Problem Answers
1 2
(t − 3) (3 ≤ t < 4)
2
3
B3,3 = −(t − 3)2 + 3(t − 3) − (4 ≤ t < 5)
2
1 2
(t − 6) (5 ≤ t < 6)
2
1 2
(t − 4) (4 ≤ t < 5)
2
3
B4,3 = −(t − 4)2 + 3(t − 4) − (5 ≤ t < 6)
2
1 2
(t − 7) (6 ≤ t < 7)
2
3.4 10t 2
−13.3t 2 + 9.33t − 0.933
3.5 P0 ⋅ (5 − 10t ) + P1 ⋅ (10t − 4)
3.6 P0 ⋅ t 4
P0 ⋅ (5 − t ) + P1 ⋅ (t − 4)
3.7
t2 (0 ≤ t < 1) 2.5t 2 (0 ≤ t < 1)
2t − 1 (1 ≤ t < 2) −5.5t 2 + 16t − 8 (1 ≤ t < 2)
2
−0.5t + 4t − 3 (2 ≤ t < 3) 2
7.5t − 36t + 44 (2 ≤ t < 3)
x (t ) = 2 y(t ) =
0.5t − 2t + 6 (3 ≤ t < 4) 2
−11t + 75t − 122.5 (3 ≤ t < 4)
−4.5t 2 + 38t − 74 (4 ≤ t < 5) 9t − 85t + 197.5
2
(4 ≤ t < 5)
3.5(t − 6)2 (5 ≤ t < 6) −2.5(t − 6)2 (5 ≤ t < 6)
3.8
0.1t 2 (0 ≤ t < 1) 0.25t 2 (0 ≤ t < 1)
0.1t 2 (1 ≤ t < 2) −2.5t 2 + 22t − 44 (1 ≤ t < 2)
2 2
−0.083t + 1.83t − 4.58 (2 ≤ t < 3) 1.25t − 15.5t + 49.8 (2 ≤ t < 3)
x (t ) = 2
y(t ) = 2
0.15t − 1.9t + 10.4 (3 ≤ t < 4) −4.85t + 82.1t − 341 (3 ≤ t < 4)
−0.392t 2 + 7.85t − 33.5 (4 ≤ t < 5) 0.858t 2 − 20.6t + 122 (4 ≤ t < 5)
0.583(t − 15)2 (5 ≤ t < 6) −0.417(t − 15)2 (5 ≤ t < 6)
3.9 0.333t 3 (0 ≤ t < 1)
x (t ) = 3 2
−0.333t + 2t − 2t + 0.667 (1 ≤ t < 2)
0 (0 ≤ t < 1)
y(t ) =
0.166(t − 1)3 (1 ≤ t < 2)
3.10 0.1t 3 (0 ≤ t < 1)
−0.2t + 0.9t 2 − 0.9t + 0.3
3
(1 ≤ t < 2)
B0,4 = 3 2
0.05t − 0.6t + 2.1t − 1.7 (2 ≤ t < 3)
−0.05(t − 6)3 (3 ≤ t < 4)
(Continued )
402 ◾ Appendix II: Answers to Practice Problems
Problem Answers
4.1 0 1 0
M1 = R(45) ⋅ Fx ⋅ R(−45) = 1 0 0
0 0 1
0 1 0
M 2 = R(90) ⋅ Fx = 1 0 0
0 0 1
4.2 0.5 1 0
0 0.5 2
0 0 1
4.5 0 0 5
0 0 4
0 0 1
4.7 x c
y=− −
m m
4.8 a b 0
ab a + b2 0
0 0 1
4.9 0.2 0.1 −0.3
−0.2 0.4 −0.2
0 0 1
4.10 (a) (7.00, −4.00), (−2.00, 2.00), (−10.00, −3.00), (−1.00, −9.00)
(b) (7.00, −4.00), (−0.50, 0.50), (−1.67, −0.50), (−0.33, −3.00)
5.1 12.407
(Continued )
Appendix II: Answers to Practice Problems ◾ 403
Problem Answers
5.2 0.88
5.3 1
3
5.4 18
5.5 (1.15, −2.05), (−1.15,2.05), (0,0)
5.6 i + 2 j , − 2i + j , y = 2 x − 1
5.7 3i − j i + 3 j
,
10 10
5.8 x + 2 y − 3 = 0, i + 2 j
5.9 i + j, − i + j
5.10 0.416, (0.48,0.43)
6.1 u is the unit vector along the direction of p
6.2 (4 − 4t )i + (3t ) j
6.3 s = −3, t = −2
6.4 x −2 −y +3 z +4
= =
3 1 2
6.5 x −y +3
= ,z =0
4 3
6.6 L is perpendicular to P
6.7 (0,5,2)
6.8 L is parallel to P
6.9 r = (0, −3,8) + t ⋅ (20,21, −68)
7.3 0.65
0 0.76 −0.41k
0 1 0 0
−0.76 0 0.65 1.11k
0 0 0 1
7.4 (4.28,0.67,2.38)
7.5 1/3 −2/3 −2/3 0
−2/3 1/3 −2/3 0
−2/3 −2/3 1/3 0
0 0 0 1
(Continued )
404 ◾ Appendix II: Answers to Practice Problems
Problem Answers
7.6 1/9 4/9 −8/9 8/9
4/9 7/9 4/9 −4/9
−8/9 4/9 1/9 8/9
0 0 0 1
7.7 (2, 7, 1), (2, 7, 0), (3, 10, 0), (3, 10, 1), (0, 0, 1), (0, 0, 0), (1, 3, 0), (1, 3, 1)
7.8 0.7454
−0.2981 −0.5963 0
0 0.8944 −0.4472 0
0.6667 0.3333 0.6667 0
0 0 0 1
8.1 (−s 2
− 1, 0, t − 1 )
8.2 5u 2 + 2v 2 ≥ 10
8.3 u2 v 2
u, v , a + b
8.5 −4i − 12 j + 2k
8.6 153π
5
8.7 512π
21
8.8 ( −0.0221 0.4204 0.9071) , (0.892, 0.45, − 0.0369), 41.094°
8.10 33.3%
(Continued )
Appendix II: Answers to Practice Problems ◾ 405
Problem Answers
9.1 0.3846, 0.9515, 0.9730
9.2 (1.7931,0.6897,4.5862)
9.3 (0.2632,4.6842), (−0.3333,7.6667), (1.6053, −2.0263)
9.4 (−1.24,0.52,0.05), (0.67,0.33, −0.33), (0.86, −1.29,0.43), (−1.05, −1.10,0.81), (−0.86,1.29, −0.43),
9.10 −c
0 a 0
0 −c b 0
0 0 0 0
0 0 0 −c
References
407
Index
C H
Cabinet projection, 383 Hermite spline, 53
Cardinal spline, 57 Homogeneous coordinates, 135, 225, 229, 354
Cartesian equation, 215, 218 Hybrid spline, 41, 52
Catmull-rom spline, 61
Cavalier projection, 382
I
Centroid, 189
Constraint matrix, 5, 36 Identity matrix, 355, 361
Continuity condition, 26, 32 Implicit surface, 298
Control points, 1, 88, 287 Interpolating spline, 1, 46
Coordinate system, 133–135, 167, 241–242 Interpolation, 192
Cox de boor algorithm, 77, 79, 90, 109–110 Isometric projection, 375
Critical points, 172
Cross product, 214
K
Cubic spline, 18, 22
Curve fitting, 192 Knot vector, 77, 102
Curve segments, 25, 31
L
D
Length of curve, 181
Diffused reflection, 332 Linear spline, 4, 10
Dimetric projection, 375 Local control, 77
409
410 ◾ Index
M S
MATLAB, xi Scaling, 138, 143, 245, 251
MATLAB function summary, 395 Shape parameter, 57, 61
Multi-view projection, 366 Shear, 152, 155, 280
Spatial domain, 4, 13, 18
Specular reflection, 333
N
Spline conversion, 69
Non-uniform b-spline, 77, 108 Spline properties, 171
Normal, 176, 234, 312 Sub-division ratio, 15, 22
Surface, 288
Surface area, 317
O
Surface illumination, 329
Oblique projection, 353, 356, 361, 382 Surface of revolution, 308, 317
Open uniform b-spline, 105
Orthographic projection, 353, 356, 361
T
Tangent, 176, 234, 312
P
Texture mapping, 320
Parallel projection, 353, 355, 366 3D plotting functions, 335
Parametric equations, 3, 10, 15, 22, 31 3D projection, 353
Parametric surface, 288, 293 Transformation matrix, 134–135
Perspective projection, 353, 355, 361, 387 Translation, 136, 241
Perspective transformation, 159, 324, 361 Trimetric projection, 375
Piecewise spline, 25, 31 2D plotting functions, 199
Point of inflection, 172–173 2D projection, 354
Polynomial, 1, 3–4
Position vector, 207–208, 215, 218
U
Primary axis, 134, 222, 241
Primary plane, 241, 353 Uniform b-spline, 78, 89, 105
Projection, 353 Unit vector, 208
Projection reference point, 353
Projection vector, 355
V
Vector alignment, 222, 264
Q
Vector equation of line, 215
Quadratic spline, 13, 15 Vector equation of plane, 218
Vectors, 207
Viewing direction, 334, 382
R
Viewing transformation, 163
Reflection, 147, 149, 277 Viewplane, 361
Right-handed coordinate system, 133, 241
Rotation, 140, 145, 248, 254, 256, 270, 273