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
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 % E-mail: david.legland@inrae.fr 0044 % Created: 2004-05-05 0045 % Copyright 2004-2024 INRA - TPV URPOI - BIA IMASTE 0046 0047 % Store hold state 0048 state = ishold(gca); 0049 hold on; 0050 0051 0052 %% Check input 0053 if nargin < 1 0054 error ('should specify at least one argument'); 0055 end 0056 0057 % check for empty polygons 0058 if isempty (px) 0059 return 0060 end 0061 0062 % extract handle of axis to draw on 0063 ax = gca; 0064 if isAxisHandle(px) 0065 ax = px; 0066 px = varargin{1}; 0067 varargin(1) = []; 0068 end 0069 0070 0071 %% Manage cell arrays of polygons 0072 0073 % case of a set of polygons stored in a cell array 0074 if iscell(px) 0075 h = cellfun(@(x) drawPolygon(ax, x, varargin{:}), px, 'UniformOutput', 0); 0076 h = horzcat(h{:}); 0077 0078 else 0079 % Check size vs number of arguments 0080 if size(px, 2) == 2 0081 % Case of polygon specified as a N-by-2 array (most standard case) 0082 py = px(:, 2); 0083 px = px(:, 1); 0084 0085 elseif size(px, 2) == 1 0086 % Case of polygon specified as two N-by-1 arrays with same length 0087 if nargin < 2 || nargin == 2 && ~isnumeric(varargin{1}) 0088 error('Matgeom:invalid_input_arg', ... 0089 'Should specify either a N-by-2 array, or 2 N-by-1 vectors'); 0090 end 0091 0092 % Extract coordinates of polygon vertices 0093 py = varargin{1}; 0094 varargin(1) = []; 0095 0096 if length(py) ~= length(px) 0097 error('Matgeom:invalid_input_arg', ... 0098 'X and Y coordinate arrays should have same lengths (%d,%d)', ... 0099 length(px), length(py)) 0100 end 0101 0102 else 0103 error('Matgeom:invalid_input_arg', 'Should specify a N-by-2 array'); 0104 end 0105 0106 % set default line format 0107 if isempty (varargin) 0108 varargin = {'b-'}; 0109 end 0110 0111 % Check case of polygons with holes 0112 if any (isnan (px(:)) ) 0113 polygons = splitPolygons ([px py]); 0114 h = drawPolygon (ax, polygons, varargin{:}); 0115 0116 else 0117 % ensure last point is the same as the first one 0118 px(end+1, :) = px(1,:); 0119 py(end+1, :) = py(1,:); 0120 0121 % draw the polygon outline 0122 h = plot(ax, px, py, varargin{:}); 0123 0124 end % whether there where holes 0125 0126 end % whether input arg was a cell 0127 0128 if ~state 0129 hold off 0130 end 0131 0132 % avoid returning argument if not required 0133 if nargout > 0 0134 varargout = {h}; 0135 end