Recreating a contour using coordinate points

2 views (last 30 days)
Hi all,
I am reading contour points from an external file, which are not stored in order.
I want to set a start point and reconstruct the contour in counterclockwise direction.
I have the following code, it works when the points are equidistant and constructs the contour in clockwise direction.
How Can I change it in a way to force it in one direction so it will ignore the closest distance if the point in wrong direction?
Thanks a lot
%data=xlsread('known_point_set.xlsx');
startx=2;
starty=0;
data=[2 1;2 0;2 2;1 2;0 2;-1 2;-2 2;-2 -1;-2 -2;-1 -2;0 -2;-2 1;-2 0;-2 -1;-2 0;1 -2;2 -2;2 -1];
N = size(data,1);
idx=find(data(:,1)==startx & data(:,2)==starty);
data=circshift(data,N-idx+1,1);
dist = pdist2(data,data);
result = NaN(1,N);
result(1) = 1; % first point is first row in data matrix
for ii=2:N
dist(:,result(ii-1)) = Inf;
[~, closest_idx] = min(dist(result(ii-1),:));
result(ii) = closest_idx;
end
newset=data(result,:);
plot(newset(:,1),newset(:,2))
xlswrite('screen.xlsx',newset);

Accepted Answer

Rajesh Balagam
Rajesh Balagam on 16 Oct 2017
You can try using a variant of Graham Scan algorithm:

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!