Home > matGeom > polygons2d > polygonEdges.m

polygonEdges

PURPOSE ^

POLYGONEDGES Return the edges of a simple or multiple polygon.

SYNOPSIS ^

function edges = polygonEdges(poly)

DESCRIPTION ^

POLYGONEDGES Return the edges of a simple or multiple polygon.

   EDGES = polygonEdges(POLY)
   Return the set of edges of the polygon specified by POLY. POLY may be
   either a simple polygon given as a N-by-2 array of vertices, or a
   multiple polygon given by a cell array of linear rings, each ring being
   given as N-by-2 array of vertices.


   Example
     poly = [50 10;60 10;60 20;50 20];
     polygonEdges(poly)
     ans =
         50    10    60    10
         60    10    60    20
         60    20    50    20
         50    20    50    10

   See also
     polygons2d, polygonVertices

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function edges = polygonEdges(poly)
0002 %POLYGONEDGES Return the edges of a simple or multiple polygon.
0003 %
0004 %   EDGES = polygonEdges(POLY)
0005 %   Return the set of edges of the polygon specified by POLY. POLY may be
0006 %   either a simple polygon given as a N-by-2 array of vertices, or a
0007 %   multiple polygon given by a cell array of linear rings, each ring being
0008 %   given as N-by-2 array of vertices.
0009 %
0010 %
0011 %   Example
0012 %     poly = [50 10;60 10;60 20;50 20];
0013 %     polygonEdges(poly)
0014 %     ans =
0015 %         50    10    60    10
0016 %         60    10    60    20
0017 %         60    20    50    20
0018 %         50    20    50    10
0019 %
0020 %   See also
0021 %     polygons2d, polygonVertices
0022  
0023 % ------
0024 % Author: David Legland
0025 % e-mail: david.legland@inra.fr
0026 % Created: 2017-08-29,    using Matlab 9.1.0.441655 (R2016b)
0027 % Copyright 2017 INRA - Cepia Software Platform.
0028 
0029 % test presence of NaN values
0030 if isnumeric(poly) && any(isnan(poly(:)))
0031     poly = splitPolygons(poly);
0032 end
0033 
0034 % create the array of polygon edges
0035 if iscell(poly)
0036     % process multiple polygons
0037     edges = zeros(0, 4);
0038     for i = 1:length(poly)
0039         pol = poly{i};
0040         N = size(pol, 1);
0041         edges = [edges; pol(1:N, :) pol([2:N 1], :)]; %#ok<AGROW>
0042     end
0043 else
0044     % get edges of a simple polygon
0045     N = size(poly, 1);
0046     edges = [poly(1:N, :) poly([2:N 1], :)];
0047 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019