-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathArrange2dPos.m
executable file
·60 lines (43 loc) · 1.55 KB
/
Arrange2dPos.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function [x,y,I] = Arrange2dPos(x,y)
%
% takes vectors of x and y values and tries to arrange them so that they form a line
%
if ~isempty(x) && ~isempty(y)
xr=x ; yr=y;
xc=mean(x) ; yc=mean(y);
s=(x-xc).^2+(y-yc).^2;
[~,iloc]=max(s);
%[~,iloc]=min(y);
N=numel(x);
I=zeros(N,1);
I(1)=iloc;
xc=x(iloc) ; yc=y(iloc);
x(iloc)=NaN; y(iloc)=NaN;
% now just walk through the points always moving to the next closest point
d=zeros(N-1,1);
for k=1:N-1
s=(x-xc).^2+(y-yc).^2;
[dist,iloc]=min(s);
d(k)=dist;
I(k+1)=iloc;
xc=x(iloc) ; yc=y(iloc);
x(iloc)=NaN; y(iloc)=NaN; % don't reuse this point
end
x=xr(I) ; y=yr(I);
% try to split GL up into individual GLs, and only return the longest one
ind=find(d>10*std(d));
if ~isempty(ind)
x(ind+1)=NaN; y(ind+1)=NaN;
temp=sort(find(isnan(x))) ; temp=[0;temp;numel(x)+1];
[temp2,I]=max(diff(temp));
n1=temp(I)+1 ;
n2=temp(I+1)-1 ;
x=x(n1:n2) ; y=y(n1:n2);
end
else
%fprintf(' x and y empty on input to Arrange2dPos! \n')
x=[] ; y=[] ; I=[] ;
end
%figure ; plot(x,y,'-o') ; axis equal
%%
end