本文分享一段矩阵形式的bezier代码:
Matlab
clc
clear
% 控制点
P = [25;
10;
5;
13];
%% 获得M矩阵
n = length(P) - 1;
M = zeros(n+1,n+1);
for i = 1:n+1
for j = 1:n+1
if(i+j<n+3)
M(i,j) = (-1)^(n -i-j+2)*nchoosek(n,n-i+1)*nchoosek(n-i+1,j-1);
else
M(i,j) = 0;
end
end
end
t_temp = linspace(0,1,100)';
% 生成T矩阵
T = ones(length(t_temp),n + 1);
for i = 1:n
T(:,i) = t_temp.^(n-i+1);
end
% 计算获得bezier曲线离散点
r = T*M*P;
figure(1)
plot(r)
hold on
plot([1,25,50,100],P,'*')
主要是要获得: