CIRCLETOPOLYGON Convert a circle into a series of points. PTS = circleToPolygon(CIRC, N); Converts the circle CIRC into a N-by-2 numeric array, containing the x and y positions of vertices. CIRC is given as [x0 y0 r], where x0 and y0 are coordinate of center, and r is the radius. If CIRC is a N-by-4 array, the fourth value is used as the angle (with respect to the x axis) of the first vertex, in radians. Default value is 0, corresponding to initial vertex with right-most position. P = circleToPolygon(CIRC); uses a default value of N=64 vertices. Example % simple example poly = circleToPolygon([30 20 15], 16); figure; hold on; axis equal;axis([0 50 0 50]); drawPolygon(poly, 'b'); drawPoint(poly, 'bo'); % add a rotation angle to the first vertex position figure; hold on; axis equal; axis([-12 12 -12 12]); poly = circleToPolygon([0 0 10 pi/4], 10); drawPolygon(poly, 'b'); drawVertices(poly); See also circles2d, polygons2d, circleArcToPolyline, ellipseToPolygon
0001 function varargout = circleToPolygon(circle, varargin) 0002 %CIRCLETOPOLYGON Convert a circle into a series of points. 0003 % 0004 % PTS = circleToPolygon(CIRC, N); 0005 % Converts the circle CIRC into a N-by-2 numeric array, containing the x 0006 % and y positions of vertices. 0007 % CIRC is given as [x0 y0 r], where x0 and y0 are coordinate of center, 0008 % and r is the radius. 0009 % 0010 % If CIRC is a N-by-4 array, the fourth value is used as the angle (with 0011 % respect to the x axis) of the first vertex, in radians. Default value 0012 % is 0, corresponding to initial vertex with right-most position. 0013 % 0014 % 0015 % P = circleToPolygon(CIRC); 0016 % uses a default value of N=64 vertices. 0017 % 0018 % Example 0019 % % simple example 0020 % poly = circleToPolygon([30 20 15], 16); 0021 % figure; hold on; 0022 % axis equal;axis([0 50 0 50]); 0023 % drawPolygon(poly, 'b'); 0024 % drawPoint(poly, 'bo'); 0025 % 0026 % % add a rotation angle to the first vertex position 0027 % figure; hold on; axis equal; axis([-12 12 -12 12]); 0028 % poly = circleToPolygon([0 0 10 pi/4], 10); 0029 % drawPolygon(poly, 'b'); drawVertices(poly); 0030 % 0031 % See also 0032 % circles2d, polygons2d, circleArcToPolyline, ellipseToPolygon 0033 % 0034 0035 % ------ 0036 % Author: David Legland 0037 % E-mail: david.legland@inrae.fr 0038 % Created: 2005-04-06 0039 % Copyright 2005-2024 INRAE - BIA Research Unit - BIBS Platform (Nantes) 0040 0041 % check input size 0042 if size(circle, 1) > 1 0043 error('require circle to be 1-by-3 numeric array'); 0044 end 0045 0046 % determines number of points 0047 N = 64; 0048 if ~isempty(varargin) 0049 N = varargin{1}; 0050 end 0051 0052 % angular shift for initial vertex 0053 t0 = 0; 0054 if size(circle, 2) > 3 0055 t0 = circle(1, 4); 0056 end 0057 0058 % create circle 0059 t = linspace(0, 2*pi, N+1)' + t0; 0060 t(end) = []; 0061 0062 % coordinates of circle points 0063 x = circle(1) + circle(3) * cos(t); 0064 y = circle(2) + circle(3) * sin(t); 0065 0066 % format output 0067 if nargout == 1 0068 varargout{1} = [x y]; 0069 elseif nargout == 2 0070 varargout{1} = x; 0071 varargout{2} = y; 0072 end