function thinned_stroke = thin_stroke2(stroke,radius,alpha)
% function thinned_stroke = thin_stroke2(stroke,radius,alpha)
% Return a thinned version of the input stroke using 
% radius as the neighborhood metric. Do a linear fit first.
% Then do a quadratic fit. 

neighbor_indices = [];
thinned_stroke   = zeros(size(stroke));
for i = 1:length(stroke);
    neighbor_indices=get_neighbors(stroke(:,[1,2]), stroke(i,[1,2]),radius);
    neighbors = stroke(neighbor_indices,:);
    p     = polyfit( neighbors(:,[1]), neighbors(:,[2]), 1 );
    angle = atan2(p(1),1);
    center = mean(neighbors);
    neighbors(:,1) = neighbors(:,1) - center(1);
    neighbors(:,2) = neighbors(:,2) - center(2);
    rotated_neighbors = rotate_stroke(neighbors,-angle,[0,0,0]);
    p2    = polyfit( rotated_neighbors(:,1), rotated_neighbors(:,2), 2 );
    rotated_point = rotate_point(stroke(i,:)-center,-angle,[0,0,0]);
    % find where to move the point
    y_intercept  = p2(1)*rotated_point(1)*rotated_point(1) + p2(2)*rotated_point(1) + p2(3);
    moved_point  = [rotated_point(1),rotated_point(2)+(y_intercept-rotated_point(2))*alpha, 0];
    % rotate the point back
    rotated_back = rotate_point(moved_point, angle,[0,0,0]);
    
    thinned_stroke(i,:) = [rotated_back(1)+center(1), rotated_back(2)+center(2), stroke(i,3)];
end


