Bézier曲線とB-spline曲線

clear; close all;

figure();grid();
xlim([-10 10]);ylim([-10 10]);
[x,y]=ginput(4);
x0=x(1);x1=x(2);x2=x(3);x3=x(4);
y0=y(1);y1=y(2);y2=y(3);y3=y(4);

Px1=[];Py1=[];
Px2=[];Py2=[];
for t=0:0.01:1
    %Bézier
    B0=(1-t)^3;
    B1=3*(1-t)^2*t;
    B2=3*(1-t)*t^2;
    B3=t^3;
    Px=B0*x0+B1*x1+B2*x2+B3*x3;
    Py=B0*y0+B1*y1+B2*y2+B3*y3;
    Px1(length(Px1)+1)=Px;
    Py1(length(Py1)+1)=Py;
    %B-spline
    N0=1/6*(1-t)^3;
    N1=1/2*t^3-t^2+2/3;
    N2=-1/2*t^3+1/2*t^2+1/2*t+1/6;
    N3=1/6*t^3;
    Px=N0*x0+N1*x1+N2*x2+N3*x3;
    Py=N0*y0+N1*y1+N2*y2+N3*y3;
    Px2(length(Px2)+1)=Px;
    Py2(length(Py2)+1)=Py;
end

plot(Px1,Py1,'c-');hold on;
plot(Px2,Py2,'m-');

h1=plot(x0,y0,'ro','HandleVisibility','off');
h2=plot(x1,y1,'bo','HandleVisibility','off');
h3=plot(x2,y2,'bo','HandleVisibility','off');
h4=plot(x3,y3,'ro','HandleVisibility','off');
xlim([-10 10]);ylim([-10 10]);grid();
legend('Bézier','B-spline');

set(h1, 'markerfacecolor', get(h1, 'color'));
set(h2, 'markerfacecolor', get(h2, 'color'));
set(h3, 'markerfacecolor', get(h3, 'color'));
set(h4, 'markerfacecolor', get(h4, 'color'));

f:id:seinzumtode:20191105025100p:plain