function val = DEBOOR(T,p,y,order)

% function val = DEBOOR(T,p,y,order)
%
% INPUT:  T     Knot vector
%         p     Control points (nx2-Matrix)
%         y     t values of the desired points along the B-spline curve
%         order the order of the B-spline (which is the degree + 1)
%
% OUTPUT: val   Points along the B-Spline at t values given by y (mx2-Matrix)
%
% Date:   2007-11-27
% Author: Jonas Ballani
% English translation: Neil Dodgson

m = size(p,1);
n = length(y);
X = zeros(order,order);
Y = zeros(order,order);
a = T(order);
b = T(m);


for l = 1:n
		% we wish to calculate the point on the B-spline for t value t0 = y[l]
    t0 = y(l);
		% id is the list of indices, i, of T for which t0 is greater than T(i)
    id = find(t0 >= T);
		% k is the highest index in id, that is: T(k) is the highest value in T
		%   that is less than or equal to t0
    k = id(end) ;
		if (k > m)
			return;
        end
    X(:,1) = p(k-order+1:k,1);
    Y(:,1) = p(k-order+1:k,2);

    for i = 2:order
        for j = i:order
            num = t0-T(k-order+j);
            if num == 0
                weight = 0;
            else
				s = T(k+j-i+1)-T(k-order+j);
                weight = num/s;
            end
            X(j,i) = (1-weight)*X(j-1,i-1) + weight*X(j,i-1);
            Y(j,i) = (1-weight)*Y(j-1,i-1) + weight*Y(j,i-1);
        end
    end
    val(l,1) = X(order,order);
    val(l,2) = Y(order,order);
end

