Bezier curves are used in computer graphics to draw shapes, for CSS animation and in many other places.
They are actually a very simple thing, worth to study once and then feel comfortable in the world of vector graphics and advanced animations.
A bezier curve is defined by control points.
There may be 2, 3, 4 or more.
For instance, two points curve:
Three points curve:
Four points curve:
If you look closely at these curves, you can immediately notice:
Points are not always on curve. That’s perfectly normal, later we’ll see how the curve is built.
The curve order equals the number of points minus one. For two points we have a linear curve (that’s a straight line), for three points – quadratic curve (parabolic), for three points – cubic curve.
A curve is always inside the convex hull of control points:
Because of that last property, in computer graphics it’s possible to optimize intersection tests. If convex hulls do not intersect, then curves do not either. So checking for the convex hulls intersection first can give a very fast “no intersection” result. Checking the intersection or convex hulls is much easier, because they are rectangles, triangles and so on (see the picture above), much simpler figures than the curve.
The main value of Bezier curves for drawing – by moving the points the curve is changing in intuitively obvious way.
Try to move control points using a mouse in the example below:
As you can notice, the curve stretches along the tangential lines 1 → 2 and 3 → 4.
After some practice it becomes obvious how to place points to get the needed curve. And by connecting several curves we can get practically anything.
Here are some examples:
A Bezier curve can be described using a mathematical formula.
As we’ll see soon – there’s no need to know it. But for completeness – here it is.
Given the coordinates of control points
Pi: the first control point has coordinates
P1 = (x1, y1), the second:
P2 = (x2, y2), and so on, the curve coordinates are described by the equation that depends on the parameter
t from the segment
The formula for a 2-points curve:
P = (1-t)P1 + tP2
For three points:
P = (1−t)2P1 + 2(1−t)tP2 + t2P3
For four points:
P = (1−t)3P1 + 3(1−t)2tP2 +3(1−t)t2P3 + t3P4
These are vector equations.
We can rewrite them coordinate-by-coordinate, for instance the 3-point curve:
x = (1−t)2x1 + 2(1−t)tx2 + t2x3
y = (1−t)2y1 + 2(1−t)ty2 + t2y3
x1, y1, x2, y2, x3, y3 we should put coordinates of 3 control points.
For instance, if control points are
(0.5, 1) and
(1, 0), the equations are:
x = (1−t)2 * 0 + 2(1−t)t * 0.5 + t2 * 1 = (1-t)t + t2 = 1
y = (1−t)2 * 0 + 2(1−t)t * 1 + t2 * 0 = 2(1-t)t = –t2 + 2t
t runs from
1, the set of values
(x,y) for each
t forms the curve.
That’s probably too scientific, not very obvious why curves look like that, and how they depend on control points.
So here’s the drawing algorithm that may be easier to understand.
De Casteljau’s algorithm is identical to the mathematical definition of the curve, but visually shows how it is built.
Let’s see it on the 3-points example.
Here’s the demo, and the explanation follow.
Points can be moved by the mouse. Press the “play” button to run it.
De Casteljau’s algorithm of building the 3-point bezier curve:
Draw control points. In the demo above they are labeled:
Build segments between control points 1 → 2 → 3. In the demo above they are brown.
1. In the example above the step
0.05is used: the loop goes over
0, 0.05, 0.1, 0.15, ... 0.95, 1.
For each of these values of
On each brown segment we take a point located on the distance proportional to
tfrom its beginning. As there are two segments, we have two points.
For instance, for
t=0– both points will be at the beginning of segments, and for
t=0.25– on the 25% of segment length from the beginning, for
t=0.5– 50%(the middle), for
t=1– in the end of segments.
Connect the points. On the picture below the connecting segment is painted blue.
Now the blue segment take a point on the distance proportional to the same value of
t. That is, for
t=0.25(the left picture) we have a point at the end of the left quarter of the segment, and for
t=0.5(the right picture) – in the middle of the segment. On pictures above that point is red.
1, every value of
tadds a point to the curve. The set of such points forms the Bezier curve. It’s red and parabolic on the pictures above.
That was a process for 3 points. But the same is for 4 points.
The demo for 4 points (points can be moved by mouse):
- Control points are connected by segments: 1 → 2, 2 → 3, 3 → 4. We have 3 brown segments.
- For each
tin the interval from
- We take points on these segments on the distance proportional to
tfrom the beginning. These points are connected, so that we have two green segments.
- On these segments we take points proportional to
t. We get one blue segment.
- On the blue segment we take a point proportional to
t. On the example above it’s red.
- We take points on these segments on the distance proportional to
- These points together form the curve.
The algorithm is recursive and can be generalized for any number of control points.
Given N of control points, we connect them to get initially N-1 segments.
Then for each
- Take a point on each of segment on the distance proportional to
tand connect them – there will be N-2 segments.
- Take a point on each of these segments on the distance proportional to
tand connect – there will be N-3 segments, and so on…
- Till we have one point. These points make the curve.
Move examples of curves:
With other points:
Not smooth Bezier curve:
As the algorithm is recursive, we can build Bezier curves of any order: using 5, 6 or more control points. But in practice they are less useful. Usually we take 2-3 points, and for complex lines glue several curves together. That’s simpler to develop and calculate.
We use control points for a Bezier curve. As we can see, they are not on the curve. Or, to be precise, the first and the last ones do belong to curve, but others don’t.
Sometimes we have another task: to draw a curve through several points, so that all of them are on a single smooth curve. That task is called interpolation, and here we don’t cover it.
There are mathematical formulas for such curves, for instance Lagrange polynomial.
In computer graphics spline interpolation is often used to build smooth curves that connect many points.
Bezier curves are defined by their control points.
We saw two definitions of Bezier curves:
- Using a mathematical formulas.
- Using a drawing process: De Casteljau’s algorithm
Good properties of Bezier curves:
- We can draw smooth lines with a mouse by moving around control points.
- Complex shapes can be made of several Bezier curves.
- In computer graphics, modeling, vector graphic editors. Fonts are described by Bezier curves.
- In web development – for graphics on Canvas and in the SVG format. By the way, “live” examples above are written in SVG. They are actually a single SVG document that is given different points as parameters. You can open it in a separate window and see the source: demo.svg.
- In CSS animation to describe the path and speed of animation.