天天看點

B樣條曲線的繪制

B樣條曲線的繪制
B樣條曲線的繪制
%四個控制點B樣條曲線的生成
%四個控制點
c=[0 1 2 3;0 1 1 0];
s=0:0.01:1; %歸一化路程0=<s<=1
%四個樣條函數
f1s=(1-s).^3/6;
f2s=(3*s.^3-6*s.^2+4)/6;
f3s=(-3*s.^3+3*s.^2+3*s+1)/6;
f4s=s.^3/6;
ps=c(:,1)*f1s+c(:,2)*f2s+c(:,3)*f3s+c(:,4)*f4s;
figure(1)
plot(c(1,:),c(2,:),'r*',ps(1,:),ps(2,:),'b','LineWidth',2);
legend('控制點','B樣條曲線')
title('四個控制點及其B樣條曲線')
grid on      
B樣條曲線的繪制
%五個控制點B樣條曲線的生成
%五個控制點
c=[0 1 2 3 4;0 1 0 1 0];
s=0:0.01:1;
%四個樣條函數
f1s=(1-s).^3/6;
f2s=(3*s.^3-6*s.^2+4)/6;
f3s=(-3*s.^3+3*s.^2+3*s+1)/6;
f4s=s.^3/6;
%計算兩個樣條曲線,個數為控制點的個數減3
p1s=c(:,1)*f1s+c(:,2)*f2s+c(:,3)*f3s+c(:,4)*f4s;
p2s=c(:,2)*f1s+c(:,3)*f2s+c(:,4)*f3s+c(:,5)*f4s;
figure(1)
plot(c(1,:),c(2,:),'r*',p1s(1,:),p1s(2,:),'b',p2s(1,:),p2s(2,:),'c','LineWidth',2);
legend('控制點','第一段','第二段')
title('五個控制點及其B樣條曲線')
grid on      
B樣條曲線的繪制
%N個控制點B樣條曲線的生成
%N個控制點
c=[0 1 2 -2 -1 0;0 0.3 2.5 2.5 4.7 5];
s=0:0.01:1;
N=length(c); %控制點數目
%四個樣條函數
f1s=(1-s).^3/6;
f2s=(3*s.^3-6*s.^2+4)/6;
f3s=(-3*s.^3+3*s.^2+3*s+1)/6;
f4s=s.^3/6;
figure(1)
mycolor='mbc';
plot(c(1,:),c(2,:),'r*');
hold on
%繪制控制點及B樣條曲線
for i=1:N-3
    p=c(:,i)*f1s+c(:,i+1)*f2s+c(:,i+2)*f3s+c(:,i+3)*f4s;
    plot(p(1,:),p(2,:),...
        mycolor(mod(i,3)+1),'LineWidth',2);
end
title('N個控制點及其B樣條曲線')
grid on;
hold off      
B樣條曲線的繪制
B樣條曲線的繪制
B樣條曲線的繪制
 中國大學MOOC智能機器人系統:

繼續閱讀