B-Spline

Author

Eric W. Weisstein
December 17, 2003

This notebook downloaded from http://mathworld.wolfram.com/notebooks/NumericalMath/B-Spline.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/B-Spline.html.

©2005 Wolfram Research, Inc. except for portions noted otherwise

Code

BSplineKnots[p_List,knot_List]:=Module[{i,n=Length[p]-1,m=Length[knot]-1,t},f[t_]:=Evaluate[Sum[p[[i+1]] Nf[i,m-n-1,knot,t],{i,0,n}]];
Join[Drop[Table[f[knot[[i]]],{i,1,m+1}],-1],{p[[-1]]}] 
(*Last point erroneously given as 0*)
]

BSpline[p_List,knot_List]:=Module[{i,n=Length[p]-1,m=Length[knot]-1,t,curve},curve=ParametricPlot[Evaluate[Sum[p[[i+1]] Nf[i,m-n-1,knot,t],{i,0,n}]],{t,0,1},DisplayFunction->Identity,Axes->False][[1]];
Line[Join[Drop[curve[[1,1,1]],-1],{p[[-1]]}]] 
 (*Last point erroneously given as 0*)
]

ND[a_,0]:=0
ND[a_,b_]:=a/b
Nf[i_,0,ti_List,t_]:=If[ti[[i+1]]≤t&&t<ti[[i+2]]&&ti[[i+1]]<ti[[i+2]],1,0]
Nf[i_,p2_,ti_List,t_]:=ND[t-ti[[i+1]],ti[[i+p2+1]]-ti[[i+1]]] Nf[i,p2-1,ti,t]+ND[ti[[i+p2+2]]-t,ti[[i+p2+2]]-ti[[i+2]]] Nf[i+1,p2-1,ti,t]

OpenCircles[p_List,r_:.02]:=Circle[#,r]&/@p
ControlPoints[p_List,r_:.02]:=Disk[#,r]&/@p

Graphic

p={{0,0},{-.5,-1},{.4,-1.5},{2,-1},{3.6,-1.5},{4.5,-1},{4,0}};
k={0,0,0,0,1/4,1/2,3/4,1,1,1,1};
Length/@{p,k}

Show[Graphics[{
BSpline[p,k],
OpenCircles[BSplineKnots[p,k],.1],ControlPoints[p,.05]
}],AspectRatio->Automatic]