Function Repository Resource:

BezierInterpolatingControlPoints

Source Notebook

Compute the control points of a Bézier curve that interpolates a given set of points

Contributed by: Jan Mangaldan

ResourceFunction["BezierInterpolatingControlPoints"][{x1,x2,},{f1,f2,}]

gives the Bernstein basis coefficients of the interpolating polynomial for the function values fi corresponding to x values xi.

ResourceFunction["BezierInterpolatingControlPoints"][{t1,t2,},{{x1,y1,},{x2,y2,},}]

generates the control points for a full-degree interpolating Bézier curve with interpolation nodes ti and points {xi,yi,}.

Examples

Basic Examples (3) 

A list of points:

In[1]:=
pts = {{0., 0.}, {0.2, 0.8}, {0.7, 0.1}, {1., 1.}};

Get the coefficients of the Bézier interpolant:

In[2]:=
cof = ResourceFunction["BezierInterpolatingControlPoints"][
  pts[[All, 1]], pts[[All, 2]]]
Out[2]=

Plot the Bézier interpolant along with the points:

In[3]:=
bFun = BezierFunction[cof];
Plot[bFun[x], {x, 0, 1}, Epilog -> {Directive[AbsolutePointSize[4], ColorData[97, 2]], Point[pts]}]
Out[3]=

Scope (3) 

A set of points to interpolate:

In[4]:=
pts = {{1., 2.}, {0., 1.}, {2., 0.}, {2., 2.}, {3., 3.}, {5., 2.}};

Generate the Bézier control points:

In[5]:=
cp = ResourceFunction["BezierInterpolatingControlPoints"][
  ResourceFunction["LeeInterpolatingNodes"][pts], pts]
Out[5]=

Show the Bézier curve along with the points:

In[6]:=
Graphics[{{Directive[AbsoluteThickness[2], ColorData[97, 1]], BezierCurve[cp, SplineDegree -> (Length[pts] - 1)]}, {Directive[
    AbsolutePointSize[6], ColorData[97, 4]], Point[pts]}}, Sequence[
 PlotRange -> {{0., 5.}, {0., 3.}}, PlotRangePadding -> Scaled[0.1]]]
Out[6]=

Applications (1) 

Use BezierInterpolatingControlPoints to generate an interpolating Bézier surface patch:

In[7]:=
xl = {0., 1./3, 0.8, 1.}; yl = {0., 0.2, 0.4, 0.75, 1.};
vals = Outer[Sin[\[Pi] #1 + Sin[3 \[Pi] #2/2]]/3 &, xl, yl];
cp = Transpose[
  ResourceFunction["BezierInterpolatingControlPoints"][yl, Transpose[
    ResourceFunction["BezierInterpolatingControlPoints"][xl, vals]]]]
Out[7]=
In[8]:=
bF = BezierFunction[Map[List, cp, {2}]];
Show[Plot3D[bF[x, y], {x, 0, 1}, {y, 0, 1}, Mesh -> None], Graphics3D[{Red, Sphere[Flatten[
     MapIndexed[{xl[[#2[[1]]]], yl[[#2[[2]]]], #} &, vals, {2}], 1], 0.015]}], BoxRatios -> Automatic]
Out[8]=

Properties and Relations (1) 

With inexact inputs, the result of BezierInterpolatingControlPoints is usually more accurate than using LinearSolve with BernsteinBasis:

In[9]:=
rExact = LinearSolve[
   Outer[BernsteinBasis[15, #2, #1] &, Range[16]/17, Range[0, 15]], yl];
xl = N[Range[16]/17]; yl = {2, 1, 2, 3, \[Minus]1, 0, 1, \[Minus]2, 4,
   1, 1, \[Minus]3, 0, \[Minus]1, \[Minus]1, 2};
r1 = BezierInterpolatingCoefficients[xl, yl]
Out[9]=
In[10]:=
Norm[r1 - rExact, \[Infinity]]
Out[10]=
In[11]:=
r2 = LinearSolve[
  Transpose[Outer[BernsteinBasis[15, #1, #2] &, Range[0, 15], xl]], yl]
Out[11]=
In[12]:=
Norm[r2 - rExact, \[Infinity]]
Out[12]=

Version History

  • 1.0.0 – 05 April 2021

Source Metadata

Related Resources

License Information