Parametric and Bézier Curves
A parametric curve gives and (and maybe ) as separate functions of one parameter , often time. That lets a curve loop back on itself, go vertical or trace the same path at different speeds. Bézier curves, the smooth paths in every drawing tool, font and animation editor, are parametric curves built from repeated linear interpolation.
Curves as vector functions
Instead of , write the position as a vector function of a parameter:
| Curve | Parametric form | Notes |
|---|---|---|
| Line through in direction | ||
| Circle, radius | , | can't be a single |
| Ellipse | ||
| Helix (3D) | a spring | |
| Graph of | every ordinary graph is a parametric curve |
Sometimes you can eliminate the parameter. For , , use to get , an ellipse.
Derivatives: tangents and slopes
Differentiate each component separately. The result is the tangent vector: it points along the curve in the direction of increasing , and its length is the speed at which the point moves. The slope of the curve follows from the chain rule:
Where but the tangent is vertical, something a function can never have.
Arc length
A short step moves the point a distance , so the length of the curve is
The integral rarely has a closed form, so in practice it is computed numerically, or by summing many short straight segments, which is what a game does to measure a path.
import sympy as sp
t = sp.symbols('t', positive=True)
x, y = t**2, t**3 / 3
dx, dy = sp.diff(x, t), sp.diff(y, t)
print("r'(t) =", (dx, dy))
print("dy/dx =", sp.simplify(dy / dx))
print("slope at t = 2:", (dy / dx).subs(t, 2))
speed = sp.sqrt(dx**2 + dy**2)
L = sp.integrate(sp.simplify(speed), (t, 0, 3))
print("length for 0 <= t <= 3:", sp.simplify(L), "≈", round(float(L), 4))r'(t) = (2*t, t**2)
dy/dx = t/2
slope at t = 2: 1
length for 0 <= t <= 3: -8/3 + 13*sqrt(13)/3 ≈ 12.9574
For the speed is , which has the antiderivative (substitute ). From 0 to 3 that gives , matching SymPy. (SymPy prints powers as **, so t**2 means .)
Bézier curves
A Bézier curve is defined by control points. The curve starts at the first point, ends at the last, and is pulled toward the middle ones. A cubic Bézier with points is
The four weights are the Bernstein polynomials. They are never negative and always sum to 1, so every point of the curve is a weighted average of the control points, which is why the curve stays inside their convex hull.
De Casteljau's algorithm evaluates the same curve with nothing but linear interpolation: lerp each neighbouring pair of points by , then lerp the results, until one point is left. It is numerically stable and is how most libraries evaluate and split Béziers.
import numpy as np
import matplotlib.pyplot as plt
P = np.array([[0.0, 0.0], [1.0, 3.0], [4.0, 3.0], [5.0, 0.0]])
def de_casteljau(points, t):
pts = points.copy()
while len(pts) > 1:
pts = (1 - t) * pts[:-1] + t * pts[1:] # lerp neighbours
return pts[0]
def bernstein(P, t):
return (1-t)**3 * P[0] + 3*(1-t)**2*t * P[1] + 3*(1-t)*t**2 * P[2] + t**3 * P[3]
print("t=0.5 de Casteljau:", de_casteljau(P, 0.5), " Bernstein:", bernstein(P, 0.5))
ts = np.linspace(0, 1, 101)
curve = np.array([de_casteljau(P, t) for t in ts])
plt.figure(figsize=(6, 4))
plt.plot(P[:, 0], P[:, 1], "o--", color="gray", label="control polygon")
plt.plot(curve[:, 0], curve[:, 1], lw=2, label="cubic Bézier")
plt.axis("equal"); plt.legend(); plt.title("A cubic Bézier curve")t=0.5 de Casteljau: [2.5 2.25] Bernstein: [2.5 2.25]
The derivative of a Bézier curve
Differentiating gives another Bézier curve, one degree lower, built from the differences of the control points:
So and : the curve leaves heading toward and arrives at coming from . To join two Béziers smoothly, make the last handle of one and the first handle of the next collinear, with equal lengths if you also want the speed to match ( continuity).
import numpy as np
P = np.array([[0.0, 0.0], [1.0, 3.0], [4.0, 3.0], [5.0, 0.0]])
def B(t):
return (1-t)**3*P[0] + 3*(1-t)**2*t*P[1] + 3*(1-t)*t**2*P[2] + t**3*P[3]
def dB(t):
return 3*((1-t)**2*(P[1]-P[0]) + 2*(1-t)*t*(P[2]-P[1]) + t**2*(P[3]-P[2]))
h = 1e-6
for t in [0.0, 0.3, 1.0]:
numeric = (B(min(t + h, 1)) - B(max(t - h, 0))) / (min(t + h, 1) - max(t - h, 0))
print(f"t={t}: formula {dB(t)}, numeric {np.round(numeric, 4)}")
# arc length by summing short chords
ts = np.linspace(0, 1, 2001)
pts = np.array([B(t) for t in ts])
print("length ≈", round(float(np.sum(np.linalg.norm(np.diff(pts, axis=0), axis=1))), 4))t=0.0: formula [3. 9.], numeric [3. 9.]
t=0.3: formula [5.52 3.6 ], numeric [5.52 3.6 ]
t=1.0: formula [ 3. -9.], numeric [ 3. -9.]
length ≈ 7.1906
In software
- Vector graphics and fonts. SVG paths, PDF, PostScript and TrueType/OpenType outlines are chains of quadratic and cubic Béziers.
- UI animation. CSS
transition-timing-function: cubic-bezier(x1, y1, x2, y2)is a cubic Bézier from (0, 0) to (1, 1); the browser solves for and returns as progress. See Easing, Interpolation and Smooth Curves. - Games. Camera rails, roads, projectile previews and enemy paths. Moving at constant speed along a Bézier needs an arc-length table, because equal steps in are not equal steps in distance. See Bézier Curves and Catmull-Rom Splines.
Exercises
Exercise 1: Eliminate the parameter
Find an equation in and for , , and name the curve.
Answer
, a parabola with vertex .
Solution
From we get . Substitute: .
Exercise 2: Slope and vertical tangents
For , , find at and the points where the tangent is vertical.
Answer
at . Vertical tangents at , the points and .
Solution
Exercise 3: Length of a circle arc
Use the arc length formula to find the length of for .
Answer
.
Solution
That is a quarter of the circumference , as it should be.
Exercise 4: Quadratic Bézier by hand and in code
For the quadratic Bézier with , , , compute using de Casteljau by hand, then check it with code.
Answer
.
Solution
Note that the curve's peak, , is only half the control point's height of 4.
import numpy as np
pts = np.array([[0.0, 0.0], [2.0, 4.0], [4.0, 0.0]])
t = 0.5
while len(pts) > 1:
pts = (1 - t) * pts[:-1] + t * pts[1:]
print(pts[0])[2. 2.]
Test yourself
For a parametric curve, how is computed?
Which statement about a cubic Bézier curve is true?
- It passes through all four control points
- Its tangent at the start points from toward
- Its weights can be negative
- It is a degree-4 polynomial
What does de Casteljau's algorithm use to evaluate a Bézier curve?
- Repeated linear interpolation between neighbouring points
- Newton's method
- Numerical integration
- Matrix inversion