ROWTOPOLYGON Create a polygon from a row vector. POLY = rowToPolygon(ROW) Convert a 1-by-2*N row vector that concatenates all polygon vertex coordinates into a N-by-2 array of coordinates. Default ordering of coordinates in ROW is: [X1 Y1 X2 Y2 X3 Y3 .... XN YN]. POLY = rowToPolygon(ROW, METHOD) Specifies the method for concatenating coordinates. METHOS is one of: 'interlaced': default method, described above. 'packed': the vector ROW has format: [X1 X2 X3 ... XN Y1 Y2 Y3 ... YN]. POLYS = rowToPolygon(ROWS, ...) When ROWS is a NP-by-NV array containing the vertex coordinates of NP polygons, returns a 1-by-NP cell array containing in each cell the coordinates of the polygon. Example % Concatenate coordinates of a circle and draw it as a polygon t = linspace (0, 2*pi, 200); row = [cos(t) sin(t)]; poly = rowToPolygon(row, 'packed'); figure;drawPolygon(poly) See also polygons2d, polygonToRow
0001 function poly = rowToPolygon(row, varargin) 0002 %ROWTOPOLYGON Create a polygon from a row vector. 0003 % 0004 % POLY = rowToPolygon(ROW) 0005 % Convert a 1-by-2*N row vector that concatenates all polygon vertex 0006 % coordinates into a N-by-2 array of coordinates. 0007 % Default ordering of coordinates in ROW is: 0008 % [X1 Y1 X2 Y2 X3 Y3 .... XN YN]. 0009 % 0010 % POLY = rowToPolygon(ROW, METHOD) 0011 % Specifies the method for concatenating coordinates. METHOS is one of: 0012 % 'interlaced': default method, described above. 0013 % 'packed': the vector ROW has format: 0014 % [X1 X2 X3 ... XN Y1 Y2 Y3 ... YN]. 0015 % 0016 % POLYS = rowToPolygon(ROWS, ...) 0017 % When ROWS is a NP-by-NV array containing the vertex coordinates of NP 0018 % polygons, returns a 1-by-NP cell array containing in each cell the 0019 % coordinates of the polygon. 0020 % 0021 % 0022 % Example 0023 % % Concatenate coordinates of a circle and draw it as a polygon 0024 % t = linspace (0, 2*pi, 200); 0025 % row = [cos(t) sin(t)]; 0026 % poly = rowToPolygon(row, 'packed'); 0027 % figure;drawPolygon(poly) 0028 % 0029 % See also 0030 % polygons2d, polygonToRow 0031 0032 % ------ 0033 % Author: David Legland 0034 % e-mail: david.legland@grignon.inra.fr 0035 % Created: 2010-07-23, using Matlab 7.9.0.529 (R2009b) 0036 % Copyright 2010 INRA - Cepia Software Platform. 0037 0038 % HISTORY 0039 % 2014-01-29 add support for multiple rows 0040 0041 type = 'interlaced'; 0042 if ~isempty(varargin) 0043 type = varargin{1}; 0044 end 0045 0046 % number of polygons 0047 nPolys = size(row, 1); 0048 0049 % polygon vertex number 0050 Np = size(row, 2) / 2; 0051 0052 0053 if strcmp(type, 'interlaced') 0054 % ordering is [X1 Y1 X2 X2... XN YN] 0055 if nPolys == 1 0056 poly = reshape(row, [2 Np])'; 0057 else 0058 poly = cell(1, nPolys); 0059 for i = 1:nPolys 0060 poly{i} = reshape(row(i,:), [2 Np])'; 0061 end 0062 end 0063 0064 elseif strcmp(type, 'packed') 0065 % ordering is [X1 X2 X3... XN Y1 Y2 Y3... YN] 0066 if nPolys == 1 0067 poly = [row(1:Np)' row(Np+1:end)']; 0068 else 0069 poly = cell(1, nPolys); 0070 for i = 1:nPolys 0071 poly{i} = [row(i, 1:Np)' row(i, Np+1:end)']; 0072 end 0073 end 0074 end