function stroke_properties = derive_stroke_properties(stroke)
% Derive stroke curvature, speed, accumulated length and direction.
% Fill in columns of stroke_properties. The number of 
% entries is equal to the number of points in the stroke. 
% Speed is padded with a 0 at the end, and curvature is
% padded with two zeroes at the end. Accumulated length starts with 0.

stroke_properties = zeros( length( stroke ), 4);
for i = 2:length(stroke);
    distance                 = norm(stroke(i,[1,2])-stroke(i-1,[1,2]));
    stroke_properties(i,  3) = distance + stroke_properties(i-1,3);
    stroke_properties(i-1,2) = distance/(stroke(i,3)-stroke(i-1,3));
end

d1 = zeros([1,3]);
d2 = zeros([1,3]);
stroke_properties(1,4)   = atan2(stroke(2,2)-stroke(1,2), stroke(2,1)-stroke(1,1));
for i = 3:length(stroke)
    d1(1,[1,2])          = stroke(i-1,[1,2])-stroke(i-2,[1,2]);
    d2(1,[1,2])          = stroke(i,[1,2])  -stroke(i-1,[1,2]);
    cross_product        = cross(d1/norm(d1),d2/norm(d2));
    stroke_properties(i-1,4) = asin( cross_product(1,3) )*sign(dot(d1,d2))+stroke_properties(i-2,4);
end

for i = 3:length(stroke)
    stroke_properties(i-2,1) = (stroke_properties(i-1,4)-stroke_properties(i-2,4))/((stroke_properties(i,3)-stroke_properties(i-2,3))/2);
end
