De Casteljau algorithm
CSE 477, G. Farin

The next cell evaluates the de Casteljau algorithm:

	(Subscript[b, i]^r)(t) = (1-t)(Subscript[b, i]^(r-1))(t) + t(Subscript[b, i+1]^(r-1))(t)

For r=n, (Subscript[b, 0]^n)(t) is the point corresponding to parameter value t on the Bezier curve defined by (Subscript[b, 0]), ..., (Subscript[b, n]).
All intermediate values are best conceptualized as a triangular array, here for n=3:

	(Subscript[b, 0]^0)
	(Subscript[b, 1]^0)    (Subscript[b, 0]^1)
	(Subscript[b, 2]^0)    (Subscript[b, 1]^1)    (Subscript[b, 0]^2)
	(Subscript[b, 3]^0)    (Subscript[b, 2]^1)    (Subscript[b, 1]^2)    (Subscript[b, 0]^3)

dca[b_,r_,i_,t_]:=If [r==0,b[[i+1]],(1-t)*dca[b,r-1,i,t]+t*dca[b,r-1,i+1,t]];

bb={
{{0,0,0},{1,0,0}}, (* linear *)
{{0,0,0},{1,0,0},{2,0,2}}, (* quadratic *)
{{1,0,0},{0,1,0},{1,1,1},{0,0,0}}, (* cubic *)
{{0,0,0},{0,1,0},{0.5,1,0},{2,0,0}}, (* cubic *)
{{0,0,0},{1,0,0},{1,0,1},{1,1,1},{1,1,0},{0,1,0}}, (* quintic *)
Table[{Cos[i],Sin[i],i/(3Pi)},{i,1,11}], (* degree 10 *)
{{-3,0,0},{6,3,0},{0,6,6},{-6,3,0},{3,0,0}}  (* quartic *)

};
b=bb[[7]];
n=Length[b]-1;

(* To Do: calculate these based on mmbox of input data *)
xmin = 0; xmax = 1;
ymin = 0; ymax = 1;
zmin = 0; zmax = 1;
 

curve=ParametricPlot3D[dca[b,n,0,t],{t,0,1},PlotStyle->{Thick}]; (* try different t-limits *)
poly=Graphics3D[{Thick,Green,Line[b]}];

points=Graphics3D[{PointSize[Large],Point[b]}];
begin= Graphics3D[{PointSize[Large],Red,Point[b[[1]]]}];
Show[{poly,points,curve,begin},Boxed->False]

Dynamic de Casteljau

polyr[b_,r_,t_]:=Table[dca[b,r,i,t],{i,0,n-r}];
nested[b_,t_]:=Graphics3D[{Line[Table[polyr[b,r,t] ,{r,0,n}]]}];
curvept[t_]:=Graphics3D[{PointSize[Large],Red,Point[dca[b,n,0,t]]}];

Manipulate[
nested[b,t];
curvept[t];
Show[{poly,curve,points,nested[b,t],curvept[t]},Boxed->False,Axes->False,PlotRange->{{xmin, xmax},{ymin,ymax},{zmin, zmax}}],
{t,0,1}]

Nested Polygons

This plots all intermediate line segments arising in the de Casteljau algorithm for several values of t.

polyr[b_,r_,t_]:=Table[dca[b,r,i,t],{i,0,n-r}];
nested[b_,t_]:=Graphics3D[Line[Table[polyr[b,r,t] ,{r,0,n}]]];
tree=Table[nested[b,t],{t,0,1,0.02}]; (* this defines how many t's to use *)

Show[{tree,curve,points,poly},Boxed->False]

cpoints=Graphics3D[{PointSize[Large],Point[Table[dca[b,n,0,t],{t,0,1,0.02}]]}];
Show[{poly,points,cpoints},Boxed->False]