De Casteljau algorithm and procedure
1) Description
The most basic concept of de Casteljau algorithm is to find C points in the line segment AB, so that the C point divides the AB line segment into a ratio (| AC |: | AB | = u), how to find this C point? ?
The vector of the A to B is B - a, because u is between 0 and 1, the C point is at U (B - a), taking into account the location of the A point, the position of the C point is A U (B - A) = (1 - u) A UB, therefore, for a given U, (1-u) A UB is the C point between A and B, C point divide the AB line segment into u: ( 1-u) ratio.
The thinking of the de Casteljau algorithm is as mentioned below: Suppose we require C (u), where u is between [0, 1], starting from the first multilateral, 00-01-02-03 ...- 0N, The above formula obtained a point 1i 1i 1i on a line segment of 0i to 0 (i 1), 1I point divides the line segment of 0i to 0 (i 1) into u: (1-U) ratio; so we can get N points 10, 11, 12, ...., 1 (n-1), which form a new N-1 line segment.
In the above figure, u = 0.4, 10 is a point on the line segment of 00 to 01, 11 is a point on the 01 to 02 line segments, ..., 14 is a point on the 04 to 05 line segment, and all the new dot is displayed.
The new point is named 1i mode, perform the same operations on these new points, we will get the second multilateral and N-2 composed of (20, 21, ..., 2 (n-2)) N-1 point Tibbon; Perform this operation again, we will obtain a multilateral and N-3 strip of the third by (30, 31, ..., 3 (n-3)) N-2 points; this cycle is carried out N times After that, a single point N0 will be produced; de Casteljau proves that this point is the C (u) we required.
Let us continue to look at the above graphics, get points 20 on the line segments of 10 to 11 divide the line segments of 10 to 11 into u: (1-U) ratio, and can also get the points 21, 12 of the line segments of 11 to 12 13 The points 23, 13 to 14 on the line segment of 13, this third multilateral side has four points and three edges; so continues, we can get new multilateral edges consisting of 30, 31, and 32 points. From this fourth multilateral, we can get the fifth multilaterality consisting of 40 and 41 points; so again, we can get 50, which is the C (u) we required on the curve.
This is the geometric interpretation of the de Casteljau algorithm, very wonderful curve design.
Note: The above original text
Http://www.cs.mtu.edu/~shene/courses/cs3621/notes/spline/bezier/de-casteljau.html
2) Program
///
// PDC device
// FlarrayX composition point X coordinate sequence
// FlarrayY composition point y coordinate sequence
// created by: handwolf
// CREATE TIME: 2004-10-1
///
INT Decasteljau (CDC * PDC, Carray
{
IF (flarrayx.getsize ()! = flarrayy.getsize ()) Return 0;
Float * Pflx, * pfly;
Float fltempx, fltempy, flu; // Flu ------- u parameters
INT I, N, J;
n = flarrayx.getsize ();
IF (n <2) Return 0;
Pflx = new float [n];
Pfly = new float [n];
FLTEMPX = flarrayx.getat (0);
FLTEMPY = flarrayy.getat (0);
For (i = 0; i PFLX [I] = flarrayx.getat (i); Pfly [i] = flarrayy.getat (i); } FOR (Flu = 0; Flu <= 1; Flu = 0.05 / n) { For (i = 1; i For (j = 0; j PFLX [J] = (1-Flu) * PFLX [J] Flu * PFLX [J 1]; Pfly [J] = (1-Flu) * pfly [j] flu * pfly [j 1]; } } PDC-> Moveto (fltempx, fltempy); PDC-> LineTo (PFLX [0], Pfly [0]); FLTEMPX = PFLX [0]; FLTEMPY = Pfly [0]; } Delete [] PFLX; Delete [] pfly; Return 1; } end// Finding a point on a Bézier Curve: de Casteljau's Algorithm Following the construction of a Bézier curve, the next important task is to find the point C (u) on the curve for a particular u. A simple way is to plug u into every basis function, compute the product of each basis function and its Corresponding Control Point, And Finally Add Them Together. While this Works Fine, IT IS Not Numeric All (IE, Could Introduce Numeric Errors During The Course of Evaluating the Bernstein Polynomial). In What Follows, We Shall Only Write Down The Control Point Numbers. That IS, The Control Points Are 00 For P0, 01 for P1, ..., 0i for Pi, ..., 0N for pn. The 0s in these NumBers Indicate The Initial or the 0-TH ITERATION. Later ON, IT Will Be Replaced with 1, 2, 3 And So ON. The fundamental concept of de Casteljau's algorithm is to choose a point C in line segment AB such that C divides the line segment AB in a ratio of u: 1-u (ie, the ratio of the distance between A and C and the distance between A and b IS u). Let us Find A Way to DETERMINE The Point C.The Vector from a to b IS B - A. Since U Is A Ratio In The Range of 0 and 1, Point C Is Located AT U (B - a). TAKING THE POSITION OF A INTO Consideration, Point C IS A U (B - a) = (1 - U) A Ub. Therefore, Given Au, (1 - U) A UB Is The Point C BETWEEN A and B That Divides Ab in A Ratio OF U: 1-U. The Idea of de Casteljau's Algorithm Goes As Follows. Suppose Want To Find C (U), WHERE U IN [0, 1]. Starting with the first polyline, 00-01-02-03 ...- 0N, USE The Above formula to find a point 1i on the leg (IE line segment) from 0i to 0 (i 1) That Divides the line segment 0i and 0 (i 1) IN A Ratio of u: 1-u. in this Way, WE Will Obtain N Points 10, 11, 12, ...., 1 (N-1). They Define A New Polyline of N - 1 LEGS. In The Figure Above, U IS 0.4. 10 IS in The Leg of 00 and 01, 11 IS in the leg of 01 and 02, ..., and 14 is in the leg of 04 and 05. All of these New Points Are in blue. The New Points Are Numbered As 1i's. Apply The Procedure To this New Polyline and WE Shall Get A Second Polyline of N - 1 Points 20, 21, ..., 2 (N-2) and N - 2 Legs. Starting with THIS. STARTING WITH POLYLINE, WE CAN CONSTRUCT A Third ONE OF N - 2 Points 30, 31, ..., 3 (N-3) And n - 3 Legs. Repeating this process n Times Yields a single point point n0. de Casteljau Proved That IS The point c (u) on the curve what corresponds to u. LET US Continue with the Above Figure. Let 20 Be The Point in The Line Segment 10 And 11 in A Ratio of u: 1-u. Similarly, Choose 21 on The Leg of 11 and 12, 22 on the leg of 12 and 13, and 23 on the leg of 13 and 14. this gives a third polyline defined by 20, 21, 22 and 23. This Third Polyline HAS 4 Points and 3 Legs. Keep Doing this and we share Obtain a New Polyline of Three Points 30, 31 and 32. from this Fourth Polyline, WE Have The Fifth ONE OF TWO POINTS 40 and 41. Do It Once More, And We Have 50, The Point C (0.4) on The curve. This is the geometric interpretation of de Casteljau's Algorithm, ONE of the MOST ELEGANT RESULT IN CURVE Design. Actual computation Given The Above Geometric Interpretation of De Casteljau's Algorithm, WE Shall Present a Computation Method, Which is shown in the folowing figure. First, all given control points are arranged into a column, which is the left-most one in the figure. For each pair of adjacent control points, draw a south-east bound arrow and a north-east bound arrow, and write down a New Point At the InterSection of The Two Adjacent Arrows. for Example, IF The Two Adjacent Points Are IJ and I (J 1), The New Point IS (i 1) J. The South-East (resp., North- East Bound Arrow Means Multiplying 1 - U (RESP., U) To The Point At Its Tail, IJ (Resp., I (J 1)), and the new point is the sum. Thus, from the initial column, column 0, we compute column 1;.. From column we 1 obtain column 2 and so on Eventually, after n applications we shall arrive at a single point n0 and this is the point on the curve The following Algorithm Summarizes What We Haveses ARRAY P OF N 1 Points and au in The Range of 0 and Returns a Point on The Bézier Curve C (U) .Input: array P [0: N] of N 1 Points and Real Number u in [0, 1] Output: Point on Curve, C ( u) Working: Point Array Q [0: n] for i: = 0 TO n DO Q [ I]: = P I]; // save input for K: = 1 TO n DO for i: = 0 TO N - K DO Q [ I]: = (1 - u) Q [ I] U Q [ i 1]; Return Q [0]; A Recurrence RELATION The Above Computation Can Be Expressed Recursively, Initially, Let P0, J Be Pj For J = 0, 1, ..., N. That IS, P0, J I J-TH Entry On Column 0. The Computation of Entry J On Column I is The FOLLOWING: More precisely, entry pi, j is the amount of (1-u) PI-1, J (Upper-Left Corner) and upi-1, J 1 (Lower-Left Corner). The final result (IE, The Point On The Curve) IS PN, 0. Based On this IDEA, One May Immediately Come Up with The Following Recursive Procedure: FUNCTION Decasteljau i, j) Begin IF i = 0 THEN Return P 0, J Else Return (1- u) * Decasteljau I-1, J) u * Decasteljau I-1, J 1) end This procedure looks simple and short;... However, it is extremely inefficient Here is why We start with a call to deCasteljau (n, 0) for computing Pn, 0 The else part splits this call into two more calls, deCasteljau (n -1,0) for computing pn-1,0 and decasteljau (N-1, 1) for computing PN-1, 1. Consider the call to decasteljau (N-1, 0). It splits Into Two more calls, decasteljau (N-2, 0) for Computing PN-2,0 and Decasteljau (N-2, 1) for Computing PN-2, 1. THE CALL TO DECASTELJAU (N-1, 1) Splits INTO Two Calls, DECASTELJAU (N-2, 1) for Computing PN-2, 1 and Decasteljau (N-2, 2) for Computing PN-2, 2. Thus, deCasteljau (n-2,1) is called twice. If we keep expanding these function calls, we should discover that almost all function calls for computing Pi, j are repeated, not once but many times. How bad is this? In Fact, The Above Computation Scheme Is Identical To The Following Way of Computing The N-TH Fibonacci Number: Function Fibonacci N) Begin IF N = 0 oral n = 1 THEN Return 1 Else Return Fibonacci N-1) Fibonacci N-2) end This program takes an exponential number of function calls (an exercise) to compute Fibonacci (n). Therefore, the above recursive version of de Casteljau's algorithm is not suitable for direct implementation, although it looks simple and elegant! An Interesting Observation The triangular computation scheme of de Casteljau's algorithm offers an interesting observation. Take a look at the following computation on a Bézier curve of degree 7 defined by 8 control points 00, 01, ..., 07. Let us consider a set of consecutive points on the same column as the control points of a Bézier curve. Then, given au in [0,1], how do we compute the corresponding point on this Bézier curve? If de Casteljau's algorithm is applied to these control points, the point on The curve is the oppositive Vertex of the Equilateral's base factory factory! For example, if the selected points are 02, 03, 04 and 05, the point on the curve defined by these four control points that corresponds to u is 32. See the blue triangle. If the selected points are 11, 12 and 13, The Point On The Curve IS 31. See The Yellow Triangle. if The Selected Points Are 30, 31, 32, 33 and 34, The Point on The Curve is 70.by The Same Reason, 70 Is The Point on The Bézier Curve Defined By Control Points 60 and 61. It is Also The Point on The Curve Defined by 50, 51 And 52, And On The Curve Defined by 40, 41, 42 and 43.In General, IF WE SELECT A Point And Draw An Equilateral AS SHOWN ABOVE, The Base of this Equilateral Consists of the control point.com which the successd pin