Home > matGeom > polygons2d > drawPolyline.m

drawPolyline

PURPOSE ^

DRAWPOLYLINE Draw a polyline specified by a list of points.

SYNOPSIS ^

function varargout = drawPolyline(varargin)

DESCRIPTION ^

DRAWPOLYLINE Draw a polyline specified by a list of points.

   drawPolyline(COORD);
   packs coordinates in a single [N*2] array.

   drawPolyline(PX, PY);
   specifies coordinates in separate arrays. PX and PY must be column
   vectors with the same length.

   drawPolyline(..., TYPE);
   where TYPE is either 'closed' or 'open', specifies if last point must
   be connected to the first one ('closed') or not ('open').
   Default is 'open'.

   drawPolyline(..., PARAM, VALUE);
   specify plot options as described for plot command.

   H = drawPolyline(...) also return a handle to the list of line objects.

   Example:
   % Draw a curve representing an ellipse
   t = linspace(0, 2*pi, 100)';
   px = 10*cos(t); py = 5*sin(t);
   drawPolyline([px py], 'closed');
   axis equal;

   % The same, with different drawing options
   drawPolyline([px py], 'closed', 'lineWidth', 2, 'lineStyle', '--');

   See Also:
   polygons2d, drawPolygon

   ---------
   author : David Legland
   INRA - TPV URPOI - BIA IMASTE
   created the 06/04/2004.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawPolyline(varargin)
0002 %DRAWPOLYLINE Draw a polyline specified by a list of points.
0003 %
0004 %   drawPolyline(COORD);
0005 %   packs coordinates in a single [N*2] array.
0006 %
0007 %   drawPolyline(PX, PY);
0008 %   specifies coordinates in separate arrays. PX and PY must be column
0009 %   vectors with the same length.
0010 %
0011 %   drawPolyline(..., TYPE);
0012 %   where TYPE is either 'closed' or 'open', specifies if last point must
0013 %   be connected to the first one ('closed') or not ('open').
0014 %   Default is 'open'.
0015 %
0016 %   drawPolyline(..., PARAM, VALUE);
0017 %   specify plot options as described for plot command.
0018 %
0019 %   H = drawPolyline(...) also return a handle to the list of line objects.
0020 %
0021 %   Example:
0022 %   % Draw a curve representing an ellipse
0023 %   t = linspace(0, 2*pi, 100)';
0024 %   px = 10*cos(t); py = 5*sin(t);
0025 %   drawPolyline([px py], 'closed');
0026 %   axis equal;
0027 %
0028 %   % The same, with different drawing options
0029 %   drawPolyline([px py], 'closed', 'lineWidth', 2, 'lineStyle', '--');
0030 %
0031 %   See Also:
0032 %   polygons2d, drawPolygon
0033 %
0034 %   ---------
0035 %   author : David Legland
0036 %   INRA - TPV URPOI - BIA IMASTE
0037 %   created the 06/04/2004.
0038 %
0039 
0040 %   HISTORY
0041 %   03/01/2007: better processing of input, and update doc (drawing
0042 %       options and CLOSE option)
0043 %   30/04/2009 rename as drawPolyline.
0044 %   2011-10-11 add management of axes handle
0045 
0046 
0047 % extract handle of axis to draw on
0048 if isAxisHandle(varargin{1})
0049     ax = varargin{1};
0050     varargin(1) = [];
0051 else
0052     ax = gca;
0053 end
0054 
0055 % If first argument is a cell array, draw each curve individually,
0056 % and eventually returns handle of each plot.
0057 var = varargin{1};
0058 if iscell(var)
0059     h = [];
0060     for i = 1:length(var(:))
0061         h = [h ; drawPolyline(ax, var{i}, varargin{2:end})]; %#ok<AGROW>
0062     end
0063     if nargout > 0
0064         varargout = {h};
0065     end
0066     return;
0067 end
0068 
0069 % extract curve coordinate
0070 if size(var, 2) == 1
0071     % first argument contains x coord, second argument contains y coord
0072     px = var;
0073     if length(varargin) == 1
0074         error('Wrong number of arguments in drawPolyline');
0075     end
0076     py = varargin{2};
0077     varargin = varargin(3:end);
0078 else
0079     % first argument contains both coordinate
0080     px = var(:, 1);
0081     py = var(:, 2);
0082     varargin = varargin(2:end);
0083 end
0084 
0085 % check if curve is closed or open
0086 closed = false;
0087 if ~isempty(varargin)
0088     var = varargin{1};
0089     if strncmpi(var, 'close', 5)
0090         closed = true;
0091         varargin = varargin(2:end);
0092     elseif strncmpi(var, 'open', 4)
0093         closed = false;
0094         varargin = varargin(2:end);
0095     end
0096 end
0097 
0098 % if curve is closed, add first point at the end of the list
0099 if closed
0100     px = [px; px(1)];
0101     py = [py; py(1)];
0102 end
0103 
0104 % plot the curve, with eventually optional parameters
0105 h = plot(ax, px, py, varargin{:});
0106 
0107 % format output arguments
0108 if nargout > 0
0109     varargout = {h};
0110 end

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