Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 50 ยท Curves and Surfaces

Bezier Curves

Understand Bezier curves: Bernstein polynomials, the de Casteljau algorithm, cubic curves, subdivision and Canvas bezierCurveTo, with live editors.

Open any vector drawing program, any font, any SVG path: underneath, you will almost certainly find Bezier curves. They were developed in the 1960s by engineers at the French car makers Renault (Pierre Bezier) and Citroen (Paul de Casteljau) who needed to describe car bodies with a few numbers. The idea is both simple and powerful: a handful of control points define a smooth curve, and you shape the curve by moving the points.

Unlike the interpolating splines from the previous lesson, a Bezier curve only passes through its first and last control points. The inner points act like magnets that pull the curve toward them. That behavior turns out to be much easier to control when you are designing a shape by hand.

From lerp to curve

Start with what you already know. The straight line between two points is a linear interpolation:

TEXT
P(t) = (1 โˆ’ t) P0 + t P1

That is a Bezier curve of degree 1. For three points you can build a degree-2 curve by nesting: first interpolate between P0 and P1, and between P1 and P2, then interpolate between those two results. For four points, repeat once more and you have the cubic Bezier, the version used everywhere.

Written out as a blend of the control points, the cubic is:

TEXT
P(t) = (1โˆ’t)ยณ P0 + 3(1โˆ’t)ยฒt P1 + 3(1โˆ’t)tยฒ P2 + tยณ P3        0 <= t <= 1

The four factors are the Bernstein polynomials of degree 3. In general, a degree-n Bezier curve with control points P0 ... Pn is

TEXT
P(t) = sum over i of  B(i,n)(t) * Pi          B(i,n)(t) = C(n,i) t^i (1โˆ’t)^(nโˆ’i)

where C(n,i) is the binomial coefficient (the numbers from Pascal's triangle, 1 3 3 1 for the cubic). Each Bernstein polynomial is the weight of one control point, and the weights have properties that give Bezier curves their good behavior:

  • Non-negative and summing to 1 at every t (a "partition of unity"). Each curve point is a weighted average of the control points, so the curve can never fly outside them.
  • Therefore the curve lies inside the convex hull of its control points. This makes quick bounding and intersection tests possible.
  • At t = 0 only B(0,n) is non-zero, and at t = 1 only B(n,n). So the curve starts at P0 and ends at Pn.
  • The tangent at the start points along P1 โˆ’ P0, and at the end along Pn โˆ’ Pnโˆ’1. The first and last segment of the control polygon are tangent to the curve.

The first demo draws the four cubic weights as functions of t. Drag the slider and notice how the weight of P1 and P2 peak at one third and two thirds, while the sum (dashed line) stays at exactly 1.

Bernstein weights of a cubic

The de Casteljau algorithm

You could evaluate the formula directly, but there is a more intuitive and more numerically stable way: de Casteljau's algorithm. It uses only repeated linear interpolation, the very construction from the start of this lesson.

To find the point at parameter t:

  1. Interpolate each pair of neighboring control points at t. Four points give three new points.
  2. Interpolate neighbors of those: two points.
  3. Interpolate once more: a single point, which is P(t).
flowchart TD
  P0["P0"] --> Q0["Q0"]
  P1["P1"] --> Q0
  P1 --> Q1["Q1"]
  P2["P2"] --> Q1
  P2 --> Q2["Q2"]
  P3["P3"] --> Q2
  Q0 --> R0["R0"]
  Q1 --> R0
  Q1 --> R1["R1"]
  Q2 --> R1
  R0 --> S["S = P(t)"]
  R1 --> S

Each arrow pair is one lerp: four points collapse to three, two and finally one.

JavaScript
function deCasteljau(points, t) {
  let level = points;
  while (level.length > 1) {
    level = level.slice(1).map((p, i) => [
      (1 - t) * level[i][0] + t * p[0],
      (1 - t) * level[i][1] + t * p[1],
    ]);
  }
  return level[0];
}

This works for any number of control points, which is why it is the generic evaluator. Try the construction below: drag the four points, then move t. The green and orange layers are the intermediate points, and the red dot is the curve point. Press the button to animate t.

De Casteljau construction

Working with cubic Bezier curves

The cubic is the standard because it matches the minimum we asked for in the last lesson: position and direction at both ends. In fact, it is the same family as Hermite curves, only written with different handles. Converting is a matter of arithmetic:

TEXT
T0 = 3 (P1 โˆ’ P0)        T1 = 3 (P3 โˆ’ P2)

Read it as: the Hermite tangent is three times the vector from an end point to its neighboring control point. Going the other way, P1 = P0 + T0/3 and P2 = P3 โˆ’ T1/3.

Joining two cubics. To chain Bezier segments into a long path, make the joint smooth:

  • C0: the last point of one segment equals the first point of the next.
  • G1: additionally, the last control handle of the first segment, the joint, and the first control handle of the next one are on one straight line. Drawing programs enforce this with their "smooth node" handles.
  • C1: additionally, both handles have the same length (the joint is the midpoint of them).

Derivative. The tangent of a cubic is itself a Bezier curve of degree 2, built from the differences of control points:

TEXT
P'(t) = 3 [ (1โˆ’t)ยฒ (P1โˆ’P0) + 2(1โˆ’t)t (P2โˆ’P1) + tยฒ (P3โˆ’P2) ]

This helps when you need the direction of the curve for a car on a road, an arrowhead at the end of a path or a normal for offsetting.

Subdivision

De Casteljau gives you more than one point. The intermediate points from the evaluation at t are exactly the control points of the two halves of the curve:

  • Left half (parameters 0 to t): P0, Q0, R0, S
  • Right half (parameters t to 1): S, R1, Q2, P3

Both halves are cubic Beziers that trace the same curve as the original, just with different control points. This subdivision is how renderers draw curves and test them. Split until each piece is nearly straight, which is the case when its control points are close to the chord between its end points, then output straight lines. It adapts automatically: tight bends get many small pieces, gentle stretches only a few. It is also used for intersection tests and for finding the closest point on a curve.

JavaScript
function flat(c, tol) {                              // c = [p0, p1, p2, p3]
  const dev = (p) => Math.abs((c[3][0] - c[0][0]) * (c[0][1] - p[1]) - (c[0][0] - p[0]) * (c[3][1] - c[0][1]))
                     / (Math.hypot(c[3][0] - c[0][0], c[3][1] - c[0][1]) || 1);
  return Math.max(dev(c[1]), dev(c[2])) < tol;
}

Bezier curves in the Canvas 2D API

The browser already implements all of this. The Canvas path API has a cubic and a quadratic form:

JavaScript
ctx.moveTo(x0, y0);
ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);   // cubic: two control points then the end point
ctx.quadraticCurveTo(x1, y1, x2, y2);        // quadratic: one control point then the end point

