1 ## Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
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
6 ## (at your option) any later version.
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{spoly} = } simplifypolygon (@var{poly})
18 %% Simplify a polygon using the Ramer-Douglas-Peucker algorithm.
20 %% @var{poly} is a N-by-2 matrix, each row representing a vertex.
22 %% @seealso{simplifypolyline, shape2polygon}
25 function polygonsimp = simplifypolygon (polygon, varargin)
27 polygonsimp = simplifypolyline (polygon,varargin{:});
29 # Remove parrallel consecutive edges
30 PL = polygonsimp(1:end-1,:);
31 PC = polygonsimp(2:end,:);
32 PR = polygonsimp([3:end 1],:);
35 tf = find(isParallel(a,b))+1;
36 polygonsimp (tf,:) = [];
41 %! P = [0 0; 1 0; 0 1];
42 %! P2 = [0 0; 0.1 0; 0.2 0; 0.25 0; 1 0; 0 1; 0 0.7; 0 0.6; 0 0.3; 0 0.1];
43 %! assert(simplifypolygon (P2),P,min(P2(:))*eps)
47 %! P = [0 0; 1 0; 0 1];
48 %! P2 = [0 0; 0.1 0; 0.2 0; 0.25 0; 1 0; 0 1; 0 0.7; 0 0.6; 0 0.3; 0 0.1];
49 %! Pr = simplifypolygon (P2);
52 %! drawPolygon(P,'or;Reference;');
54 %! drawPolygon(P2,'x-b;Redundant;');
55 %! drawPolygon(Pr,'*g;Simplified;');
58 %! % --------------------------------------------------------------------------
59 %! % The two polygons describe the same figure, a triangle. Extra points are
60 %! % removed form the redundant one.