Repository navigation
De Casteljau Algorithm
De Casteljau's algorithm finds the point BezierView uses it to draw the curve and the colored construction lines you see in the animation.
You could also evaluate the curve from its Bernstein polynomial form, which needs powers of
Given two points
lies on segment
A Bézier curve of order
- Split every segment of the control polygon at ratio
$t$ . This gives$n$ new points; connect them to get a polygon with$n - 1$ segments. - Split every segment of that new polygon at the same ratio
$t$ . This gives$n - 1$ points. - Keep going. Each round has one point fewer than the previous one. After
$n$ rounds a single point is left, and that point is$C(t)$ .
The figure shows a cubic curve (
Here are the numbers. Write
| Round |
Points |
|---|---|
| 0 |
|
| 1 |
|
| 2 |
|
| 3 |
So
Every round uses only the points from the round before. In formula form:
Laid out row by row, the points form an upside-down triangle. Each point is computed from the two points directly above it, with weight
The triangle maps directly to code. Copy the control points into a working array. In round
fun deCasteljau(points: List<PointF>, t: Float): PointF {
// Work on a copy so the caller's control points stay unchanged
val q = points.map { PointF(it.x, it.y) }
val n = points.size - 1
for (k in 1..n) {
for (i in 0..n - k) {
q[i].x = (1 - t) * q[i].x + t * q[i + 1].x
q[i].y = (1 - t) * q[i].y + t * q[i + 1].y
}
}
return q[0]
}An order-
The recurrence above looks like it was made for a recursive function: to get
The call tree for a cubic shows the problem. The red nodes are values that are computed more than once.
The tree has 15 calls, but there are only 10 distinct values. In general the recursion makes
Pick any run of consecutive points in one row of the triangle, say
In the figure (
A few useful facts follow from this:
-
Every row leads to the same answer. The whole of row
$k$ is the control polygon of a curve of order$n - k$ , and its tip is the same$P_0^{(n)}$ . So$C(t)$ can be computed from any row, not only from the original control points. -
The last segment is the tangent. Row
$n - 1$ has two points, and$C(t)$ lies on the segment between them. That segment is tangent to the curve at$C(t)$ , and the derivative is$C'(t) = n\,(P_1^{(n-1)} - P_0^{(n-1)})$ . -
The edges split the curve. The left edge of the triangle,
$P_0^{(0)}, P_0^{(1)}, \dots, P_0^{(n)}$ , holds the control points of the part of the curve from$t = 0$ to the current$t$ . The right edge,$P_n^{(0)}, P_{n-1}^{(1)}, \dots, P_0^{(n)}$ , holds the control points of the remaining part. This is how a Bézier curve is cut in two.
BezierView draws all of the rounds while the animation plays. The gray lines are the control polygon, each colored layer is one round of interpolation, the black dot is

In the source (BezierView.kt):
-
buildBezierPoints()samples the curve at about 1,000 values of$t$ when the animation starts. It uses the recursive form (deCasteljauX/deCasteljauY). For order 7 that is 127 calls per coordinate per sample, which is cheap enough to do once per playback. -
buildTangentPoints()builds the intermediate rounds one after another. Each round reuses the round before it, the way the triangle does. These are the colored lines in the animation.
- Finding a Point on a Bézier Curve: De Casteljau's Algorithm, course notes for CS3621 Introduction to Computing with Geometry, Michigan Technological University
- De Casteljau's algorithm on Wikipedia