fbpx
Wikipedia

De Casteljau's algorithm

In the mathematical field of numerical analysis, De Casteljau's algorithm is a recursive method to evaluate polynomials in Bernstein form or Bézier curves, named after its inventor Paul de Casteljau. De Casteljau's algorithm can also be used to split a single Bézier curve into two Bézier curves at an arbitrary parameter value.

Although the algorithm is slower for most architectures when compared with the direct approach, it is more numerically stable.[citation needed]

Definition edit

A Bézier curve   (of degree  , with control points  ) can be written in Bernstein form as follows

 

where   is a Bernstein basis polynomial

 

The curve at point   can be evaluated with the recurrence relation

 
 

Then, the evaluation of   at point   can be evaluated in   operations. The result   is given by

 

Moreover, the Bézier curve   can be split at point   into two curves with respective control points:

 
 

Geometric interpretation edit

The geometric interpretation of De Casteljau's algorithm is straightforward.

  • Consider a Bézier curve with control points  . Connecting the consecutive points we create the control polygon of the curve.
  • Subdivide now each line segment of this polygon with the ratio   and connect the points you get. This way you arrive at the new polygon having one fewer segment.
  • Repeat the process until you arrive at the single point – this is the point of the curve corresponding to the parameter  .

The following picture shows this process for a cubic Bézier curve:

 

Note that the intermediate points that were constructed are in fact the control points for two new Bézier curves, both exactly coincident with the old one. This algorithm not only evaluates the curve at  , but splits the curve into two pieces at  , and provides the equations of the two sub-curves in Bézier form.

The interpretation given above is valid for a nonrational Bézier curve. To evaluate a rational Bézier curve in  , we may project the point into  ; for example, a curve in three dimensions may have its control points   and weights   projected to the weighted control points  . The algorithm then proceeds as usual, interpolating in  . The resulting four-dimensional points may be projected back into three-space with a perspective divide.

In general, operations on a rational curve (or surface) are equivalent to operations on a nonrational curve in a projective space. This representation as the "weighted control points" and weights is often convenient when evaluating rational curves.

Notation edit

When doing the calculation by hand it is useful to write down the coefficients in a triangle scheme as

 

When choosing a point t0 to evaluate a Bernstein polynomial we can use the two diagonals of the triangle scheme to construct a division of the polynomial

 

into

 

and

 

Bézier curve edit

 
A Bézier curve

When evaluating a Bézier curve of degree n in 3-dimensional space with n + 1 control points Pi

 

with

 

we split the Bézier curve into three separate equations

 
 
 

which we evaluate individually using De Casteljau's algorithm.

Example edit

We want to evaluate the Bernstein polynomial of degree 2 with the Bernstein coefficients

 
 
 

at the point t0.

We start the recursion with

 
 

and with the second iteration the recursion stops with

 

which is the expected Bernstein polynomial of degree 2.

Implementations edit

Here are example implementations of De Casteljau's algorithm in various programming languages.

Haskell edit

deCasteljau :: Double -> [(Double, Double)] -> (Double, Double) deCasteljau t [b] = b deCasteljau t coefs = deCasteljau t reduced  where  reduced = zipWith (lerpP t) coefs (tail coefs)  lerpP t (x0, y0) (x1, y1) = (lerp t x0 x1, lerp t y0 y1)  lerp t a b = t * b + (1 - t) * a 

Python edit

def de_casteljau(t, coefs): beta = [c for c in coefs] # values in this list are overridden n = len(beta) for j in range(1, n): for k in range(n - j): beta[k] = beta[k] * (1 - t) + beta[k + 1] * t return beta[0] 

JavaScript edit

The following function applies De Casteljau's algorithm to an array of points, resolving the final midpoint with the additional properties in and out (for the midpoint's "in" and "out" tangents, respectively).

function deCasteljau(points, position = 0.5){  let a, b, midpoints = [];  while(points.length > 1){  const num = points.length - 1;  for(let i = 0; i < num; ++i){  a = points[i];  b = points[i+1];  midpoints.push([  a[0] + ((b[0] - a[0]) * position),  a[1] + ((b[1] - a[1]) * position),  ]);  }  points = midpoints;  midpoints = [];  }  return Object.assign(points[0], {in: a, out: b}); } 

