圆弧曲线
function curve=mymethod(points1,NT,c1,c0,speed)
px=zeros(2*NT,1);
py=zeros(2*NT,1);
px(1)=points1(1);
py(1)=points1(2);
temp=NT/tan(abs(c1-c0)/2);
R=temp*speed;
for i=2:2*NT
wt=1/temp*(i-1);
if c1-c0>0
px(i) = points1(1) + R* sin(wt)*cos(c0)-(R-R*cos(wt))*sin(c0);
py(i) = points1(2) + R* sin(wt)*sin(c0)+(R-R*cos(wt))*cos(c0);
else
px(i) = points1(1) + (R-R*cos(wt))*sin(c0)+R* sin(wt)*cos(c0);
py(i) = points1(2) + R*sin(wt)*sin(c0)-(R-R*cos(wt))*cos(c0);
end
end
curve=[px,py];
end
B样条曲线
function curve = bspline(points, t)
n = size(points, 1) - 1;
curve = zeros(length(t), 2);
for i = 1:length(t)
for j = 0:n
blend = nchoosek(n, j) * t(i)^j * (1-t(i))^(n-j);
curve(i,:) = curve(i,:) + blend * points(j+1,:);
end
end
end
调用函数
NT=20;
v=0.0257;
c0=0.351;
c1=1.75;
point1=[0,0];
point2=point1+[NT*v*cos(c0),NT*v*sin(c0)];
point3=point2+[NT*v*cos(c1),NT*v*sin(c1)];
% Define control points
points = [point1;point2;point3];
% B-spline
t = 0:0.01:1;
bspline_curve = bspline(points, t);
% Bezier curve
% bezier_curve = bezier(points, t);
mymethod_curve=mymethod(point1,NT,c1,c0,v);
% Plot curves
figure;
hold on;
plot(points(:,1), points(:,2), 'o-', 'LineWidth', 2);
plot(bspline_curve(:,1), bspline_curve(:,2), 'r', 'LineWidth', 2);
hold on
% plot(bezier_curve(:,1), bezier_curve(:,2), 'g', 'LineWidth', 2);
plot(mymethod_curve(:,1), mymethod_curve(:,2), 'g', 'LineWidth', 2);
xlim([0,1]);
ylim([0,1]);
legend('Control Points', 'B-spline Curve', 'Mymethod Curve');
xlabel('X');
ylabel('Y');
title('B-spline vs Mymethod Curve');
参考文献: