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@inrae.fr 0040 % Created: 2018-06-07, using Matlab 9.4.0.813654 (R2018a) 0041 % Copyright 2018-2024 INRA - Cepia Software Platform 0042 0043 if isnumeric(poly) 0044 % find NaN or infinite values 0045 inds = sum(isfinite(poly), 2) == 2; 0046 0047 % filter non-finite vertices if necessary 0048 if any(~inds) 0049 vertices = poly(inds, :); 0050 else 0051 vertices = poly; 0052 end 0053 elseif iscell(poly) 0054 % process cell array 0055 vertices = zeros(0, 2); 0056 for i = 1:length(poly) 0057 vertices = [vertices ; polygonVertices(poly{i})]; %#ok<AGROW> 0058 end 0059 end