Bezier curve functions in MATLAB Write a MATLAB function cal
Bezier curve functions in MATLAB
Write a MATLAB function called myBezier.m. The first line of the file should be: function myBezier(xdp, ydp, xguide, yguide, n). Plot the Bezier curve that interpolates between data points (1,2) and (5,8) with guide points (-1,-3) and (12,10).
Solution
function myBezier(xdp, ydp, xguide, yguide, n)
xs = xdp(1):.02:xdp(2);
ys = ydp(1):.02:ydp(2);
% Define the quadratic Bernstein polynomials
BernsteinX = [(1-xs); xs];
BernsteinY = [(1-ys); ys];
% Define quadratic control points (X,Y)
P = [xguide(1) yguide(1);
xguide(2) yguide(2)];
% Beziers for the X and Y coordinates
DataX = BernsteinX(1,:) * P(1,1) + BernsteinX(2,:) * P(2,1);
DataY = BernsteinY(1,:) * P(1,2) + BernsteinY(2,:) * P(2,2);
figure
axis equal
hold on
% Plot control polygon
plot( P(:,1), P(:,2), \'k.--\', \'LineWidth\', 1, \'MarkerSize\', 30 );
% Plot Bezier curve
plot( DataX, DataY, \'Color\', [255/255,127/255], \'LineWidth\', 2 );
% Add labels for control points P
for i=1:length(P)
text( P(i,1), P(i,2), [\'P\', num2str(i) ], \'FontSize\', 16, \'Color\',[0,129/255] );
end
