1 Chapter 6. Bezier Curves In this chapter, we will discuss a spline based upon a specific set of polynomials known as the Bernstein polynomials. The Bernstein polynomials are an approximation to a given function over an interval [ a, b ] . The values of the function are required over a set of evenly distributed points in [ a, b ] rather than an arbitrarily distributed set used in the previous sections. This seems, at first, to be a restriction that is not desirable. But as we shall learn, this type of spline is very useful in creating or constructing smooth curves that conform to a user-specified shape. These curves are called Bezier curves. So here we deal mainly with methods and procedures for generating shapes rather than function approximations to sets of data points. 6.1. Bernstein Polynomials First we introduce the Bernstein polynomials which are the foundation for building Bezier curves. The Bernstein polynomials of degree n associated with function y (x) on [ a, b ] is defined as B n ( y; x ) 1 (b a) n n n i ( x a) (b x) i 0 i n i y ( xi ) (6.1.1) n where are the binomial coefficients and i ba xi a i , i 0, 1, 2, , n (6.1.2) n is an evenly distributed set of points in [a, b] generated by Equation 6.1.2 and not given data values. Indeed, Bn ( y; x) is not an interpolating polynomial. In previous chapters, the problems we dealt with are: given a set of data points, try to produce a function to connect them. When the value of the function at a location in between the data points is needed, the value of the interpolation or spline function at that point can be used as an approximation. The data points can be arbitrary as long as they are discrete and arranged in some order. The Bernstein polynomials exhibit distinctly different behavior. They are polynomials of degree n which vanish at the end points a and b . Over the entire interval [ a, b ] , Bn ( y; x) is a single function rather than a combination of piecewise functions. The coefficients of the polynomials are the same for the entire interval. In contrast, in the case of the polynomial splines and B-splines, the coefficients are generally different from sub-interval to sub-interval. Although Bn ( y; x) require values of y (x) over only a set of discrete points xi rather than over the entire interval, the values y ( xi ) are part of the coefficients. The 2 polynomials do not pass through externally supplied points ( xi , y ( xi )) . They are not splines by definition. In this chapter, the generated points xi are only dependent on the degree n once [ a, b ] is set because positions xi vary according to Equation 6.1.2. The Bernstein polynomials require different xi for different degree n , then y ( xi ) is determined from a function y (x) . So strictly speaking, the function y (x) cannot be viewed as data as it could be in dealing with interpolating polynomials or splines. A set of arbitrarily distributed discrete data points will not satisfy the construction requirements of the Bernstein polynomials. This appears to be a serious restriction, but as will be seen later, this is not disadvantageous to our purpose. One would then ask, what is the use of the Bernstein polynomials? The answer is that the Bernstein polynomials are approximations to the function y (x) . As we will show later, when the degree n increases, they converge uniformly to y (x) . The values of a and b are not essential in the construction of the Bernstein polynomial. For the sake of convenience, [ 0, 1 ] is chosen as the interval of x . For instance, by a simple linear transformation, any [ a, b ] can be transformed to [ 0, 1 ] by introducing a new independent variable z per the linear transformation z xa ba and x a (b a) z Clearly, z [ 0, 1 ] for x [ a, b ] . Substituting z for x in the Bernstein polynomials, we get Bn ( y; z ) 1 (b a) n n n i (b a) z (b a) i 0 i i n i (1 z ) n i y ( z i ) n n z i (1 z ) n i y ( z i ) i 0 i where zi xi a i , i 0, 1, 2, , n ba n However to comply with common notation, we revert to x [ 0, 1 ] and use x from here on, so the Bernstein polynomials are rewritten as n n n i i Bn ( y; x) x i (1 x) n i y ( ) Bn,i y ( ), x [ 0, 1] n i 0 n i 0 i where Bn ,i ( x) are called the Bernstein basis polynomials and are defined as (6.1.3) 3 n Bn ,i ( x) x i (1 x) n i , i 0, 1, 2, , n, x [ 0, 1] i Clearly, the coefficients of the Bernstein basis polynomials are dependent on n and i and nothing else. There is no function y (x) in the basis formula; it appears in the polynomial, Equation 6.1.3, where it is evaluated at points i / n , not in the basis. To get a sense of the polynomials, let’s first write all the Bernstein basis polynomials up to degree n 4 . n 0 : B0 , 0 1 n 1 : B1, 0 1 x, B1,1 x n 2 : B2,0 (1 x) 2 , B2,1 2 x(1 x), B2, 2 x 2 n 3 : B3,0 (1 x) 3 , B3,1 3x(1 x) 2 , B3, 2 3x 2 (1 x), B3,3 x 3 n 4 : B4,0 (1 x) 4 , B4,1 4 x(1 x) 3 , B4, 2 6 x 2 (1 x) 2 , B4,3 4 x 3 (1 x), B4, 4 x 4 For every degree n , there are n 1 basis polynomials which are the building blocks for the Bernstein polynomials. Looking at these formulas, especially the coefficients, one may recognize that they are familiar. In fact, recalling the expanded formula for (a b) n , the above expressions are for the case a 1 x and b x . So basically, Bn ,i ( x) are the individual terms of the expanded expression of (1 x) x . To make this even easier, the basis polynomials can be generated by recalling the Pascal (Yang Hui, Khayam) Triangle n Power 0 1 2 3 4 5 6 …. Coefficients 1 11 121 1331 14641 1 5 10 10 5 1 1 6 15 20 15 6 1 ………………… which provides the coefficients for powers of (1 x) and x , respectively. Example 6.1.1: Develop a computer program for calculating Bn ,i ( x) . For sure, we can write a program to calculate the formula directly. But since we need it for any degree n , it is better to have a recursive algorithm. To do this, we introduce the following equation: Bn,i ( x) (1 x) Bn 1,i ( x) xBn 1,i 1 ( x), i 1, 2, , n 1 (6.1.4) 4 where i 0 and n are not included. For i 0 , the second term above would require i 1 1, which is not permitted. For i n , the first term would require i n for degree n 1 which is not permitted either. p (Note: in B p ,q , p is always greater than q because for , p q .) q For the i 0 and i n term, we have Bn , 0 ( x) (1 x) Bn 1, 0 ( x); Bn ,n ( x) xBn 1,n 1 ( x) (6.1.5) where Bn 1,0 and Bn 1,n 1 are already obtained from the previous computational cycle. To prove the recursive formula, we have (n 1)! x i (1 x) n i 1 (1 x) i!(n 1 i )! (n 1)! x i 1 (1 x) n i 1 x (i 1)!(n 1 i 1)! (1 x) Bn 1,i ( x) xBn 1,i 1 ( x) (n 1)! (n 1)! x i (1 x) n i i!(n 1 i )! (i 1)!(n i )! n! n i i x i (1 x) n i i!(n i )! n n B n ,i ( x ) The algorithm of the MATLAB code “bernstein_basis.m” is as follows (1) Input the degree n of B and x where the interpolation value is needed; (2) Check to see if x is out of range; if yes, abort the program; if no, proceed; (3) Building bi Bn ,i through Bn 1,i : Assign b1 1 ; Calculate bi using the recursive scheme (Equations 6.1.4 and 6.1.5); Let’s write the recursive formula in MATLAB language function b = bernstein_basis(n,xx) %BERNSTEIN_BASIS Bernstein basis polynomials; %BERNSTEIN_BASIS returns values of Bernstein basis % polynomials Bn,i(xx),n>=1 at xx; %b is a vector of length n+1, b(i+1)=Bn,i(xx), i=0,1,..,n; if ( (xx<0.0) | (xx>1.0) ); error('xx out of range in function "bernstein_basis"!'); end; xx_1=1.0-xx; b=1:n+1; % index shifted from [0,n] to [1,n+1]; for i=1:n; %starting from B_1,1, build Bn,i from Bn-1,i; 5 b(i+1)=xx*b(i); %2nd term in Equation 6.1.5; for j=i-1:-1:1; b(j+1)=xx_1*b(j+1)+xx*b(j); % Equation 6.1.4; end; b(1)=xx_1*b(1); % 1st term in Equation 6.1.5; end; 6.2. Building Bernstein Polynomials Of course, one would note that (1 x) x 1 for all n ’s. Then from the binomial theorem, for any given n , the summation of B n ,i over all i is also exactly 1 . n n n i x (1 x) n i [(1 x) x]n 1 B ( x ) n ,i i 0 i 0 i n n Thus, for the function y ( x) 1 , the Bernstein polynomial, Bn (1; x) Bn ,i ( x) 1 1 so i 0 that in this case Equation 6.1.3 matches the function y = 1 exactly. In general, (6.1.3) only approximates a function y. Differentiating the above equation, we get n n 0 x i 1 (1 x) n i 1 [i(1 x) (n i ) x] i 0 i n n x i 1 (1 x) n i 1 (i nx) i 0 i Multiplying by x(1 x) / n , we get n n i x (1 x) i 0 i n i i ( x) 0 n n n i n i n i i x (1 x) n i x x ( 1 x ) x i n i 0 i 0 i Bn ( x; x) Bn (1; x) Bn ( x; x) xBn (1; x) x n which means for the function y ( x ) x , the Bernstein polynomial is also exactly the function itself. 6 Example 6.2.1. Prove that the Bernstein polynomial Bn ( y; x) is linear in regard to function y. Proof: For any constants a and b , and two functions f and g , we get n n i i Bn (af bg ; x) x i (1 x) n i [af ( ) bg ( )] aBn ( f ; x) bBn ( g ; x) n n i 0 i Then for any linear function y ( x) ax b , we have Bn ( y; x) aBn ( x; x) bBn (1; x) ax b y which means the Bernstein polynomials for a linear function is also exactly the function itself. Could this be true for all polynomial functions? The answer is no. Let’s consider y as a quadratic function, the simplest of which would be y( x) x 2 . Differentiating x Bn ( x; x) , we get n n i 1 x i 1 (1 x) n i 1 (i nx) n i 0 i then multiplying by x(1 x) / n n n x(1 x) i i x i (1 x) n i ( x) n n n i 0 i n n n n i i x i (1 x) n i x x i (1 x) n i n n i 0 i i 0 i 2 Bn ( x 2 ; x) xBn ( x; x) Bn ( x 2 ; x) x 2 So Bn ( x 2 ; x) x 2 , but rather it is an approximation to x 2 . And the error term is Bn ( x 2 ; x ) x 2 x(1 x) 1 0, as n n 4n Generally, we have the following theorem stating that the Bernstein polynomials are an approximation to the function y . 7 Weierstrass Theorem: If y is continuous on [ 0, 1 ] , then Bn ( y; x) converges uniformly to y . Please refer to Buchanan and Turner (1992) for the proof of this theorem. Let’s see how well Bernstein polynomials do in approximating a function. Example 6.2.2. Evaluate the Bernstein polynomials for sin 4 x on [ 0, 1 ] . A MATLAB program “bernstein_example.m” is written for this example. It uses the function bernstein_basis.m. Evaluation of B n(sin4x;x) 1 0.8 sin4x B2(sin4x;x) 0.6 B4(sin4x;x) B3(sin4x;x) B5(sin4x;x) B6(sin4x;x) 0.4 y 0.2 0 -0.2 -0.4 -0.6 -0.8 0 0.1 0.2 0.3 0.4 0.5 x 0.6 0.7 0.8 0.9 1 Figure 6.2.1 Evaluation of sin4x using the Bernstein polynomials As can be seen in Figure 6.2.1, the Bernstein polynomials are not very good at converging towards sin 4 x . Generally, this is true for all functions other than those simple ones discussed above. Well, our interest here is to create the Bezier curve, which is discussed in the following section, not particularly to approximate functions. 8 6.3. Bezier Curve The Bezier curves are intended to develop shapes. For instance, if we view the curves in Figure 6.2.1 as the generation of a sequence of shapes rather than an approximation to a function, we might be satisfied with the results. Indeed, this is the objective of the Bezier curves: the generation of shapes in design. Let’s run “bezier_example_1.m” 0.8 1 (a) 0.8 0.4 n=3 n=4 n=5 n=6 0.2 0 0 0.5 x 0.6 0.4 0.2 1 1 (c) 0.6 0.8 0.4 0.6 y y 0.8 y y 0.6 0.2 0 (b) 0 0.5 x 1 0.5 x 1 (d) 0.4 0 0.5 x 1 0.2 0 Figure 6.3.1 Bezier curves for polygons First, we have four control points in this example i 1 2 3 4 xp 0.0 0.3 0.7 1.0 yp 0.1 0.6 0.8 0.2 Let the function y be the linear spline passing through these points. The shape of the function is then a polygon. This and the resulting Bernstein polynomials for n 3 to 6 are plotted in Figure 6.3.1a. It is clear that these smooth curves converge towards the polygon. Beside the two endpoints, these curves do not pass through the polygon. 9 The polygon is of a concave shape, and so are the curves. When the shape of the polygon is convex, so are the curves as shown in Figure 6.3.1b. This conclusion is generally true. These curves are called “Bezier curves”. Let’s move the second control point in Figure 6.3.1a to (0.3, 0.0). The resulting polygon (Figure 6.3.1c) is convex at the beginning and concave near the end. The Bezier curves also are convex at the beginning and concave near the end. Similar things happen when the third control point in Figure 6.3.1d is changed to (0.7,0.8). So by setting up several control points, the Bezier curves are created from the resulting polygon. These curves have certain shapes like concave, convex or more generally complicated. We are not interested in converging the Bezier curves to the polygon by increasing the degree n. That would be very slow and unnecessary. Our interest is to generate a smooth curve to our liking while n is relatively low. This process is fast and easy to implement in a computer code. And this is the beauty of the Bezier curve. To further illustrate the essence and power of the Bezier curve, let’s look at the following example. It is more complex, but the code remains the same, other than the control points. Example 6.3.2. Create a smooth curve, which satisfying the following requirements 1). Must pass through endpoints (0,0) and (1,0.1); 2). Be convex and increase initially; 3). Be convex and decrease near the end; 4). Reach its maximum value of 0.6 near x=0.6; 5). The maximum should be its only stationary point on [0,1]. We do this exercise in “bezier_example_2.m” As a first step, the following 7 points are selected as control points. The function y is the linear spline across these points. Then the Bernstein polynomials of the orders 3 to 6 are plotted in Figure 6.3.2a. i 1 2 3 4 5 6 7 xp 0 0.2 0.4 0.5 0.7 0.9 1 yp 0 -0.1 0.45 0.6 0.6 -0.1 0.1 The selection of points 1 and 7 are obvious. That is according to requirement No.1 above and should be kept in all subsequent tries. Because of the requirements of the convexity of the curve near both ends, we put y = -0.1 at x = 0.2 and 0.9. Then, we put two points in the middle, (0.5, 0.6) and (0.7, 0.6), in trying to get the curve to pass through (0.6, 0.6). 10 1 1 (a) y 0.5 y 0.5 (b) n=3 n=4 n=5 n=6 0 -0.5 0 0 0.5 x 1 -0.5 1 0 0.5 x 1 (c) (d) y 0.5 y 0.5 1 0 -0.5 0 0 0.5 x 1 -0.5 0 0.5 x 1 Figure 6.3.2 Bezier Curve The resulting curves (n = 3 to 6), plotted in Figure 6.3.2a, do not have a convex shape at the beginning for n = 3, but they do for higher degrees. For n = 5 and 6, the curves decrease from zero. So we probably need to raise the second control point a bit. Also, the high point of the curves only reach y = 0.4, not 0.6 as required. This can be corrected by raising the 4th and 5th control points. Implementing these two changes in the second try, we get curves in Figure 6.3.2b. They are better. The decrease of y at the beginning is eliminated. The high value of y is raised from 0.4 to 0.55. Raising y at x = 0.5 and 0.7 further to 0.9 from 0.8, generates the Bezier curves plotted in Figure 6.3.2c. This time the curves are generally good. Let’s use n = 6 for our final solution. The only adjustment is to move the peak a bit lower and to the right. This is accomplished by setting the two high control points to (0.52, 0.88) and (0.72, 0.88). The n = 6 curve in Figure 6.3.2d is pretty satisfactory with respect to all the requirements. From this example, we can appreciate the flexibility and convenience of the Bezier curves. Setting up the program is simple. It is easy to just have several control points and move them around, or even deleting or adding new control points. These moves do not require any change in the core of the code. 11 6.4 Bezier Curves with Closed Ends So far we discussed the Bezier curves with open ends. Sometimes we would like to generate a closed curve, or a curve having x going back and forth on the x- axis. Obviously the formula above does not permit this. This problem is actually easy to fix. A dummy variable t is introduced and both x and y are treated as functions of t . They can be written as n x(t ) xi Bn ,i (t ), i 0 n y (t ) y i Bn ,i (t ) i 0 For a closed or self-crossed curve, or more precisely, a closed or self-crossed polygon from the control points chosen, we can start from any control point and let t 0 there. Then chose a path to go through all the other points while t increases. Finally we go back to the starting control point and use it again for t 1 . This scheme is a parametric representation of the curve, a very important concept in computer graphics. Example 6.4.2. Generate the infinity sign using “bezier_example_3.m” The plots in Figures 6.4.2 (a), (b) and (c) illustrate Bezier curves for three degrees: n = 7, 9, 11. The control curves for each plot are given by the following data: Plot (a): xp -1 -2/3 -1/3 1/3 2/3 1 2/3 1/3 yp 0 2/3 2/3 -2/3 -2/3 0 2/3 2/3 Plot (b): xp -1 yp 0 -2/3 1 -1/3 1 1/2 -3/2 1 3/2 -3/2 0 Plot (c): xp -0.8 -0.6 -1/4 1/2 1 yp 0 0.9 0.9 -3/2 -3/2 3/2 0 -1/3 -2/3 -2/3 -2/3 -1 0 -2/3 -1 -1 0 1 3/2 1/2 3/2 -1/3 -1 1 3/2 1/2 3/2 -1/4 -0.6 -0.8 -0.9 -0.9 0 where xp, yp are the control points for the curves. Note: the control lines are not shown for plots (b) and (c). 12 1 0.5 0.5 y y 0 0 -0.5 -1 -1 -0.5 (a) 0 x 0.5 1 -0.5 -1 (b) -0.5 0 x 0.5 1 0.5 y Inner-to-outer curves n = 7, are for9, 11. respectively. 0 -0.5 -1 (c) -0.5 0 x 0.5 1 Figure 6.4.1. Creating the infinite sign with the Bezier Curve. 6.5 Interactive Curve Generation A MATLAB code “bezier_interactive.m” is used for interactive plotting of the Bezier curves. It has the following capabilities (1) Add, delete and move control points interactively; (2) Show any of the Bezier curves of degrees n 3, 4,, 10 ; (3) Write the curves as lines or their polynomial coefficients to files; The detail of using the code is explained in “bezier_int_readme.m”. The user should review this file before running the code. References: Cheney, W. and Kincaid, D., “Numerical Mathematics and Computing,” Third Edition, Brooks/Cole Publishing Company, 1994. Buchanan, J.L. and Turner, P.R., “Numerical Methods and Analysis,” McGraw-Hill, 1992.