Home > matGeom > polygons2d > fillPolygon.m

fillPolygon

PURPOSE ^

FILLPOLYGON Fill a polygon specified by a list of points.

SYNOPSIS ^

function varargout = fillPolygon(varargin)

DESCRIPTION ^

FILLPOLYGON Fill a polygon specified by a list of points.

   fillPolygon(POLY);
   Fills the interior of the polygon specified by POLY. The boundary of
   the polygon is not drawn, see 'drawPolygon' to do it.
   POLY is a single [N*2] array.
   If POLY contains NaN-couples, each portion between the [NaN;NaN] will
   be filled separately.

   fillPolygon(PX, PY);
   Specifies coordinates of the polygon in separate arrays.


   H = fillPolygon(...);
   Also returns a handle to the created patch


   See also:
     polygons2d, drawCurve, drawPolygon

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = fillPolygon(varargin)
0002 %FILLPOLYGON Fill a polygon specified by a list of points.
0003 %
0004 %   fillPolygon(POLY);
0005 %   Fills the interior of the polygon specified by POLY. The boundary of
0006 %   the polygon is not drawn, see 'drawPolygon' to do it.
0007 %   POLY is a single [N*2] array.
0008 %   If POLY contains NaN-couples, each portion between the [NaN;NaN] will
0009 %   be filled separately.
0010 %
0011 %   fillPolygon(PX, PY);
0012 %   Specifies coordinates of the polygon in separate arrays.
0013 %
0014 %
0015 %   H = fillPolygon(...);
0016 %   Also returns a handle to the created patch
0017 %
0018 %
0019 %   See also:
0020 %     polygons2d, drawCurve, drawPolygon
0021 
0022 %   ---------
0023 %   author : David Legland
0024 %   INRA - TPV URPOI - BIA IMASTE
0025 %   created the 07/04/2005.
0026 %
0027 
0028 %   HISTORY
0029 %   2008-05-07 add psb to specify drawing options
0030 %   2008/10/15 add psb to draw polygons with holes
0031 
0032 % check input
0033 if isempty(varargin)
0034     error('need to specify a polygon');
0035 end
0036 
0037 % case of a set of polygons stored in a cell array
0038 var = varargin{1};
0039 if iscell(var)
0040     N = length(var);
0041     h = zeros(N, 1);
0042     for i = 1:N
0043         % check for empty polygons
0044         if ~isempty(var{i})
0045             h(i) = fillPolygon(var{i}, varargin{2:end});
0046         end
0047     end
0048 
0049     % setup output values
0050     if nargout > 0
0051         varargout{1} = h;
0052     end
0053     return;
0054 end
0055 
0056 % Extract coordinates of polygon vertices
0057 if size(var, 2) > 1
0058     % first argument is a polygon array
0059     px = var(:, 1);
0060     py = var(:, 2);
0061     varargin(1) = [];
0062 else
0063     % arguments 1 and 2 correspond to x and y coordinate respectively
0064     if length(varargin) < 2
0065         error('should specify either a N*2 array, or 2 N*1 vectors');
0066     end
0067     
0068     px = varargin{1};
0069     py = varargin{2};
0070     varargin(1:2) = [];
0071 end
0072 
0073 
0074 % Find position of breaks, and copies first point of each loop at the end
0075 inds = find(isnan(px(:)));
0076 i1 = [inds ; length(px)+1];
0077 i0 = [1 ; inds+1];
0078 px(i1, :) = px(i0, :);
0079 py(i1, :) = py(i0, :);
0080 
0081 
0082 % set default line format
0083 if isempty(varargin)
0084     varargin = {'b'};
0085 end
0086 
0087 
0088 % fill the polygon with desired style
0089 h = fill(px, py, varargin{:}, 'lineStyle', 'none');
0090 
0091 % output
0092 if nargout > 0
0093     varargout{1} = h;
0094 end

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