Bezier Curves in Mathematica

Sergey Fomel   03/05/2002

Bernstein Polynomials

The Bezier curves of order n are defined with the help of Bernstein';s polynomials

bernstein[n_, k_, t_] := 
      Binomial[n, k] t^k (1 - t)^(n - k)

plotbern[n_, k_] := 
      Plot[bernstein[n, k, t], {t, 0, 1}, 
        PlotStyle -> {Thickness[0.01]}, Frame -> True, 
        DisplayFunction -> Identity]

Bernstein polynomials for order 4:

Show[Table[plotbern[4, k], {k, 0, 4}], 
        DisplayFunction -> $DisplayFunction];

Bernstein polynomials for order 7:

Show[Table[plotbern[7, k], {k, 0, 7}], 
        DisplayFunction -> $DisplayFunction];

The Bernstein polynomials are non-negative and sum up to one in the interval from 0 to 1.

Bezier Curves and Control Points

The definition of Bezier Curve:

bezier[pts : {{_, _} .. }, t_] := 
      Module[{n = Length[pts] - 1}, 
Simplify[
          Table[bernstein[n, k, t], {k, 0, n}]  .  pts]]

dsk[t_] := Disk[t, 0.04]

plotbez[pts : {{_, _} .. }, x : {_, _}, 
        y : {_, _}] := 
Module[{n = Length[pts]}, 
        ParametricPlot[bezier[pts, t], {t, 0, 1}, Axes -> False, 
          AspectRatio -> Automatic, 
          Epilog -> 
            Flatten[{Map[dsk, 
                  pts], {{Dashing[{0.01}], 
                    Line[{pts[[1]], 
                        pts[[2]]}]}, 
{Dashing[{0.01}], 
                    Line[{pts[[n]], pts[[n - 1]]}]}}}, 1], 
          PlotRange -> {x, y}]]

plotbez[{{1, 1}, {2, 4}, {4, 3}, {3, 0}, {5, -1}, {6, 0}}, {0.9, 
          6.1}, {-1.1, 5.1}];

The dashed lines connect the first point with the second one and the last point with the one before the last one.

plotbez[{{1, 0}, {0, 1}, {4, 2}, {3, -1}}, {-0.1, 
          4.1}, {-1.1, 2.1}];

plotbez[{{1, 0}, {2, 1}, {3, 2}, {4, -1}}, {0.9, 4.1}, {-1.1, 
          2.1}];

plotbez[{{1, 0}, {5, 0}, {2, 2}, {3, -1}}, {0.9, 5.1}, {-1.1, 
          2.1}];