Bézier Curve

Overview

Bézier curves are parametric curves defined by polynomial per-coordinate functions. They are constructed using basis functions and can be of various degrees, such as linear, quadratic, or cubic. These curves are widely used in computer graphics and design due to their intuitive control through control points. The degree of the Bézier curve determines the number of control points and the complexity of the curve.

Types of Bézier Curves

Linear Bézier Curve

The simplest form of a Bézier curve is the linear Bézier curve, which is a straight line between two control points. It is defined by a first-degree polynomial function.

Figure 7.14 The first-degree Bézier function is linear and interpolating. Figure 7.14 The first-degree Bézier function is linear and interpolating.

Quadratic Bézier Curve

A quadratic Bézier curve is defined by three control points and a second-degree polynomial function. The curve starts and ends at the first and last control points, while the middle point influences the shape of the curve.

Listing 7.8 Quadratic Bézier curve

xs = [0, 0.3, 1]                                             #1
ys = [0.2, 0.8, 0.5]                                         #1
 
def b2x(t, xs):                                              #2
    return (1-t)*(1-t)*xs[0] + 2*(1-t)*t*xs[1] + t*t*xs[2]   #2
 
def b2y(t, ys):                                              #3
    return b2x(t, ys)                                        #3
#1 Points
#2 Bézier function
#3 Because both functions share a basis, there’s no point in copying and pasting the formula.

Figure 7.15 The second-degree or quadratic Bézier curve starts and ends in control points, but the middle point only shapes the curve. Figure 7.15 The second-degree or quadratic Bézier curve starts and ends in control points, but the middle point only shapes the curve.

Cubic Bézier Curve

A cubic Bézier curve is defined by four control points and a third-degree polynomial function. It provides more flexibility and control over the curve's shape compared to linear and quadratic Bézier curves.

Listing 7.9 An example of a cubic Bézier curve

xs = [0, 0.3, 0.7, 1]                  #1
ys = [0.2, 0.8, 1., 0.5]               #1
 
def b3x(t, xs):                        #2
    return ((1-t)*(1-t)*(1-t)*xs[0]    #3
           + 3*(1-t)*(1-t)*t*xs[1]
           + 3*(1-t)*t*t*xs[2]
           + t*t*t*xs[3])
 
def b3y(t, ys):
    return b3x(t, ys)
#1 Points
#2 The cubic Bézier formula
#3 Python famously restricts the indentation, but in an expression, it allows you everything as long as you keep it in parentheses.

Figure 7.16 The third-degree or cubic Bézier curve has two points to control the curve’s shape. Figure 7.16 The third-degree or cubic Bézier curve has two points to control the curve’s shape.

Rational Bézier Curve

A rational Bézier curve introduces weights to the control points, providing additional control over the curve's shape. This type of curve is particularly useful in computer-aided design (CAD) applications where precision is crucial.

Listing 7.10 Rational Bézier curve example

xs = [0, 0.3, 0.7, 1]                    #1
ys = [0.2, 0.8, 1., 0.5]                 #1
ws = [1., 2., 2., 1.]                    #1
 
def b3x(t, xs):                          #2
    n = ((1-t)*(1-t)*(1-t)*xs[0]*ws[0]   #2
        + 3*(1-t)*(1-t)*t*xs[1]*ws[1]    #2
        + 3*(1-t)*t*t*xs[2]*ws[2]        #2
        + t*t*t*xs[3]*ws[3])             #2
    d = ((1-t)*(1-t)*(1-t)*ws[0]         #2
        + 3*(1-t)*(1-t)*t*ws[1]          #2
        + 3*(1-t)*t*t*ws[2]              #2
        + t*t*t*ws[3])                   #2
    return n / d                         #2
def b3y(t, ys):
    return b3x(t, ys)
#1 Points and weights
#2 Rational Bezier function

Figure 7.18 Cubic Bézier curves with the same control points with different weights, from left to right and top to bottom: (1, 1, 1, 1), (1, 2, 2, 1), (1, 0, 0, 1), (1, 1, 4, 1) Figure 7.18 Cubic Bézier curves with the same control points with different weights, from left to right and top to bottom: (1, 1, 1, 1), (1, 2, 2, 1), (1, 0, 0, 1), (1, 1, 4, 1)

Rational Bézier curves can describe conic sections such as circles, ellipses, and hyperbolas. However, they are more computationally intensive due to the additional multiplications and division involved.