DRAWPOLYLINE3D Draw a 3D polyline specified by a list of vertex coords. drawPolyline3d(POLY); packs coordinates in a single N-by-3 array. drawPolyline3d(PX, PY, PZ); specifies coordinates in separate numeric vectors (either row or columns) drawPolyline3d(..., CLOSED); Specifies if the polyline is closed or open. CLOSED can be one of: - 'closed' - 'open' (the default) - a boolean variable with value TRUE for closed polylines. drawPolyline3d(..., PARAM, VALUE); Specifies style options to draw the polyline, see plot for details. H = drawPolyline3d(...); also returns a handle to the list of created line objects. Example t = linspace(0, 2*pi, 100)'; xt = 10 * cos(t); yt = 5 * sin(t); zt = zeros(1,100); figure; drawPolyline3d(xt, yt, zt, 'b'); See also polygons3d, drawPolygon3d, fillPolygon3d
0001 function varargout = drawPolyline3d(varargin) 0002 %DRAWPOLYLINE3D Draw a 3D polyline specified by a list of vertex coords. 0003 % 0004 % drawPolyline3d(POLY); 0005 % packs coordinates in a single N-by-3 array. 0006 % 0007 % drawPolyline3d(PX, PY, PZ); 0008 % specifies coordinates in separate numeric vectors (either row or 0009 % columns) 0010 % 0011 % drawPolyline3d(..., CLOSED); 0012 % Specifies if the polyline is closed or open. CLOSED can be one of: 0013 % - 'closed' 0014 % - 'open' (the default) 0015 % - a boolean variable with value TRUE for closed polylines. 0016 % 0017 % drawPolyline3d(..., PARAM, VALUE); 0018 % Specifies style options to draw the polyline, see plot for details. 0019 % 0020 % H = drawPolyline3d(...); 0021 % also returns a handle to the list of created line objects. 0022 % 0023 % Example 0024 % t = linspace(0, 2*pi, 100)'; 0025 % xt = 10 * cos(t); 0026 % yt = 5 * sin(t); 0027 % zt = zeros(1,100); 0028 % figure; drawPolyline3d(xt, yt, zt, 'b'); 0029 % 0030 % See also 0031 % polygons3d, drawPolygon3d, fillPolygon3d 0032 % 0033 0034 % ------ 0035 % Author : David Legland 0036 % E-mail: david.legland@inrae.fr 0037 % Created: 2005-02-15 0038 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE 0039 0040 %% Process input arguments 0041 0042 % extract handle of axis to draw on 0043 [hAx, varargin] = parseAxisHandle(varargin{:}); 0044 0045 % check case we want to draw several curves, stored in a cell array 0046 var = varargin{1}; 0047 if iscell(var) 0048 hold on; 0049 nPolys = length(var(:)); 0050 h = gobjects(1, nPolys); 0051 for i = 1:nPolys 0052 h(i) = drawPolyline3d(hAx, var{i}, varargin{2:end}); 0053 end 0054 if nargout > 0 0055 varargout = {h}; 0056 end 0057 return; 0058 end 0059 0060 % extract vertex coordinates 0061 if min(size(var)) == 1 0062 % if first argument is a vector (either row or column), then assumes 0063 % first argument contains x coords, second argument contains y coords 0064 % and third one the z coords 0065 px = var; 0066 if length(varargin) < 3 0067 error('geom3d:drawPolyline3d:Wrong number of arguments in drawPolyline3d'); 0068 end 0069 if isnumeric(varargin{2}) && isnumeric(varargin{3}) 0070 py = varargin{2}; 0071 pz = varargin{3}; 0072 varargin(1:3) = []; 0073 else 0074 px = var(:, 1); 0075 py = var(:, 2); 0076 pz = var(:, 3); 0077 varargin(1) = []; 0078 end 0079 else 0080 % all coordinates are grouped in the first argument 0081 px = var(:, 1); 0082 py = var(:, 2); 0083 pz = var(:, 3); 0084 varargin(1) = []; 0085 end 0086 0087 % check if polyline is closed or open (default is open) 0088 closed = false; 0089 if ~isempty(varargin) 0090 var = varargin{1}; 0091 if islogical(var) 0092 % check boolean flag 0093 closed = var; 0094 varargin = varargin(2:end); 0095 0096 elseif ischar(var) 0097 % check string indicating close or open 0098 if strncmpi(var, 'close', 5) 0099 closed = true; 0100 varargin = varargin(2:end); 0101 0102 elseif strncmpi(var, 'open', 4) 0103 closed = false; 0104 varargin = varargin(2:end); 0105 end 0106 0107 end 0108 end 0109 0110 0111 %% draw the polyline curve 0112 0113 % for closed curve, add the first point at the end to close curve 0114 if closed 0115 px = [px(:); px(1)]; 0116 py = [py(:); py(1)]; 0117 pz = [pz(:); pz(1)]; 0118 end 0119 0120 h = plot3(hAx, px, py, pz, varargin{:}); 0121 0122 if nargout > 0 0123 varargout = {h}; 0124 end