POLYGONVERTICES Extract all vertices of a (multi-)polygon. VERTS = polygonVertices(POLY) Returns the set of verttices from the polygon POLY. POLY can be either: * a N-by-2 array of vertices. In that case, POLY and VERTS are the same. * a N-by-2 array of vertices with pairs of NaN values separating two rings of the polygon. In that case, the array VERTS corresponds to the vertices of the polygon, without copying the NaN values. * a cell array of loops. In that case, the functions recursively process the polygons and populated the vertex array. Example % create a polygon with a hole, using NaN for separating rings ring1 = [0 0 ; 50 0;50 50;0 50]; ring2 = [20 20;20 30;30 30;30 20]; poly = [ring1 ; NaN NaN ; ring2]; figure; drawPolygon(poly, 'b'); axis([-10 60 -10 60]); axis equal; hold on; verts = polygonVertices(poly); drawPoint(verts, 'bo'); % create a polygon with a hole, storing rings in cell array ring1 = [0 0 ; 50 0;50 50;0 50]; ring2 = [20 20;20 30;30 30;30 20]; poly = {ring1, ring2}; figure; drawPolygon(poly, 'b'); axis([-10 60 -10 60]); axis equal; hold on; verts = polygonVertices(poly); drawPoint(verts, 'bo'); See also polygons2d, polygonEdges
0001 function vertices = polygonVertices(poly) 0002 %POLYGONVERTICES Extract all vertices of a (multi-)polygon. 0003 % 0004 % VERTS = polygonVertices(POLY) 0005 % Returns the set of verttices from the polygon POLY. POLY can be either: 0006 % * a N-by-2 array of vertices. In that case, POLY and VERTS are the 0007 % same. 0008 % * a N-by-2 array of vertices with pairs of NaN values separating two 0009 % rings of the polygon. In that case, the array VERTS corresponds to 0010 % the vertices of the polygon, without copying the NaN values. 0011 % * a cell array of loops. In that case, the functions recursively 0012 % process the polygons and populated the vertex array. 0013 % 0014 % 0015 % Example 0016 % % create a polygon with a hole, using NaN for separating rings 0017 % ring1 = [0 0 ; 50 0;50 50;0 50]; 0018 % ring2 = [20 20;20 30;30 30;30 20]; 0019 % poly = [ring1 ; NaN NaN ; ring2]; 0020 % figure; drawPolygon(poly, 'b'); 0021 % axis([-10 60 -10 60]); axis equal; hold on; 0022 % verts = polygonVertices(poly); 0023 % drawPoint(verts, 'bo'); 0024 % 0025 % % create a polygon with a hole, storing rings in cell array 0026 % ring1 = [0 0 ; 50 0;50 50;0 50]; 0027 % ring2 = [20 20;20 30;30 30;30 20]; 0028 % poly = {ring1, ring2}; 0029 % figure; drawPolygon(poly, 'b'); 0030 % axis([-10 60 -10 60]); axis equal; hold on; 0031 % verts = polygonVertices(poly); 0032 % drawPoint(verts, 'bo'); 0033 % 0034 % See also 0035 % polygons2d, polygonEdges 0036 0037 % ------ 0038 % Author: David Legland 0039 % e-mail: david.legland@inra.fr 0040 % Created: 2018-06-07, using Matlab 9.4.0.813654 (R2018a) 0041 % Copyright 2018 INRA - Cepia Software Platform. 0042 0043 0044 if isnumeric(poly) 0045 % find NaN or infinite values 0046 inds = sum(isfinite(poly), 2) == 2; 0047 0048 % filter non-finite vertices if necessary 0049 if any(~inds) 0050 vertices = poly(inds, :); 0051 else 0052 vertices = poly; 0053 end 0054 elseif iscell(poly) 0055 % process cell array 0056 vertices = zeros(0, 2); 0057 for i = 1:length(poly) 0058 vertices = [vertices ; polygonVertices(poly{i})]; %#ok<AGROW> 0059 end 0060 end