Bézier curves and the Bernstein matrix

Smooth curves from a handful of control points, built by repeated interpolation, and fitted to data with one least-squares solve.

What you'll learn
  • Evaluate a Bézier curve with Bernstein polynomials and with de Casteljau's construction
  • Build the Bernstein matrix that samples a curve with one matrix product
  • Fit control points to data with a pseudoinverse

A Bézier curve of order \(n\) is defined by \(n + 1\) control points \(P_0, \dots, P_n\):

\[B(t) = \sum_{k=0}^{n} \binom{n}{k}\, t^k (1-t)^{n-k}\, P_k, \qquad 0 \le t \le 1\]

The weights \(b_{k,n}(t) = \binom{n}{k} t^k (1-t)^{n-k}\) are the Bernstein polynomials. They are never negative and always sum to 1 (they are the terms of \((t + (1 - t))^n\)), so every point of the curve is a weighted average of the control points and the curve stays inside their convex hull. It starts at \(P_0\), ends at \(P_n\), and is pulled towards the points in between.

The notebook writes the weights exactly as the formula reads:

Mtk = lambda n, t, k: t ** k * (1 - t) ** (n - k) * n_over_k(n, k)
BezierCoeff = lambda ts: [[Mtk(order, t, k) for k in range(order + 1)] for t in ts]

The same curve by repeated interpolation

There is a geometric way to get the same point. Interpolate between each pair of neighbouring control points at fraction \(t\); repeat on the new, shorter list; after \(n\) rounds one point remains, and it is \(B(t)\). This is de Casteljau’s algorithm, and it is the numerically stable way to evaluate a Bézier curve.

De Casteljau's construction sweeping t from 0 to 1. The dashed polygon joins the control points; each coloured level interpolates the one before at the same t. The bars are the Bernstein weights — one row of the Bernstein matrix — and the readout checks that the weighted sum gives the same point.

The Bernstein matrix

Evaluate the weights at \(m\) sample values \(t_1, \dots, t_m\) and stack them into an \(m \times (n + 1)\) matrix \(M\), with \(M_{ik} = b_{k,n}(t_i)\). Then sampling the whole curve is one product, \(B = M P\), where the rows of \(P\) are the control points. Every row of \(M\) sums to 1, which is easy to check numerically.

Read backwards, the same equation fits a curve to data. Given \(m\) data points and a parameter \(t_i\) for each, the control points that best explain them in the least-squares sense are \(P = M^{+} D\), with \(M^{+}\) the pseudoinverse. That is precisely what curve_utils.py (in the Bash course folder) does for a lane marking: it sets each \(t_i\) by the fraction of arc length travelled, solves with np.linalg.pinv, and then pins the first and last control points to the data’s endpoints. The animation uses its result for the script’s 16 lane points: an order-3 fit sits 0.64 units from the data on average (root mean square), an order-5 fit 0.33.

Because the end points are overwritten after the least-squares solve, the fit is no longer optimal for the remaining points; a constrained solve, which keeps the ends fixed and fits only the interior control points, is the principled alternative.

Try it yourself. Implement de_casteljau(P, t) and check it against M @ P for 50 values of \(t\) to machine precision. Then split a curve at \(t = 0.5\): the intermediate points of de Casteljau’s construction are exactly the control points of the two halves.

View source on GitHub →