Drawing Parametric Curves and Splines
Drawing Parametric Curves and Splines
Parametric curves are defined as vector functions of some scalar parameter t running from 0 to 1.
Mathematically, a point (x, y) on a curve is computed using two functions and of the parameter
t.
[ ] [ ]
The shape of the curve depends on the form of the functions and . For example if the two
nd
functions are linear in t, the curve is a line; if they are of 2 order degree, the curve is a quadratic
curve and so on.
Parametric lines:
[ ] [ ]
To get the coefficients , we may constrain the line to pass through two end points:
when t=0 and when t=1. Substituting in the above equation yields:
[ ] [ ] [ ]
[ ] [ ] [ ]
From which we have four equations in four unknowns. Note that we can solve for using only
the two constrains involving x which are:
Getting:
So:
Note also that we don’t have to work on y; after all, the formula of y is the same as that of x except
that every x is replaced with y. This always happens when the two functions and have the same
form (of course with different coefficients in general). The equations can also be written as follows:
Here we notice that each of the end points is multiplied by a function of t that weighs its
contribution to the point at t. This function is called ‘basis function’ or ‘blending function’. Basis
functions for the line case are:
Hermite curves use four constraints to compute the 4 coefficients of each function. We’ll derive the
coefficients of x(t) only because those of y(t) will follow a similar procedure. The constraints on x(t)
are:
In matrix form:
[ ][ ] [ ]
[ ] [ ] [ ] [ ][ ]
[ ][ ]
So:
[ ][ ][ ]
[ ]
There’s many ways to compute x(t). The efficient way is to use the matrix form and start by right-
multiplying the basis matrix by the input vector before the loop.
In Visual C++, we may build matrix and vector classes or structures to use them in the
implementation (we’ll need them also when we talk about transformation in next lectures).
struct Vector2
{
double x,y;
Vector2(double a=0,double b=0)
{
x=a; y=b;
}
};
class Vector4
{
double v[4];
public:
Vector4(double a=0,double b=0,double c=0,double d=0)
{
v[0]=a; v[1]=b; v[2]=c; v[3]=d;
}
Bezier Curves
Bezier curves are special cases of Hermit curves in which T0 and T1 are computed from four points P0,
P1, P2, P3 as follows:
The curve starts at P0 when t=0 and ends at P3 when t=1. This has a geometrical interpretation if we
consider the individual components of the four points for example the x components i.e. x0, x1, x2, x3.
Bezier assumes that x=x0 when t=0, x=x1 when t=1/3, x=x2 when t=2/3 and x=x3 when t=1. So we
have four points in the T-X plane: (0, x0), (1/3, x1), (2/3, x2) and (1, x3). The line connecting the point
(0, x0) to (1/3, x1) is tangent to the curve at t=0 so equals the slope of this line; i.e.:
Likewise, the line connecting (2/3, x2) to (1, x3) is tangent to the curve at t=1 so equals the
slope of the line at t=1; i.e.
The curve will not pass through P1 and P2. It lies in the ‘convex hull’ of the polygon P0-P1-P2-P3. The
following is an implementation of Bezier curve that calls the Hermite curve algorithm:
Cardinal splines
If we are given a set of points P0, P1, P2, …, Pn, we can draw a curve passing through P1, P2,…,Pn-1 by
calling the Hermite curve drawing algorithm for every interval Pi-Pi+1 , i=1,2,..,n-2. The slope at Pi is
given by:
c is called the ‘tension’ of the curve that takes values from 0 to 1. The following function shows the
implementation of this algorithm. Note that the algorithm does not draw the first and last interval
because it cannot compute the tangents at these points.