From 156a174301eb993968b6572e7f9cb17eaa9e90f9 Mon Sep 17 00:00:00 2001
From: Jean-Yves TINEVEZ <jean-yves.tinevez@pasteur.fr>
Date: Fri, 24 Jul 2020 14:56:03 +0200
Subject: [PATCH] First version of the find_contour function.

That can connect unordered points on a non-convex polygon,
provided that connected points are close.
---
 src/@deproj/find_countour.m | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 src/@deproj/find_countour.m

diff --git a/src/@deproj/find_countour.m b/src/@deproj/find_countour.m
new file mode 100644
index 0000000..a5cae48
--- /dev/null
+++ b/src/@deproj/find_countour.m
@@ -0,0 +1,37 @@
+function P2 = find_countour( P )
+%FIND_COUNTOUR Sort x,y coordinates along a continuous contour.
+
+    P2 = NaN( size( P ) );
+    current = 1;
+
+    % Start point
+    ps = P( 1, : );    
+    set_visited( 1 )
+    P2( current, : ) = ps;
+    current = current + 1;
+    
+    % Loop.
+    done = size( P, 1 ) == 1;
+    while ~done
+
+        done = current == size( P, 1 );
+
+        id = find_next_point( ps );
+        ps = P( id, : );
+        set_visited( id )
+        P2( current, : ) = ps;
+        current = current + 1;
+        
+    end
+    
+    
+    function set_visited( id )
+        P( id, : ) = NaN;
+    end
+    
+    function id = find_next_point( p0 )        
+        [ ~, id ] = pdist2( P, p0, 'Euclidean', 'Smallest', 1 );
+    end
+    
+end
+
-- 
GitLab