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@inra.fr 0037 % Created: 2005-02-15 0038 % Copyright 2005 INRA - TPV URPOI - BIA IMASTE 0039 0040 % HISTORY 0041 % 2010-03-08 rename to drawPolyline3d 0042 0043 0044 %% Process input arguments 0045 hAx = gca; 0046 if isAxisHandle(varargin{1}) 0047 hAx = varargin{1}; 0048 varargin(1) = []; 0049 end 0050 0051 % check case we want to draw several curves, stored in a cell array 0052 var = varargin{1}; 0053 if iscell(var) 0054 hold on; 0055 h = zeros(length(var(:)), 1); 0056 for i = 1:length(var(:)) 0057 h(i) = drawPolyline3d(hAx, var{i}, varargin{2:end}); 0058 end 0059 if nargout > 0 0060 varargout = {h}; 0061 end 0062 return; 0063 end 0064 0065 % extract curve coordinates 0066 if min(size(var)) == 1 0067 % if first argument is a vector (either row or column), then assumes 0068 % first argument contains x coords, second argument contains y coords 0069 % and third one the z coords 0070 px = var; 0071 if length(varargin) < 3 0072 error('geom3d:drawPolyline3d:Wrong number of arguments in drawPolyline3d'); 0073 end 0074 if isnumeric(varargin{2}) && isnumeric(varargin{3}) 0075 py = varargin{2}; 0076 pz = varargin{3}; 0077 varargin(1:3) = []; 0078 else 0079 px = var(:, 1); 0080 py = var(:, 2); 0081 pz = var(:, 3); 0082 varargin(1) = []; 0083 end 0084 else 0085 % all coordinates are grouped in the first argument 0086 px = var(:, 1); 0087 py = var(:, 2); 0088 pz = var(:, 3); 0089 varargin(1) = []; 0090 end 0091 0092 % check if curve is closed or open (default is open) 0093 closed = false; 0094 if ~isempty(varargin) 0095 var = varargin{1}; 0096 if islogical(var) 0097 % check boolean flag 0098 closed = var; 0099 varargin = varargin(2:end); 0100 0101 elseif ischar(var) 0102 % check string indicating close or open 0103 if strncmpi(var, 'close', 5) 0104 closed = true; 0105 varargin = varargin(2:end); 0106 0107 elseif strncmpi(var, 'open', 4) 0108 closed = false; 0109 varargin = varargin(2:end); 0110 end 0111 0112 end 0113 end 0114 0115 0116 %% draw the curve 0117 0118 % for closed curve, add the first point at the end to close curve 0119 if closed 0120 px = [px(:); px(1)]; 0121 py = [py(:); py(1)]; 0122 pz = [pz(:); pz(1)]; 0123 end 0124 0125 h = plot3(hAx, px, py, pz, varargin{:}); 0126 0127 if nargout > 0 0128 varargout = {h}; 0129 end