function demoSplineBezier2
% functie ce ploteaza spline-ul Bezier
% asa cum se regaseste in prezentarea de la curs
%
% UT-CLUJ 2018-2019


% pentru a salva imagini (doar daca e necesar)
printOn = 0;
fileNameBase = 'quad_bezier_spline_';


xdata   = [0 2 5 7 9];
ydata   = [1 6 4 8 5];

xmin    = min(xdata);
xmax    = max(xdata);
ymin    = min(ydata);
ymax    = max(ydata);

lxdata  = length(xdata);
spxdata = (xmax-xmin)/(lxdata-1);
spydata = (ymax-ymin)/(lxdata-1);

xeqdata = [xmin:spxdata:xmin+(lxdata-1)*spxdata];
yeqdata = interp1(xdata,ydata, xeqdata);

axLims  = [xmin-spxdata xmax+spxdata ymin-spydata ymax+spydata];
plot(xdata,ydata,'og');
axis(axLims);
if printOn
  drawnow('expose');
  fileName = [fileNameBase 'not_equidistant.jpg'];
  print(fileName);
end;
hold on;
pause 
plot(xeqdata,yeqdata,'-or');
if printOn
  drawnow('expose');
  fileName = [fileNameBase 'equi_lin_spline.jpg'];
  print(fileName);
end;
hold on;
pause;

xmaxB     = xmin+(lxdata-1)*spxdata;
xBezier   = [xmin:spxdata/2:xmaxB];
yBezier   = interp1(xeqdata,yeqdata, xBezier);
lxBezier  = length(xBezier);

i0 = 2; i1 = i0+1; i2 = i0+2;
while i2 < lxBezier
  xA0 = xBezier(i0); yA0 = yBezier(i0);
  xA1 = xBezier(i1); yA1 = yBezier(i1);
  xA2 = xBezier(i2); yA2 = yBezier(i2);
  t = [0:0.05:1];
  xPlot = (1-t).^2*xA0+ 2*t.*(1-t)*xA1 + t.^2 * xA2;
  yPlot = (1-t).^2*yA0+ 2*t.*(1-t)*yA1 + t.^2 * yA2;
  plot(xPlot,yPlot);
  if printOn
    drawnow('expose');
    countStr  = sprintf('_%d.jpg', i2);
    fileName = [fileNameBase 'bezier_2' countStr];
    print(fileName);
  end;
  hold on;
  i0 = i2;
  i1 = i0+1;
  i2 = i0+2;
  pause;
end;

 

