DRAWSHAPE Draw various types of shapes (circles, polygons...). drawShape(TYPE, PARAM) Draw the shape of type TYPE, specified by given parameter PARAM. TYPE can be one of {'circle', 'ellipse', 'rect', 'polygon', 'curve'} PARAM depend on the type. For example, if TYPE is 'circle', PARAM will contain [x0 y0 R]. Examples: drawShape('circle', [20 10 30]); Draw circle centered on [20 10] with radius 10. drawShape('rect', [20 20 40 10 pi/3]); Draw rectangle centered on [20 20] with length 40 and width 10, and oriented pi/3 wrt axis Ox. drawShape(..., OPTION) also specifies drawing options. OPTION can be 'draw' (default) or 'fill'.
0001 function varargout = drawShape(varargin) 0002 %DRAWSHAPE Draw various types of shapes (circles, polygons...). 0003 % 0004 % drawShape(TYPE, PARAM) 0005 % Draw the shape of type TYPE, specified by given parameter PARAM. TYPE 0006 % can be one of {'circle', 'ellipse', 'rect', 'polygon', 'curve'} 0007 % PARAM depend on the type. For example, if TYPE is 'circle', PARAM will 0008 % contain [x0 y0 R]. 0009 % 0010 % Examples: 0011 % drawShape('circle', [20 10 30]); 0012 % Draw circle centered on [20 10] with radius 10. 0013 % drawShape('rect', [20 20 40 10 pi/3]); 0014 % Draw rectangle centered on [20 20] with length 40 and width 10, and 0015 % oriented pi/3 wrt axis Ox. 0016 % 0017 % 0018 % drawShape(..., OPTION) 0019 % also specifies drawing options. OPTION can be 'draw' (default) or 0020 % 'fill'. 0021 % 0022 0023 % ------ 0024 % Author: David Legland 0025 % E-mail: david.legland@inrae.fr 0026 % Created: 2005-04-07 0027 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE 0028 0029 % extract handle of axis to draw on 0030 if isAxisHandle(varargin{1}) 0031 ax = varargin{1}; 0032 varargin(1) = []; 0033 else 0034 ax = gca; 0035 end 0036 0037 % extract type and representation of shape 0038 type = varargin{1}; 0039 param = varargin{2}; 0040 varargin(1:2) = []; 0041 0042 0043 if ~iscell(type) 0044 type = {type}; 0045 end 0046 if ~iscell(param) 0047 tmp = cell(1, size(param, 1)); 0048 for i=1:size(param, 1) 0049 tmp{i} = param(i,:); 0050 end 0051 param = tmp; 0052 end 0053 0054 % compute drawing options 0055 option = 'draw'; 0056 if ~isempty(varargin) 0057 var = varargin{1}; 0058 if strcmpi(var, 'fill') 0059 option = 'fill'; 0060 varargin(1) = []; 0061 elseif strcmpi(var, 'draw') 0062 option = 'draw'; 0063 varargin(1) = []; 0064 end 0065 end 0066 0067 0068 % transform each shape into a polygon 0069 shape = cell(1,length(type)); 0070 for i = 1:length(type) 0071 if strcmpi(type{i}, 'circle') 0072 shape{i} = circleToPolygon(param{i}, 128); 0073 elseif strcmpi(type{i}, 'rect') 0074 shape{i} = rectToPolygon(param{i}); 0075 elseif strcmpi(type{i}, 'polygon') 0076 shape{i} = param{i}; 0077 end 0078 end 0079 0080 0081 % draw or fill each shape as polygon 0082 hold on; 0083 h = zeros(length(shape), 1); 0084 if strcmp(option, 'draw') 0085 for i = 1:length(shape) 0086 h(i) = drawPolygon(ax, shape{i}, varargin{:}); 0087 end 0088 else 0089 for i = 1:length(shape) 0090 h(i) = fillPolygon(ax, shape{i}, varargin{:}); 0091 end 0092 end 0093 0094 % format output 0095 if nargout > 0 0096 varargout = {h}; 0097 end