DRAWCIRCLE Draw a circle on the current axis. drawCircle(X0, Y0, R); Draw the circle with center (X0,Y0) and the radius R. If X0, Y0 and R are column vectors of the same length, draw each circle successively. drawCircle(CIRCLE); Concatenate all parameters in a Nx3 array, where N is the number of circles to draw. drawCircle(CENTER, RADIUS); Specify CENTER as Nx2 array, and radius as a Nx1 array. drawCircle(..., NSTEP); Specify the number of edges that will be used to draw the circle. Default value is 72, creating an approximation of one point for each 5 degrees. drawCircle(..., NAME, VALUE); Specifies plotting options as pair of parameters name/value. See plot documentation for details. drawCircle(AX, ...) Specifies the handle of the axis to draw on. H = drawCircle(...); return handles to each created curve. Example figure; hold on; drawCircle([10 20 30]); drawCircle([15 15 40], 'color', 'r', 'linewidth', 2); axis equal; See also circles2d, drawCircleArc, drawEllipse, circleToPolygon
0001 function varargout = drawCircle(varargin) 0002 %DRAWCIRCLE Draw a circle on the current axis. 0003 % 0004 % drawCircle(X0, Y0, R); 0005 % Draw the circle with center (X0,Y0) and the radius R. If X0, Y0 and R 0006 % are column vectors of the same length, draw each circle successively. 0007 % 0008 % drawCircle(CIRCLE); 0009 % Concatenate all parameters in a Nx3 array, where N is the number of 0010 % circles to draw. 0011 % 0012 % drawCircle(CENTER, RADIUS); 0013 % Specify CENTER as Nx2 array, and radius as a Nx1 array. 0014 % 0015 % drawCircle(..., NSTEP); 0016 % Specify the number of edges that will be used to draw the circle. 0017 % Default value is 72, creating an approximation of one point for each 5 0018 % degrees. 0019 % 0020 % drawCircle(..., NAME, VALUE); 0021 % Specifies plotting options as pair of parameters name/value. See plot 0022 % documentation for details. 0023 % 0024 % drawCircle(AX, ...) 0025 % Specifies the handle of the axis to draw on. 0026 % 0027 % H = drawCircle(...); 0028 % return handles to each created curve. 0029 % 0030 % Example 0031 % figure; 0032 % hold on; 0033 % drawCircle([10 20 30]); 0034 % drawCircle([15 15 40], 'color', 'r', 'linewidth', 2); 0035 % axis equal; 0036 % 0037 % See also 0038 % circles2d, drawCircleArc, drawEllipse, circleToPolygon 0039 % 0040 0041 % ------ 0042 % Author: David Legland 0043 % E-mail: david.legland@inrae.fr 0044 % Created: 2003-10-31 0045 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE 0046 0047 %% Parse input arguments 0048 0049 % extract handle of axis to draw on 0050 [ax, varargin] = parseAxisHandle(varargin{:}); 0051 0052 % process input parameters 0053 var = varargin{1}; 0054 if size(var, 2) == 1 0055 x0 = varargin{1}; 0056 y0 = varargin{2}; 0057 r = varargin{3}; 0058 varargin(1:3) = []; 0059 0060 elseif size(var, 2) == 2 0061 x0 = var(:,1); 0062 y0 = var(:,2); 0063 r = varargin{2}; 0064 varargin(1:2) = []; 0065 0066 elseif size(var, 2) == 3 0067 x0 = var(:,1); 0068 y0 = var(:,2); 0069 r = var(:,3); 0070 varargin(1) = []; 0071 else 0072 error('bad format for input in drawCircle'); 0073 end 0074 0075 % default number of discretization steps 0076 N = 72; 0077 0078 % check if discretization step is specified 0079 if ~isempty(varargin) 0080 var = varargin{1}; 0081 if isnumeric(var) && isscalar(var) 0082 N = round(var); 0083 varargin(1) = []; 0084 end 0085 end 0086 0087 0088 %% Pre-processing 0089 0090 % ensure each parameter is column vector 0091 x0 = x0(:); 0092 y0 = y0(:); 0093 r = r(:); 0094 0095 % parametrization variable for circle (use N+1 as first point counts twice) 0096 t = linspace(0, 2*pi, N+1); 0097 cot = cos(t); 0098 sit = sin(t); 0099 0100 % empty array for graphic handles 0101 h = zeros(size(x0)); 0102 0103 % save hold state 0104 holdState = ishold(ax); 0105 hold(ax, 'on'); 0106 0107 0108 %% Display each circle 0109 0110 % compute discretization of each circle 0111 for i = 1:length(x0) 0112 xt = x0(i) + r(i) * cot; 0113 yt = y0(i) + r(i) * sit; 0114 0115 h(i) = plot(ax, xt, yt, varargin{:}); 0116 end 0117 0118 0119 %% Post-processing 0120 0121 % restore hold state 0122 if ~holdState 0123 hold(ax, 'off'); 0124 end 0125 0126 if nargout > 0 0127 varargout = {h}; 0128 end