Skip to content

Instantly share code, notes, and snippets.

@facundoq
Created August 3, 2015 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save facundoq/39b4772151291fd41543 to your computer and use it in GitHub Desktop.
Save facundoq/39b4772151291fd41543 to your computer and use it in GitHub Desktop.
Order set of 2d points to convert to a poligon
function [poligon,indices]=poligon_from_set_of_points(points,max_distance)
%
% Note that a subset of the points may be selected if the distance between
% two points exceeds max_distance
dist = pdist2(points,points);
N = size(points,1);
indices = NaN(1,N);
indices(1) = 1; % first point is first row in x matrix
for i=2:N
dist(:,indices(i-1)) = Inf;
[distance, closest_idx] = min(dist(indices(i-1),:));
if (distance<max_distance)
indices(i) = closest_idx;
else
indices(i:end)=[];
break;
end
end
poligon=points(indices,:);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment