1 %% Copyright (C) 2012 Simeon Simeonov <simeon.simeonov.s@gmail.com>
3 %% This program is free software: you can redistribute it and/or modify
4 %% it under the terms of the GNU General Public License as published by
5 %% the Free Software Foundation, either version 3 of the License, or
8 %% This program is distributed in the hope that it will be useful,
9 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
10 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 %% GNU General Public License for more details.
13 %% You should have received a copy of the GNU General Public License
14 %% along with this program. If not, see <http://www.gnu.org/licenses/>.
17 %% @deftypefn {Function File} {@var{y} =} polygon (@var{x})
18 %% Returns a simple closed path that passes through all the points in @var{x}.
19 %% @var{x} is a vector containing 2D coordinates of the points.
23 %% Author: Simeon Simeonov <simeon.simeonov.s@gmail.com>
25 function y = closed_path(x)
27 if(size(x,1) > 1 && size(x,1) < size(x,2))
31 N = size(x,1); % Number of points
32 idx = zeros(N, 1); % ind contains the indices of the sorted coordinates
34 a = find(x(:,2)==min(x(:,2)));
37 [~, i] = sort(x(a,1));
41 x_1 = x(x(:,2)==x(a,2),:); % find all x with the same y coordinate
43 if(x(a,1) == min(x(:,1)))
44 x_2 = x(x(:,1)==x(a,1),:); % find all x with the same x coordinate
49 if(size(x_1,1) > 1 || size(x_2,1) > 1)
51 x_1 = sort(x_1); % Sort by x coordinate
52 y(1,:) = x(a,:); % original starting point
56 x_2 = sort(x_2, 'descend');
60 i = ismember(x,x_not,'rows');
62 x = [x_1(size(x_1,1),:); x];
63 x_1(size(x_1, 1),:) = [];
70 d = x - repmat(x(a,:), N, 1);
71 th = d(:,2)./(d(:,1) + d(:,2));
73 [~, idx0] = ismember(sort(th(th==0)), th);
74 [~, idx1] = ismember(sort(th(th>0)), th);
75 [~, idx2] = ismember(sort(th(th<0)), th);
77 idx = [a; idx0; idx1; idx2];
78 % I contains the indices of idx in a sorted order. [v i] = sort(idx) then
81 if(size(I,1) ~= size(J,1))
82 R = histc(J, 1:size(I,1)); % R(1) will always be 1?
86 idx_repeated = idx_sorted(ri);
87 idx(idx==idx_repeated) = find(th==th(idx_sorted(ri)));
91 y = [x_1; x(idx,:); x_2;];
100 %! x = randi(maxInt, N, 2);
101 %! y = closed_path(x);
102 %! plot(y(:,1), y(:,2), '*');
104 %! plot(y(:,1), y(:,2));