DENSIFYPOLYGON Add several points on each edge of the polygon. POLY2 = densifyPolygon(POLY, N) POLY is a NV-by-2 array containing polygon coordinates. The function iterates on polygon edges, divides it into N subedges (by inserting N-1 new vertices on each edges), and return the resulting polygon. The new polygon POLY has therefore N*NV vertices. Example % Densifies a simple polygon poly = [0 0 ; 10 0;5 10;15 15;5 20;-5 10]; poly2 = densifyPolygon(poly, 10); figure; drawPolygon(poly); axis equal hold on; drawPoint(poly2); See also drawPolygon, edgeToPolyline ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2011-11-25, using Matlab 7.9.0.529 (R2009b) Copyright 2011 INRA - Cepia Software Platform.
0001 function poly2 = densifyPolygon(poly, N) 0002 %DENSIFYPOLYGON Add several points on each edge of the polygon. 0003 % 0004 % POLY2 = densifyPolygon(POLY, N) 0005 % POLY is a NV-by-2 array containing polygon coordinates. The function 0006 % iterates on polygon edges, divides it into N subedges (by inserting N-1 0007 % new vertices on each edges), and return the resulting polygon. 0008 % The new polygon POLY has therefore N*NV vertices. 0009 % 0010 % Example 0011 % % Densifies a simple polygon 0012 % poly = [0 0 ; 10 0;5 10;15 15;5 20;-5 10]; 0013 % poly2 = densifyPolygon(poly, 10); 0014 % figure; drawPolygon(poly); axis equal 0015 % hold on; drawPoint(poly2); 0016 % 0017 % See also 0018 % drawPolygon, edgeToPolyline 0019 % 0020 % 0021 % ------ 0022 % Author: David Legland 0023 % e-mail: david.legland@grignon.inra.fr 0024 % Created: 2011-11-25, using Matlab 7.9.0.529 (R2009b) 0025 % Copyright 2011 INRA - Cepia Software Platform. 0026 0027 % number of vertices, and of edges 0028 Nv = size(poly, 1); 0029 0030 % number of vertices in new polygon 0031 N2 = N * Nv; 0032 poly2 = zeros(N2, 2); 0033 0034 % iterate on polygon edges 0035 for i = 1:Nv 0036 % extract current edge 0037 v1 = poly(i, :); 0038 v2 = poly(mod(i, Nv) + 1, :); 0039 0040 % convert current edge to polyline 0041 newVertices = edgeToPolyline([v1 v2], N); 0042 0043 % indices of current polyline to resulting polygon 0044 i1 = (i-1)*N + 1; 0045 i2 = i * N; 0046 0047 % fill up polygon 0048 poly2(i1:i2, :) = newVertices(1:end-1, :); 0049 end 0050