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
A Bézier curve [math]\displaystyle{ B }[/math] (of degree [math]\displaystyle{ n }[/math], with control points [math]\displaystyle{ \beta_0, \ldots, \beta_n }[/math]) can be written in Bernstein form as follows
- [math]\displaystyle{ B(t) = \sum_{i=0}^{n}\beta_{i}b_{i,n}(t), }[/math]
where [math]\displaystyle{ b }[/math] is a Bernstein basis polynomial
- [math]\displaystyle{ b_{i,n}(t) = {n \choose i}(1-t)^{n-i}t^i. }[/math]
The curve at point [math]\displaystyle{ t_0 }[/math] can be evaluated with the recurrence relation
- [math]\displaystyle{ \beta_i^{(0)} := \beta_i,\ \ i=0,\ldots,n }[/math]
- [math]\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 }[/math]
Then, the evaluation of [math]\displaystyle{ B }[/math] at point [math]\displaystyle{ t_0 }[/math] can be evaluated in [math]\displaystyle{ \binom{n}{2} }[/math] operations. The result [math]\displaystyle{ B(t_0) }[/math] is given by
- [math]\displaystyle{ B(t_0) = \beta_0^{(n)}. }[/math]
Moreover, the Bézier curve [math]\displaystyle{ B }[/math] can be split at point [math]\displaystyle{ t_0 }[/math] into two curves with respective control points:
- [math]\displaystyle{ \beta_0^{(0)},\beta_0^{(1)},\ldots,\beta_0^{(n)} }[/math]
- [math]\displaystyle{ \beta_0^{(n)},\beta_1^{(n-1)},\ldots,\beta_n^{(0)} }[/math]
Geometric interpretation
The geometric interpretation of De Casteljau's algorithm is straightforward.
- Consider a Bézier curve with control points [math]\displaystyle{ P_0,...,P_n }[/math]. Connecting the consecutive points we create the control polygon of the curve.
- Subdivide now each line segment of this polygon with the ratio [math]\displaystyle{ t : (1-t) }[/math] 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 [math]\displaystyle{ t }[/math].
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 [math]\displaystyle{ t }[/math], but splits the curve into two pieces at [math]\displaystyle{ t }[/math], 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 [math]\displaystyle{ \mathbf{R}^n }[/math], we may project the point into [math]\displaystyle{ \mathbf{R}^{n+1} }[/math]; for example, a curve in three dimensions may have its control points [math]\displaystyle{ \{(x_i, y_i, z_i)\} }[/math] and weights [math]\displaystyle{ \{w_i\} }[/math] projected to the weighted control points [math]\displaystyle{ \{(w_ix_i, w_iy_i, w_iz_i, w_i)\} }[/math]. The algorithm then proceeds as usual, interpolating in [math]\displaystyle{ \mathbf{R}^4 }[/math]. 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
When doing the calculation by hand it is useful to write down the coefficients in a triangle scheme as
- [math]\displaystyle{ \begin{matrix} \beta_0 & = \beta_0^{(0)} & & & \\ & & \beta_0^{(1)} & & \\ \beta_1 & = \beta_1^{(0)} & & & \\ & & & \ddots & \\ \vdots & & \vdots & & \beta_0^{(n)} \\ & & & & \\ \beta_{n-1} & = \beta_{n-1}^{(0)} & & & \\ & & \beta_{n-1}^{(1)} & & \\ \beta_n & = \beta_n^{(0)} & & & \\ \end{matrix} }[/math]
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
- [math]\displaystyle{ B(t) = \sum_{i=0}^n \beta_i^{(0)} b_{i,n}(t), \quad t \in [0,1] }[/math]
into
- [math]\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] }[/math]
and
- [math]\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]. }[/math]
Bézier curve
When evaluating a Bézier curve of degree n in 3-dimensional space with n + 1 control points Pi
- [math]\displaystyle{ \mathbf{B}(t) = \sum_{i=0}^{n} \mathbf{P}_i b_{i,n}(t),\ t \in [0,1] }[/math]
with
- [math]\displaystyle{ \mathbf{P}_i := \begin{pmatrix} x_i \\ y_i \\ z_i \end{pmatrix}, }[/math]
we split the Bézier curve into three separate equations
- [math]\displaystyle{ B_1(t) = \sum_{i=0}^{n} x_i b_{i,n}(t),\ t \in [0,1] }[/math]
- [math]\displaystyle{ B_2(t) = \sum_{i=0}^{n} y_i b_{i,n}(t),\ t \in [0,1] }[/math]
- [math]\displaystyle{ B_3(t) = \sum_{i=0}^{n} z_i b_{i,n}(t),\ t \in [0,1] }[/math]
which we evaluate individually using De Casteljau's algorithm.
Example
We want to evaluate the Bernstein polynomial of degree 2 with the Bernstein coefficients
- [math]\displaystyle{ \beta_0^{(0)} = \beta_0 }[/math]
- [math]\displaystyle{ \beta_1^{(0)} = \beta_1 }[/math]
- [math]\displaystyle{ \beta_2^{(0)} = \beta_2 }[/math]
at the point t0.
We start the recursion with
- [math]\displaystyle{ \beta_0^{(1)} = \beta_0^{(0)} (1-t_0) + \beta_1^{(0)}t_0 = \beta_0(1-t_0) + \beta_1 t_0 }[/math]
- [math]\displaystyle{ \beta_1^{(1)} = \beta_1^{(0)} (1-t_0) + \beta_2^{(0)}t_0 = \beta_1(1-t_0) + \beta_2 t_0 }[/math]
and with the second iteration the recursion stops with
- [math]\displaystyle{ \begin{align} \beta_0^{(2)} & = \beta_0^{(1)} (1-t_0) + \beta_1^{(1)} t_0 \\ \ & = \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 \\ \ & = \beta_0 (1-t_0)^2 + \beta_1 2t_0(1-t_0) + \beta_2 t_0^2 \end{align} }[/math]
which is the expected Bernstein polynomial of degree 2.
Implementations
Here are example implementations of De Casteljau's algorithm in various programming languages.
Haskell
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
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
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 Template:Colour points below, exactly halfway along the curve. The resulting coordinates should equal [math]\displaystyle{ (192, 32) }[/math], or the position of the centremost Template:Colour point.
{ /* 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
- Bézier curves
- De Boor's algorithm
- Horner scheme to evaluate polynomials in monomial form
- Clenshaw algorithm to evaluate polynomials in Chebyshev form
References
- Farin, Gerald & Hansford, Dianne (2000). The Essentials of CAGD. Natic, MA: A K Peters, Ltd. ISBN:1-56881-123-3
External links
- 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.
Original source: https://en.wikipedia.org/wiki/De Casteljau's algorithm.
Read more |