The SVG <path> : Bezier Curves

The SVG <path> element can draw any shape you want — but only if you know how to tell it what to do. Basic straight-line paths are easy to get the hang of. Arcs take some getting used to , but at least use the familiar shapes of circles and ellipses. Bezier curves — named after the French mathematician who developed the standard notation — are polynomial curves, but not in the standard f(x) notation from high school algebra. They are defined as weighted averages between the vertices (start and end points of the curve segment) and one or more control points.

Both curves connect the three points (0,50) (50,50) and (100,50), as shown by the red marker elements, but have different control points, as indicated by the straight lines and smaller markers. The navy curve uses quadratic Bezier curves, with one control point per curve segment. The purple curve uses cubic Bezier curves, with two control points per segment .

It gets more complicated: for both paths, the second half of the path is defined using the shorthand notation for a smooth curve. This skips the definition of the first control point, which the computer calculates as a mirror reflection of the control point from the previous curve segment. So a shorthand quadratic curve doesn't include any control points in the command statement, only the end-point coordinates.

But how is the curve determined from the control points? As you can see from the diagrams, the control points, and the lines that connect them, extend well beyond the shape of the curves.

Let's start with the simpler case: quadratic Bezier curves. These have one control point defining the shape of the curve between each pair of vertex points. The small diagram shows a single quadratic curve. The control point is connected to the start and end vertex points with dashed lines. The mid-points of those lines are connected by the dotted line. And the mid-point of the dotted line is also the mid-point of the curve.

The same relationship can be created for any point on the curve: the point 10% along the curve is 10% of the way from the 10% point of the first line to the 10% point of the second line.

Bezier curves are defined this way because it makes them easy to calculate with basic arithmetic — the curve may have the same shape as a quadratic polynomial, but there are no x 2 terms to calculate. Instead, the curve's positions are calculated as weighted averages. The mid-point of a line between (x1,y1) and (x2,y2) is the point

(average(x1,x2), average(y1,y2))
More generally, the point that is p% of the way between them is at
(( x1*(100%-p%) + x2*(p%) ), ( y1*(100%-p%) + y2*(p%) ) )
That sort of calculation is used by the computer to plot the Bezier curve, except the computer uses powers of two instead of percentages for fast binary math. The curve is divided in half and in half again until enough points have been plotted to reach the resolution of the display. For example, for the first curve segment of the navy line in the diagram, this is how the curve is positioned when broken down into eighths:
Calculation of positions on a quadratic Bezier curve between (0,50) and (50,50) , with a control point at (70,20) .
Position Start of line End of line Curve Position
(1-p)*(0,50) + p*(70,20) (1-p)*(70,20) + p*(50,50) (1-p)*Start + p*End
0/8 = start (0.00,50.00) (70.00,20.00) (0.00,50.00)
1/8 (8.75,46.25) (67.50,23.75) (16.09,43.44)
2/8 = 1/4 (17.50,42.50) (65.00,27.50) (29.38,38.75)
3/8 (26.25,38.75) (62.50,31.25) (39.84,35.94)
4/8 = 1/2 (35.00,35.00) (60.00,35.00) (47.50,35.00)
5/8 (43.75,31.25) (57.50,38.75) (52.34,35.94)
6/8 = 3/4 (52.50,27.50) (55.00,42.50) (54.38,38.75)
7/8 (61.25,23.75) (52.50,46.25) (53.59,43.44)
8/8 = end (70.00,20.00) (50.00,50.00) (50.00,50.00)

That's a lot of numbers for just the first rough approximation of a curve. Clearly, you'd never want to plot a Bezier curve by calculating every position yourself. How does knowing the math help you figure out how to draw with Bezier curves? There are a few key ideas to use:

Discussion of cubic curves & Animated diagrams still under construction...