Recreating a contour using coordinate points

2 次查看(过去 30 天)
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);

采纳的回答

Rajesh Balagam
Rajesh Balagam 2017-10-16
You can try using a variant of Graham Scan algorithm:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by