This page is intended to be a brief reference on implementing Bézier curves. It is not intended to be a full discussion of the topic, only a reference.
A Bézier curve is a sequence of control points on a parameter interval.
The control points may be scalars or vectors, and there may be an
number of them; we will denote the control points as . The here is the order
of the Bézier
curve and is one less than the number of control points.
We will refer to the parameter interval as going from to . We assume .
To find the point on a Bézier curve at some parameter value , we proceed as follows.
Let be the relative parameter value defined by .
If , return . Otherwise, define a set of control
points where the new is the old and the new is the old ; repeat until the new
is zero. The operation is called a
lerp
(short for linear
interpolation).
const lerp = (t,p0,p1) => add(mul(p0,1-t), mul(p1,t))
const lbez = (t, ...p) => {
while(p.length > 1) p = p.slice(1).map((e,i) => lerp(t,p[i],e))
return p[0]
}
In addition to providing the point on the curve at , De Casteljau’s algorithm also splits the original Bézier curve into two: the set of all (in the order created) define the portion of the Bézier curve in the interval and the set of all (in the reverse order created) define the portion of the Bézier curve in the interval
const bezcut = (t, ...p) => {
let front = [], back = []
while(p.length > 0) {
.push(p[0])
front.unshift(p[p.length-1])
back= p.slice(1).map((e,i) => lerp(t,p[i],e))
p
}return [front, back]
}
At , the value of a Bézier curve is . At , the value of a Bézier curve is . In general, the Bézier curve does not pass through any of its other control points.
Bézier curves always remain inside the convex hull of their control points.
Within the interval , de Casteljau’s algorithm is unconditionally numerically stable: it gives the value of the polynomial with as much numerical precision as the control points and values are themselves specified. Outside that interval de Casteljau’s algorithm still works in principle, but it is not stable, accumulating numerical error at a rate polynomial in the distance outside the interval.
Bézier curves also can be degree-elevated and degree-reduced; can efficiently determine their derivative using a hodograph; can represent conic sections if the control points are homogeneous coordinates; and can be generated in a smooth spline using B-splines and their variants. For an extensive treatment, see Thomas Sederberg’s book Computer Aided Geometric Design.