Home > matGeom > geom3d > drawPolygon3d.m

drawPolygon3d

PURPOSE ^

DRAWPOLYGON3D Draw a 3D polygon specified by a list of vertex coords.

SYNOPSIS ^

function varargout = drawPolygon3d(varargin)

DESCRIPTION ^

DRAWPOLYGON3D Draw a 3D polygon specified by a list of vertex coords.

   drawPolygon3d(POLY);
   packs coordinates in a single N-by-3 array.

   drawPolygon3d(PX, PY, PZ);
   specifies coordinates in separate numeric vectors (either row or
   columns)

   drawPolygon3d(..., PARAM, VALUE);
   Specifies style options to draw the polyline, see plot for details.

   H = drawPolygon3d(...);
   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; drawPolygon3d(xt, yt, zt, 'b');
 
   See Also:
   polygons3d, fillPolygon3d, drawPolyline3d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawPolygon3d(varargin)
0002 %DRAWPOLYGON3D Draw a 3D polygon specified by a list of vertex coords.
0003 %
0004 %   drawPolygon3d(POLY);
0005 %   packs coordinates in a single N-by-3 array.
0006 %
0007 %   drawPolygon3d(PX, PY, PZ);
0008 %   specifies coordinates in separate numeric vectors (either row or
0009 %   columns)
0010 %
0011 %   drawPolygon3d(..., PARAM, VALUE);
0012 %   Specifies style options to draw the polyline, see plot for details.
0013 %
0014 %   H = drawPolygon3d(...);
0015 %   also returns a handle to the list of created line objects.
0016 %
0017 %   Example
0018 %     t = linspace(0, 2*pi, 100)';
0019 %     xt = 10 * cos(t);
0020 %     yt = 5 * sin(t);
0021 %     zt = zeros(1,100);
0022 %     figure; drawPolygon3d(xt, yt, zt, 'b');
0023 %
0024 %   See Also:
0025 %   polygons3d, fillPolygon3d, drawPolyline3d
0026 %
0027 
0028 % ------
0029 % Author: David Legland
0030 % e-mail: david.legland@inra.fr
0031 % Created: 2011-08-17 from drawPolyline3d, using Matlab 7.9.0.529 (R2009b)
0032 % Copyright 2011 INRA - Cepia Software Platform.
0033 
0034 % HISTORY
0035 % 2019-02-02 add support for multiple polygons
0036 
0037 
0038 %% Process input arguments
0039 
0040 % extract handle of axis to draw on
0041 ax = gca;
0042 var1 = varargin{1};
0043 if isAxisHandle (var1)
0044     ax          = var1;
0045     varargin(1) = [];
0046 end
0047 
0048 % check case we want to draw several curves, stored in a cell array
0049 var1 = varargin{1};
0050 if iscell(var1)
0051     hold on;
0052     h = [];
0053     for i = 1:length(var1(:))
0054         h = [h; drawPolygon3d(ax, var1{i}, varargin{2:end})]; %#ok<AGROW>
0055     end
0056     if nargout > 0
0057         varargout{1} = h;
0058     end
0059     return;
0060 end
0061 
0062 %% extract polygon coordinates
0063 if min(size(var1)) == 1
0064     % if first argument is a vector (either row or column), then assumes
0065     % first argument contains x coords, second argument contains y coords
0066     % and third one the z coords
0067     px = var1;
0068     if length(varargin) < 3
0069         error('geom3d:drawPolygon3d:Wrong number of arguments in fillPolygon3d');
0070     end
0071     py = varargin{2};
0072     pz = varargin{3};
0073     varargin = varargin(4:end);
0074 else
0075     % first argument contains both coordinate
0076     px = var1(:, 1);
0077     py = var1(:, 2);
0078     pz = var1(:, 3);
0079     varargin = varargin(2:end);
0080 end
0081 
0082 
0083 %% draw the polygon
0084 
0085 % check that the polygon is closed
0086 if px(1) ~= px(end) || py(1) ~= py(end) || pz(1) ~= pz(end)
0087     px = [px(:); px(1)];
0088     py = [py(:); py(1)];
0089     pz = [pz(:); pz(1)];
0090 end
0091 
0092 % draw the closed curve
0093 h = plot3(ax, px, py, pz, varargin{:});
0094 
0095 
0096 %% Format output
0097 
0098 if nargout > 0
0099     varargout = {h};
0100 end

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