The following example calls this function with the green points below, exactly halfway along the curve. The resulting coordinates should equal  , or the position of the centremost red point.

 
Intermediate line segments obtained by recursively applying linear interpolation to adjacent points.
{  /* Definition of deCasteljau() function omitted for brevity */  const nodes = window.document.querySelectorAll("circle.n0-point");  const points = Array.from(nodes).map(({cx, cy}) => [cx.baseVal.value, cy.baseVal.value]);  deCasteljau(points); // Result: [192, 32] } 

See also edit

References edit

  • Farin, Gerald & Hansford, Dianne (2000). The Essentials of CAGD. Natic, MA: A K Peters, Ltd. ISBN 1-56881-123-3

External links edit

  • Piecewise linear approximation of Bézier curves – description of De Casteljau's algorithm, including a criterion to determine when to stop the recursion
  • Bezier Curves and Picasso — Description and illustration of De Casteljau's algorithm applied to cubic Bézier curves.
  • de Casteljau's algorithm - Implementation help and interactive demonstration of the algorithm.

casteljau, algorithm, mathematical, field, numerical, analysis, recursive, method, evaluate, polynomials, bernstein, form, bézier, curves, named, after, inventor, paul, casteljau, also, used, split, single, bézier, curve, into, bézier, curves, arbitrary, param. In the mathematical field of numerical analysis De Casteljau s algorithm is a recursive method to evaluate polynomials in Bernstein form or Bezier curves named after its inventor Paul de Casteljau De Casteljau s algorithm can also be used to split a single Bezier curve into two Bezier curves at an arbitrary parameter value Although the algorithm is slower for most architectures when compared with the direct approach it is more numerically stable citation needed Contents 1 Definition 1 1 Geometric interpretation 1 2 Notation 2 Bezier curve 3 Example 4 Implementations 4 1 Haskell 4 2 Python 4 3 JavaScript 5 See also 6 References 7 External linksDefinition editA Bezier curve B displaystyle B nbsp of degree n displaystyle n nbsp with control points b 0 b n displaystyle beta 0 ldots beta n nbsp can be written in Bernstein form as follows B t i 0 n b i b i n t displaystyle B t sum i 0 n beta i b i n t nbsp where b displaystyle b nbsp is a Bernstein basis polynomial b i n t n i 1 t n i t i displaystyle b i n t n choose i 1 t n i t i nbsp The curve at point t 0 displaystyle t 0 nbsp can be evaluated with the recurrence relation b i 0 b i i 0 n displaystyle beta i 0 beta i i 0 ldots n nbsp b i j b i j 1 1 t 0 b i 1 j 1 t 0 i 0 n j j 1 n displaystyle beta i j beta i j 1 1 t 0 beta i 1 j 1 t 0 i 0 ldots n j j 1 ldots n nbsp Then the evaluation of B displaystyle B nbsp at point t 0 displaystyle t 0 nbsp can be evaluated in n 2 displaystyle binom n 2 nbsp operations The result B t 0 displaystyle B t 0 nbsp is given by B t 0 b 0 n displaystyle B t 0 beta 0 n nbsp Moreover the Bezier curve B displaystyle B nbsp can be split at point t 0 displaystyle t 0 nbsp into two curves with respective control points b 0 0 b 0 1 b 0 n displaystyle beta 0 0 beta 0 1 ldots beta 0 n nbsp b 0 n b 1 n 1 b n 0 displaystyle beta 0 n beta 1 n 1 ldots beta n 0 nbsp Geometric interpretation edit The geometric interpretation of De Casteljau s algorithm is straightforward Consider a Bezier curve with control points P 0 P n displaystyle P 0 P n nbsp Connecting the consecutive points we create the control polygon of the curve Subdivide now each line segment of this polygon with the ratio t 1 t displaystyle t 1 t nbsp and connect the points you get This way you arrive at the new polygon having one fewer segment Repeat the process until you arrive at the single point this is the point of the curve corresponding to the parameter t displaystyle t nbsp The following picture shows this process for a cubic Bezier curve nbsp Note that the intermediate points that were constructed are in fact the control points for two new Bezier curves both exactly coincident with the old one This algorithm not only evaluates the curve at t displaystyle t nbsp but splits the curve into two pieces at t displaystyle t nbsp and provides the equations of the two sub curves in Bezier form The interpretation given above is valid for a nonrational Bezier curve To evaluate a rational Bezier curve in R n displaystyle mathbf R n nbsp we may project the point into R n 1 displaystyle mathbf R n 1 nbsp for example a curve in three dimensions may have its control points x i y i z i displaystyle x i y i z i nbsp and weights w i displaystyle w i nbsp projected to the weighted control points w i x i w i y i w i z i w i displaystyle w i x i w i y i w i z i w i nbsp The algorithm then proceeds as usual interpolating in R 4 displaystyle mathbf R 4 nbsp The resulting four dimensional points may be projected back into three space with a perspective divide In general operations on a rational curve or surface are equivalent to operations on a nonrational curve in a projective space This representation as the weighted control points and weights is often convenient when evaluating rational curves Notation edit When doing the calculation by hand it is useful to write down the coefficients in a triangle scheme as b 0 b 0 0 b 0 1 b 1 b 1 0 b 0 n b n 1 b n 1 0 b n 1 1 b n b n 0 displaystyle begin matrix beta 0 amp beta 0 0 amp amp amp amp amp beta 0 1 amp amp beta 1 amp beta 1 0 amp amp amp amp amp amp ddots amp vdots amp amp vdots amp amp beta 0 n amp amp amp amp beta n 1 amp beta n 1 0 amp amp amp amp amp beta n 1 1 amp amp beta n amp beta n 0 amp amp amp end matrix nbsp When choosing a point t0 to evaluate a Bernstein polynomial we can use the two diagonals of the triangle scheme to construct a division of the polynomial B t i 0 n b i 0 b i n t t 0 1 displaystyle B t sum i 0 n beta i 0 b i n t quad t in 0 1 nbsp into B 1 t i 0 n b 0 i b i n t t 0 t 0 t 0 displaystyle B 1 t sum i 0 n beta 0 i b i n left frac t t 0 right quad t in 0 t 0 nbsp and B 2 t i 0 n b i n i b i n t t 0 1 t 0 t t 0 1 displaystyle B 2 t sum i 0 n beta i n i b i n left frac t t 0 1 t 0 right quad t in t 0 1 nbsp Bezier curve edit nbsp A Bezier curveWhen evaluating a Bezier curve of degree n in 3 dimensional space with n 1 control points Pi B t i 0 n P i b i n t t 0 1 displaystyle mathbf B t sum i 0 n mathbf P i b i n t t in 0 1 nbsp with P i x i y i z i displaystyle mathbf P i begin pmatrix x i y i z i end pmatrix nbsp we split the Bezier curve into three separate equations B 1 t i 0 n x i b i n t t 0 1 displaystyle B 1 t sum i 0 n x i b i n t t in 0 1 nbsp B 2 t i 0 n y i b i n t t 0 1 displaystyle B 2 t sum i 0 n y i b i n t t in 0 1 nbsp B 3 t i 0 n z i b i n t t 0 1 displaystyle B 3 t sum i 0 n z i b i n t t in 0 1 nbsp which we evaluate individually using De Casteljau s algorithm Example editWe want to evaluate the Bernstein polynomial of degree 2 with the Bernstein coefficients b 0 0 b 0 displaystyle beta 0 0 beta 0 nbsp b 1 0 b 1 displaystyle beta 1 0 beta 1 nbsp b 2 0 b 2 displaystyle beta 2 0 beta 2 nbsp at the point t0 We start the recursion with b 0 1 b 0 0 1 t 0 b 1 0 t 0 b 0 1 t 0 b 1 t 0 displaystyle beta 0 1 beta 0 0 1 t 0 beta 1 0 t 0 beta 0 1 t 0 beta 1 t 0 nbsp b 1 1 b 1 0 1 t 0 b 2 0 t 0 b 1 1 t 0 b 2 t 0 displaystyle beta 1 1 beta 1 0 1 t 0 beta 2 0 t 0 beta 1 1 t 0 beta 2 t 0 nbsp and with the second iteration the recursion stops with b 0 2 b 0 1 1 t 0 b 1 1 t 0 b 0 1 t 0 1 t 0 b 1 t 0 1 t 0 b 1 1 t 0 t 0 b 2 t 0 t 0 b 0 1 t 0 2 b 1 2 t 0 1 t 0 b 2 t 0 2 displaystyle begin aligned beta 0 2 amp beta 0 1 1 t 0 beta 1 1 t 0 amp beta 0 1 t 0 1 t 0 beta 1 t 0 1 t 0 beta 1 1 t 0 t 0 beta 2 t 0 t 0 amp beta 0 1 t 0 2 beta 1 2t 0 1 t 0 beta 2 t 0 2 end aligned nbsp which is the expected Bernstein polynomial of degree 2 Implementations editHere are example implementations of De Casteljau s algorithm in various programming languages Haskell edit deCasteljau Double gt Double Double gt Double Double deCasteljau t b b deCasteljau t coefs deCasteljau t reduced where reduced zipWith lerpP t coefs tail coefs lerpP t x0 y0 x1 y1 lerp t x0 x1 lerp t y0 y1 lerp t a b t b 1 t a Python edit def de casteljau t coefs beta c for c in coefs values in this list are overridden n len beta for j in range 1 n for k in range n j beta k beta k 1 t beta k 1 t return beta 0 JavaScript edit The following function applies De Casteljau s algorithm to an array of points resolving the final midpoint with the additional properties in and out for the midpoint s in and out tangents respectively function deCasteljau points position 0 5 let a b midpoints while points length gt 1 const num points length 1 for let i 0 i lt num i a points i b points i 1 midpoints push a 0 b 0 a 0 position a 1 b 1 a 1 position points midpoints midpoints return Object assign points 0 in a out b The following example calls this function with the green points below exactly halfway along the curve The resulting coordinates should equal 192 32 displaystyle 192 32 nbsp or the position of the centremost red point nbsp Intermediate line segments obtained by recursively applying linear interpolation to adjacent points Definition of deCasteljau function omitted for brevity const nodes window document querySelectorAll circle n0 point const points Array from nodes map cx cy gt cx baseVal value cy baseVal value deCasteljau points Result 192 32 See also editBezier curves De Boor s algorithm Horner scheme to evaluate polynomials in monomial form Clenshaw algorithm to evaluate polynomials in Chebyshev formReferences editFarin Gerald amp Hansford Dianne 2000 The Essentials of CAGD Natic MA A K Peters Ltd ISBN 1 56881 123 3External links editPiecewise linear approximation of Bezier curves description of De Casteljau s algorithm including a criterion to determine when to stop the recursion Bezier Curves and Picasso Description and illustration of De Casteljau s algorithm applied to cubic Bezier curves de Casteljau s algorithm Implementation help and interactive demonstration of the algorithm Retrieved from https en wikipedia org w index php title De Casteljau 27s algorithm amp oldid 1157540172, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.