The start point is the current path position. SVG paths use the same segments as the commands C and Q, and fonts store glyph outlines as sequences of quadratic (TrueType) or cubic (PostScript/OpenType CFF) Bezier segments.

The last demo compares everything. The thick blue curve is built from your own recursive subdivision, drawn as straight lines; the thin red dashed one is the browser's bezierCurveTo. They coincide. The slider sets the flatness tolerance, and the readout counts how many straight pieces were needed.

Adaptive subdivision versus bezierCurveTo

Strengths and limits

Bezier curves are intuitive, cheap to evaluate and invariant under affine transforms: you can rotate, scale or translate the control points and get the transformed curve (see transformations). Their weak spot is global control: moving any control point changes the whole curve a little, and a long curve needs a high degree. Practical drawing tools therefore chain many low-degree segments, which brings back the joint-continuity bookkeeping. A better answer to that problem is B-splines, where one set of control points automatically yields a smooth chain with local control.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which control points does a Bezier curve always pass through?

    At t = 0 only the first Bernstein weight is non-zero, and at t = 1 only the last. The inner points pull on the curve without lying on it.
  2. 2. What do the Bernstein polynomials of a given degree add up to at any t?

    They are non-negative and sum to 1, so each curve point is a weighted average of the control points. That puts the curve inside their convex hull.
  3. 3. How does de Casteljau's algorithm find the point P(t) of a cubic?

    Interpolating neighbors at t turns 4 points into 3, then 2, then 1. The final point lies on the curve.
  4. 4. Two cubic Bezier segments join at a point. Which condition guarantees G1 continuity?

    If the last handle of the first segment, the joint and the first handle of the second are collinear, the tangent directions agree. Equal handle lengths would add C1.
  5. 5. What can you do with the intermediate points of de Casteljau's algorithm?

    P0, Q0, R0, S and S, R1, Q2, P3 are the control points of the left and right halves, which is how curves are flattened and intersected.

Summary

  • A Bezier curve is a weighted blend of control points using Bernstein polynomials; it starts and ends at the end points and stays in the convex hull.
  • De Casteljau's algorithm evaluates it with repeated lerps, works for any degree, and its intermediate points split the curve into two Beziers.
  • A cubic matches Hermite data via T0 = 3(P1 - P0) and T1 = 3(P3 - P2); G1 joints need collinear handles, C1 equal-length handles.
  • Subdividing until the pieces are flat turns curves into line segments for drawing.
  • Canvas offers bezierCurveTo (cubic) and quadraticCurveTo; SVG and fonts use the same curves.
  • Moving one control point affects the whole curve, which motivates B-splines.

Next: B-Splines and NURBS.