%BEZIER Bezier curve 
% evaluates a point along a Bezier curve
%
% Syntax:  P = bezier(u, CP)
%
% Inputs:
%    u - parameter along the curve 0 <= u <= 1
%    CP - is a nxm vector of n control points in m-space, eg. [x y z
%                                                              x y z
%                                                               ... ]
% Outputs:
%    P - a 1xm vector giving the point on the curve, eg. [x,y,z] 
%
% See also: PLOTBEZIER

% Author: Travis Hydzik
% Last revision: 20 October 2004

function P = bezier(u, CP)

    if u < 0 || u > 1   
        error('u must be between 0 and 1');
    end

	[N, m] = size(CP);
	n = N - 1;
	P = zeros(1, m);
    
	for i = 0:n
        P = P + nchoosek(n,i) * u^i*(1 - u)^(n-i) * CP(i + 1,:);
	end
