生成B样条代码(matlab)

  • Post author:
  • Post category:其他


%定义B样条曲线的控制点,其中第一行是x坐标,第二行是y坐标。
ctripoints = [-1.7 -1.4 -0.2 1 2.3 2.9
              -0.3  0.7  1.2 0.2 1.1 -0.35];
           
%定义了节点向量。节点向量的长度应该是控制点的数量加上次数+1。有7个控制点,
%次数是3(想要绘制三次B样条曲线),所以节点向量的长度应该是7+3+1=11。
%knots = [0 0 0 2 5 6 7 8 8 8];
knots = [1 1 1 2 4 6 8 8 8];

% % 计算控制点的凸包
% k = convhull(ctripoints(1,:), ctripoints(2,:));
% % 绘制凸包区域
% patch(ctripoints(1,k), ctripoints(2,k), 'blue', 'FaceAlpha', 0.1);

%使用MATLAB的spmak函数创建B样条曲线。它需要一个节点向量和控制点作为输入,
%并返回一个数据结构sp,其中包含B样条曲线的所有信息
sp = spmak(knots,ctripoints);

%使用MATLAB的fnplt函数绘制B样条曲线。
%它需要sp数据结构和一个线性样式字符串作为输入,这里使用'b-'表示蓝色实线。
fnplt(sp,'r-');

hold on;
grid on;
x0 = ctripoints(1,:);
y0 = ctripoints(2,:);
plot(x0,y0,'k*','linewidth',2.5);
plot(x0,y0,'b--','linewidth',2);
%'Color',[1,0.75,0.8]'粉色

% 在点(x,y)附近添加文本标签
text(x0(1), y0(1), 'Q0', 'FontSize', 12, 'FontWeight', 'bold', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom');
text(x0(2), y0(2), 'Q1', 'FontSize', 12, 'FontWeight', 'bold', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom');
text(x0(3), y0(3), 'Q2', 'FontSize', 12, 'FontWeight', 'bold', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom');
text(x0(4), y0(4), 'Q3', 'FontSize', 12, 'FontWeight', 'bold', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'top');
text(x0(5), y0(5), 'Q4', 'FontSize', 12, 'FontWeight', 'bold', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom');
text(x0(6), y0(6), 'Q5', 'FontSize', 12, 'FontWeight', 'bold', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'top');


% 设置横坐标范围为0到70
xlim([-2 3]);
ylim([-0.5 1.5]);


还行

这个也挺好看



版权声明:本文为weixin_45868890原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。