# hermite.txt  - commands to compute and plot Hermite curves

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

printf( "\nCommands in bezcurve.txt");
printf("\n  Hermite3(p0,v0,p1,v1) - computes cubic Hermite curve with vectors");
printf("\n  Hermite5(p0,v0,a0,p1,v1,a1) - computes quintic Hermite curve");
printf("\n Plot(c,col) - plots the Hermite curve over the range s=0..1 in the color col");


Hermite3:=proc(p0,v0,p1,v1) 
local b, pts, PP, L:
b:=array(1..4,1..2):
b[1,1]:=p0[1]:
b[1,2]:=p0[2]:
b[2,1]:=p0[1]+v0[1]/3:
b[2,2]:=p0[2]+v0[2]/3:
b[3,1]:=p1[1]-v1[1]/3:
b[3,2]:=p1[2]-v1[2]/3:
b[4,1]:=p1[1]:
b[4,2]:=p1[2]:
pts:=convert(b,matrix):
RETURN(Curve(pts));
end:
 
Hermite5:=proc(p0,v0,a0,p1,v1,a1)
local b,pts,PP,L:
b:=array(1..6,1..2):
b[1,1]:=p0[1]:
b[1,2]:=p0[2]:
b[2,1]:=p0[1]+v0[1]/5:
b[2,2]:=p0[2]+v0[2]/5:
b[3,1]:=p0[1]+2*v0[1]/5+a0[1]/20:
b[3,2]:=p0[2]+2*v0[2]/5+a0[2]/20:
b[4,1]:=p1[1]-2*v1[1]/5+a1[1]/20:
b[4,2]:=p1[2]-2*v1[2]/5+a1[2]/20:
b[5,1]:=p1[1]-v1[1]/5:
b[5,2]:=p1[2]-v1[2]/5:
b[6,1]:=p1[1]:
b[6,2]:=p1[2]:
pts:=convert(b,matrix):
RETURN(Curve(pts));
end:
  
Curve:=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)):
RETURN ([x,y]);
end:
  
Plot:=proc(curve,col)
plot([curve[1],curve[2],s=0..1],scaling=constrained,axes=none,color=col);
end: 

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: