Code covered by the BSD License

### Highlights from Bezier Curve Function

1.0
1.0 | 3 ratings Rate this file 12 Downloads (last 30 days) File Size: 1.37 KB File ID: #34902 Version: 1.0

# Bezier Curve Function

### Prakash Manandhar (view profile)

Vectorized function to calculate Bezier Curve

File Information
Description

This function constructs a Bezier curve from given control points. P is a vector of control points. N is the number of points to calculate.

Example:

P = [0 0; 1 1; 2 5; 5 -1];
x = (0:0.0001:5);
y = BezierCurve(length(x), P);
plot(x, y, P(:, 1), P(:, 2), 'x-', 'LineWidth', 2); set(gca, 'FontSize', 16)

Acknowledgements

Bezier Curve Plotter inspired this file.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Comments and Ratings (4)
04 Jun 2014 Jason Kurtz

### Jason Kurtz (view profile)

Here is modified code that works. Note that the function now returns two sets of variables.

function [x y] = BezierCurve(N, P)
% This function constructs a Bezier curve from given control points. P is a
% vector of control points. N is the number of points to calculate.
%
% Example:
%
% P = [0 0; 1 1; 2 5; 5 -1];
% y = BezierCurve(1000, P);
% plot(x, y, P(:, 1), P(:, 2), 'x-', 'LineWidth', 2); set(gca, 'FontSize', 16)
%
% Prakash Manandhar, pmanandhar@umassd.edu

Np = size(P, 1);
u = linspace(0, 1, N);
B = zeros(N, Np);
for i = 1:Np
B(:,i) = nchoosek(Np-1,i-1).*u.^(i-1).*(1-u).^(Np-i); %B is the Bernstein polynomial value
end
S = B*P;
x = S(:, 1);
y = S(:, 2);

Comment only
02 Dec 2013 Gaute Hope

### Gaute Hope (view profile)

Yes, there is a problem with this function.

16 Feb 2012 Joey

16 Feb 2012 Joey

### Joey (view profile)

I don't understand the math well enough to pick out exactly what's wrong, but the curve that this code gives for certain sets of points is definitely not right.

Try this and you'll see:
P = [0 2; 4.38 6.38; 5.32 5.32; 9.7 9.7];
x = (0:0.01:9.7);
y = BezierCurve(length(x), P);
plot(x, y, P(:, 1), P(:, 2), 'x-', 'LineWidth', 2); set(gca, 'FontSize', 16)