Home > matGeom > polygons2d > drawPolygon.m

drawPolygon

PURPOSE ^

DRAWPOLYGON Draw a polygon specified by a list of points.

SYNOPSIS ^

function varargout = drawPolygon (px, varargin)

DESCRIPTION ^

DRAWPOLYGON Draw a polygon specified by a list of points.

   drawPolygon(POLY);
   Packs coordinates in a single N-by-2 array, with N the vertex number.

   drawPolygon(PX, PY);
   Specifies coordinates in separate arrays. Both array must be N-by-1,
   with N the number of vertices.

   drawPolygon(POLYS)
   Packs coordinate of several polygons in a cell array. Each element of
   the array is a Ni-by-2 double array.

   drawPolygon(..., NAME, VALUE);
   Specifies drawing options by using one or several parameter name-value
   pairs, see the doc of plot function for details.

   drawPolygon(AX, ...)
   Specifies the axis to draw the polygon on.

   H = drawPolygon(...);
   Also return a handle to the list of line objects.

   Example
     % draw a red rectangle
     poly = [10 10;40 10;40 30;10 30];
     figure; drawPolygon(poly, 'r');
     axis equal; axis([0 50 0 50]); 

     % Draw two squares
     px = [10 20 20 10 NaN 30 40 40 30]';
     py = [10 10 20 20 NaN 10 10 20 20]';
     figure; 
     drawPolygon([px py], 'lineWidth', 2);
     axis equal; axis([0 50 0 50]); 
 
   See also:
   polygons2d, drawPolyline

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawPolygon (px, varargin)
0002 %DRAWPOLYGON Draw a polygon specified by a list of points.
0003 %
0004 %   drawPolygon(POLY);
0005 %   Packs coordinates in a single N-by-2 array, with N the vertex number.
0006 %
0007 %   drawPolygon(PX, PY);
0008 %   Specifies coordinates in separate arrays. Both array must be N-by-1,
0009 %   with N the number of vertices.
0010 %
0011 %   drawPolygon(POLYS)
0012 %   Packs coordinate of several polygons in a cell array. Each element of
0013 %   the array is a Ni-by-2 double array.
0014 %
0015 %   drawPolygon(..., NAME, VALUE);
0016 %   Specifies drawing options by using one or several parameter name-value
0017 %   pairs, see the doc of plot function for details.
0018 %
0019 %   drawPolygon(AX, ...)
0020 %   Specifies the axis to draw the polygon on.
0021 %
0022 %   H = drawPolygon(...);
0023 %   Also return a handle to the list of line objects.
0024 %
0025 %   Example
0026 %     % draw a red rectangle
0027 %     poly = [10 10;40 10;40 30;10 30];
0028 %     figure; drawPolygon(poly, 'r');
0029 %     axis equal; axis([0 50 0 50]);
0030 %
0031 %     % Draw two squares
0032 %     px = [10 20 20 10 NaN 30 40 40 30]';
0033 %     py = [10 10 20 20 NaN 10 10 20 20]';
0034 %     figure;
0035 %     drawPolygon([px py], 'lineWidth', 2);
0036 %     axis equal; axis([0 50 0 50]);
0037 %
0038 %   See also:
0039 %   polygons2d, drawPolyline
0040 
0041 %   ---------
0042 %   author : David Legland
0043 %   INRA - TPV URPOI - BIA IMASTE
0044 %   created the 05/05/2004.
0045 %
0046 
0047 %   HISTORY
0048 %   2008/10/15 manage polygons with holes
0049 %   2011-10-11 add management of axes handle
0050 %   2016-05-26 Juanpi Carbajal reorganized the function for readability and
0051 %              removed unnecessary variable arguemnts
0052 
0053 % Store hold state
0054 state = ishold(gca);
0055 hold on;
0056 
0057 
0058 %% Check input
0059 if nargin < 1
0060     error ('should specify at least one argument');
0061 end
0062 
0063 % check for empty polygons
0064 if isempty (px)
0065     return
0066 end
0067 
0068 % extract handle of axis to draw on
0069 ax = gca;
0070 if isAxisHandle(px)
0071     ax          = px;
0072     px          = varargin{1};
0073     varargin(1) = [];
0074 end
0075 
0076 
0077 %% Manage cell arrays of polygons
0078 
0079 % case of a set of polygons stored in a cell array
0080 if iscell(px)
0081     h   = cellfun(@(x) drawPolygon(ax, x, varargin{:}), px, 'UniformOutput', 0);
0082     h   = horzcat(h{:});
0083     
0084 else
0085     % Check size vs number of arguments
0086     if size(px, 2) == 2
0087         % Case of polygon specified as a N-by-2 array (most standard case)
0088         py = px(:, 2);
0089         px = px(:, 1);
0090         
0091     elseif size(px, 2) == 1
0092         % Case of polygon specified as two N-by-1 arrays with same length
0093         if nargin < 2 || nargin == 2 && ~isnumeric(varargin{1})
0094             error('Matgeom:invalid_input_arg', ...
0095                 'Should specify either a N-by-2 array, or 2 N-by-1 vectors');
0096         end
0097         
0098         % Extract coordinates of polygon vertices
0099         py          = varargin{1};
0100         varargin(1) = [];
0101         
0102         if length(py) ~= length(px)
0103             error('Matgeom:invalid_input_arg', ...
0104                 'X and Y coordinate arrays should have same lengths (%d,%d)', ...
0105                 length(px), length(py)) 
0106         end
0107         
0108     else
0109         error('Matgeom:invalid_input_arg', 'Should specify a N-by-2 array');
0110     end
0111     
0112     % set default line format
0113     if isempty (varargin)
0114         varargin = {'b-'};
0115     end
0116    
0117     % Check case of polygons with holes
0118     if any (isnan (px(:)) )
0119         polygons = splitPolygons ([px py]);
0120         h        = drawPolygon (ax, polygons, varargin{:});
0121         
0122     else
0123         % ensure last point is the same as the first one
0124         px(end+1, :) = px(1,:);
0125         py(end+1, :) = py(1,:);
0126 
0127         % draw the polygon outline
0128         h = plot(ax, px, py, varargin{:});
0129         
0130     end % whether there where holes
0131     
0132 end % whether input arg was a cell
0133 
0134 if ~state
0135     hold off
0136 end
0137 
0138 % avoid returning argument if not required
0139 if nargout > 0
0140     varargout = {h};
0141 end

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