bezcurve.txt  - commands to compute bezier curves given matrix of control pts

with(plots): with(plottools): with(linalg):

printf( "\nCommands in bezcurve.txt");
printf("\n  BezCurve(matrix pts) - computes Bezier curve from matrix of control points");
printf("\n  PolyLine(matrix pts) - draws control polyline in grey from matrix of control points");
printf("\n  BezPlot(curve,color) - plots Bezier curve in color without axes");
 
# bezcurv - computes bezier curve given matrix of control pts
BezCurve:=proc(pts)
  local d, i, m, xp, yp, x, y, ans;
   m:=rowdim(pts):
   d:=m-1:
   for i from 0 to d do
     xp[i]:=pts[i+1,1]:
     yp[i]:=pts[i+1,2]:
   od:
   unassign('i'):
   x:=evalm(sum(binomial(d,i)*s^i*(1-s)^(d-i)*xp[i],i=0..d)):
   y:=evalm(sum(binomial(d,i)*s^i*(1-s)^(d-i)*yp[i],i=0..d)):
   ans:=vector([x,y]):
   RETURN (evalm(ans));
  end:
  
# plots control polygon of bezier curve given matrix of control pts
PolyLine:=proc(pts)
  local i, m, xp, yp, L, ans;
  m:=rowdim(pts):
  for i from 1 to m do
    xp[i]:=pts[i,1]:
    yp[i]:=pts[i,2]:
  od:
  unassign('i'):
  for i from 1 to m-1 do
   
 L[i]:=line([xp[i],yp[i]],[xp[i+1],yp[i+1]],color=grey,linestyle=1):
   od:
   unassign('i'):
  
 ans:=display([seq(L[i],i=1..m-1)],insequence=false,scaling=constrained):
  RETURN (ans); 
  end:
  
BezPlot:=proc(curve,col)
 plot([curve[1],curve[2],s=0..1],scaling=constrained,axes=none,color=col);
 end: