Home > matGeom > geom2d > drawCircle.m

drawCircle

PURPOSE ^

DRAWCIRCLE Draw a circle on the current axis.

SYNOPSIS ^

function varargout = drawCircle(varargin)

DESCRIPTION ^

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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 %   INRA - TPV URPOI - BIA IMASTE
0044 %   created the 31/10/2003.
0045 %
0046 
0047 %   HISTORY
0048 %   02/11/2004: add possibility to draw multiple circles in one call
0049 %   12/01/2005: allow more than 3 parameters
0050 %   26/02/2007: add possibility to specify plot options, number of
0051 %       discretization steps, and circle as center+radius.
0052 %   2011-10-11 add support for axis handle
0053 
0054 % extract handle of axis to draw on
0055 if isAxisHandle(varargin{1})
0056     ax = varargin{1};
0057     varargin(1) = [];
0058 else
0059     ax = gca;
0060 end
0061 
0062 % process input parameters
0063 var = varargin{1};
0064 if size(var, 2) == 1
0065     x0 = varargin{1};
0066     y0 = varargin{2};
0067     r  = varargin{3};
0068     varargin(1:3) = [];
0069     
0070 elseif size(var, 2) == 2
0071     x0 = var(:,1);
0072     y0 = var(:,2);
0073     r  = varargin{2};
0074     varargin(1:2) = [];
0075     
0076 elseif size(var, 2) == 3
0077     x0 = var(:,1);
0078     y0 = var(:,2);
0079     r  = var(:,3);
0080     varargin(1) = [];
0081 else
0082     error('bad format for input in drawCircle');
0083 end
0084 
0085 % ensure each parameter is column vector
0086 x0 = x0(:);
0087 y0 = y0(:);
0088 r = r(:);
0089 
0090 % default number of discretization steps
0091 N = 72;
0092 
0093 % check if discretization step is specified
0094 if ~isempty(varargin)
0095     var = varargin{1};
0096     if isnumeric(var) && isscalar(var)
0097         N = round(var);
0098         varargin(1) = [];
0099     end
0100 end
0101 
0102 % parametrization variable for circle (use N+1 as first point counts twice)
0103 t = linspace(0, 2*pi, N+1);
0104 cot = cos(t);
0105 sit = sin(t);
0106 
0107 % empty array for graphic handles
0108 h = zeros(size(x0));
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 if nargout > 0
0119     varargout = {h};
0120 end

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