In the
mathematical
Mathematics is an area of knowledge that includes the topics of numbers, formulas and related structures, shapes and the spaces in which they are contained, and quantities and their changes. These topics are represented in modern mathematics ...
field of
numerical analysis
Numerical analysis is the study of algorithms that use numerical approximation (as opposed to symbolic manipulations) for the problems of mathematical analysis (as distinguished from discrete mathematics). It is the study of numerical methods th ...
, De Casteljau's algorithm is a
recursive
Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathemati ...
method to evaluate polynomials in
Bernstein form or
Bézier curve
A Bézier curve ( ) is a parametric curve used in computer graphics and related fields. A set of discrete "control points" defines a smooth, continuous curve by means of a formula. Usually the curve is intended to approximate a real-world shape ...
s, named after its inventor
Paul de Casteljau
Paul de Casteljau (19 November 1930 – 24 March 2022) was a French physicist and mathematician. In 1959, while working at Citroën, he developed an algorithm for evaluating calculations on a certain family of curves, which would later be formal ...
. 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
In the mathematical subfield of numerical analysis, numerical stability is a generally desirable property of numerical algorithms. The precise definition of stability depends on the context. One is numerical linear algebra and the other is algor ...
.
Definition
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
In mathematics, a recurrence relation is an equation according to which the nth term of a sequence of numbers is equal to some combination of the previous terms. Often, only k previous terms of the sequence appear in the equation, for a paramete ...
:
:
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:
:
:
Example implementation
Here is an example implementation of De Casteljau's algorithm in
Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
:
deCasteljau :: Double -> Double, Double)-> (Double, Double)
deCasteljau t = 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
An example implementation of De Casteljau's algorithm in
Python:
def de_casteljau(t, coefs):
beta = 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 = beta * (1 - t) + beta + 1* t
return beta
An example implementation of De Casteljau's algorithm in
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
:
// Example: lerp(0.5, 0.0, 1.0) 0.5
let lerp = (t, p1, p2) => (1 - t) * p1 + t * p2;
// Example: reduce(0.5, ... .0, 1.0, 2.0, 3.0 .5, 1.5, 2.5let reduce = (t, p1, p2, ...ps) => ps.length > 0
? erp(t, p1, p2), ...reduce(t, p2, ...ps) : erp(t, p1, p2)
ERP or Erp may refer to:
Acronyms Economics
* Effective rate of protection of tariffs
* Equity risk premium, excess return on risky investments
* European Recovery Program or Marshall Plan
Medicine
* Effective refractory period, in a cardiac c ...
// Example: deCasteljau(0.5, .0, 1.0, 2.0, 3.0 1.5
let deCasteljau = (t, ps) => ps.length > 1
? deCasteljau(t, reduce(t, ...ps))
: ps
Notes
When doing the calculation by hand it is useful to write down the coefficients in a triangle scheme as
:
When choosing a point ''t''
0 to evaluate a Bernstein polynomial we can use the two diagonals of the triangle scheme to construct a division of the polynomial
: