Bezier曲线拟合数据点的几何作图法


Bezier曲线拟合数据点的几何作图法MATLAB程序。

Contents

左键选择点,右键或中间结束

clear
close all
figure;
axis([-5,5,-5,5]);
grid on;
title('Bezier曲线拟合');
questdlg('1.鼠标左键:选择新点   2.鼠标中键或右键:选择最后一个点','操作','ok','ok');

从鼠标获取数据点

i = 0;
while 1
    i = i+1;
    tip = ['请选择第',num2str(i),'个数据点'];
    xlabel(tip);
    try
    [x,y,button] = ginput(1);
    catch
        return;
    end
    hold on ,plot(x,y,'o');
    dataPoints(:,i) = [x;y];
    h1 = plot(dataPoints(1,:),dataPoints(2,:),'b');
    if button ~= 1
        xlabel('');
        break;
    end
end


for i = 0:0.01:1
    newData(:,int32(i*100)+1) = cqj_GetBezier3Point(dataPoints,i);
end
h2 = plot(newData(1,:),newData(2,:),'.-r');
legend([h1,h2],'原始点','3次Bezier曲线')

得到曲线参数为u的数据点

function [ data ] = cqj_GetBezier3Point(dataPoints,u)
% 得到曲线参数为u的数据点
% dataPoints:初始点 2xn
data = dataPoints;
while 1
    if size(data,2) == 1
        break;
    end
    temp = zeros(size(data,1),size(data,2)-1);
    for i = 1:size(data,2)-1
        temp(:,i) = (1-u)*data(:,i)+u*data(:,i+1);
    end
    data = temp;
end
end


如有任何疑问或建议欢迎下方留言 :-)


评论