反求bezier曲线控制点 matlab,matlab 已知一系列的点,怎样用bezier曲线去拟合,并反求控制点...

2017-09-15 回答

最简单的多项式拟合

p = polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the data y best in a least-squares sense. p is a row vector of length n+1 containing the polynomial coefficients in descending powers, p(1)*x^n + p(2)*x^(n-1) +...+ p(n)*x + p(n+1).

三次样条插值

pp = spline(x,y) returns the piecewise polynomial form of the cubic spline interpolant for later use with ppval and the spline utility unmkpp. x must be a vector. y can be a scalar, a vector, or an array of any dimension. if y is an array that is not a vector, the size of y must have the form [d1,d2,...,dk,n], where n is the length of x. the interpolation is performed for each d1-by-d2-by-...-dk value in y.

yy = spline(x,y,xx) is the same as yy = ppval(spline(x,y),xx), thus providing, in yy, the values of the interpolant at xx. xx can be a scalar, a vector, or a multidimensional array.

bezier曲线

function [x,y]=bezier(x,y)

%用法:

%bezier(x,y)

% 生成n-1次贝塞尔曲线,其中x和y是n个点的坐标

%h=bezier(x,y)

% 生成n-1次贝塞尔曲线并返回曲线句柄

%[x,y]=bezier(x,y)

% 返回n-1次贝塞尔曲线的坐标

%例子:

%bezier([5,6,10,12],[0 5 -5 -2])

n=length(x);

t=linspace(0,1);

xx=0;yy=0;

for k=0:n-1

tmp=nchoosek(n-1,k)*t.^k.*(1-t).^(n-1-k);

xx=xx+tmp*x(k+1);

yy=yy+tmp*y(k+1);

end

if nargout==2

x=xx;y=yy;

end

h=plot(xx,yy);

if nargout==1

x=h;

end

end

matlab提供了很多插值,拟合的函数,可在帮助里查一下,还有例子程序。

比如说polyfit

可用 doc polyfit 来查找,找到后还有很多相关的函数,一个个找下去,能找到你需要的。