Function Repository Resource:

CubicSplineCurve

Source Notebook

Generate a cubic B-spline curve that passes through given points

Contributed by: Jan Mangaldan

ResourceFunction["CubicSplineCurve"][{{x1,y1,},{x2,y2,},}]

constructs a cubic interpolating BSplineCurve that passes through the given set of points.

Details and Options

ResourceFunction["CubicSplineCurve"] returns a BSplineCurve object.
With the setting SplineClosedTrue, ResourceFunction["CubicSplineCurve"] returns a curve that is closed.

Examples

Basic Examples (2) 

A list of points:

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

Show the points along with an interpolating cubic B-spline:

In[2]:=
Graphics[{{Directive[AbsoluteThickness[3], ColorData[97, 1]], ResourceFunction["CubicSplineCurve"][pts]}, {Directive[
    AbsolutePointSize[6], ColorData[97, 4]], Point[pts]}}]
Out[2]=

Scope (2) 

Choose six points in the plane to be interpolated:

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

Use CubicSplineCurve with Arrow:

In[4]:=
Graphics[{{Directive[ColorData[97, 5], Arrowheads[Medium]], Arrow[ResourceFunction["CubicSplineCurve"][pts]]}, {Directive[
    ColorData[97, 3], AbsolutePointSize[4]], Point[pts]}}]
Out[4]=

Choose 3D points to be interpolated:

In[5]:=
pts = Table[N[{Cos[t], Sin[t], t/4}], {t, 0, 4 Pi, 2 Pi/10}];

Show the points along with an interpolating cubic B-spline:

In[6]:=
Graphics3D[{{Directive[AbsoluteThickness[3], ColorData[97, 1]], ResourceFunction["CubicSplineCurve"][pts]}, {Directive[
    AbsolutePointSize[6], ColorData[97, 4]], Point[pts]}}]
Out[6]=

Show the interpolating cubic B-spline as a Tube:

In[7]:=
Graphics3D[{{Directive[AbsoluteThickness[3], ColorData[97, 1]], Tube[ResourceFunction["CubicSplineCurve"][pts], 0.03]}, {Directive[
    AbsolutePointSize[6], ColorData[97, 4]], Sphere[pts, 0.05]}}]
Out[7]=

Options (1) 

SplineClosed (1) 

Use SplineClosedTrue to interpolate a set of points with a closed B-spline:

In[8]:=
pts = With[{n = 12}, Table[N[AngleVector[{1 + (-1)^i/4, 2 \[Pi] i/n}]], {i, 0, n}]];
Graphics[{{Directive[AbsoluteThickness[3], ColorData[97, 1]], ResourceFunction["CubicSplineCurve"][pts, SplineClosed -> True]}, {Directive[AbsolutePointSize[6], ColorData[97, 4]], Point[pts]}}]
Out[8]=

Applications (2) 

Convert the result of KnotData into a BSplineCurve:

In[9]:=
pts = Transpose[
    KnotData["FigureEight", "SpaceCurve"]["ValuesOnGrid"]] . DiagonalMatrix[{1, 1, 1/3}];
knot = ResourceFunction["CubicSplineCurve"][pts, SplineClosed -> True];
Graphics3D[{Directive[AbsoluteThickness[2], ColorData[97, 3]], knot}, Boxed -> False]
Out[9]=

Visualize the knot as a Tube:

In[10]:=
Graphics3D[{ColorData[97, 3], Tube[knot, 0.3]}, Boxed -> False, Lighting -> "Accent"]
Out[10]=

Version History

  • 1.0.0 – 03 August 2021

Source Metadata

Related Resources

License Information