Home > matGeom > geom2d > circleToPolygon.m

circleToPolygon

PURPOSE ^

CIRCLETOPOLYGON Convert a circle into a series of points.

SYNOPSIS ^

function varargout = circleToPolygon(circle, varargin)

DESCRIPTION ^

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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 % INRAE - BIA Research Unit - BIBS Platform (Nantes)
0039 % Created: 2005-04-06
0040 % Copyright 2022 INRAE.
0041 
0042 % HISTORY
0043 % 2007-04-20 return a closed polygon with N+1 vertices, use default N=64
0044 % 2011-12-09 rename to 'circleToPolygon'
0045 % 2017-08-31 returns N vertices instead of N+1
0046 
0047 % check input size
0048 if size(circle, 1) > 1
0049     error('require circle to be 1-by-3 numeric array');
0050 end
0051 
0052 % determines number of points
0053 N = 64;
0054 if ~isempty(varargin)
0055     N = varargin{1};
0056 end
0057 
0058 % angular shift for initial vertex
0059 t0 = 0;
0060 if size(circle, 2) > 3
0061     t0 = circle(1, 4);
0062 end
0063 
0064 % create circle
0065 t = linspace(0, 2*pi, N+1)' + t0;
0066 t(end) = [];
0067 
0068 % coordinates of circle points
0069 x = circle(1) + circle(3) * cos(t);
0070 y = circle(2) + circle(3) * sin(t);
0071 
0072 % format output
0073 if nargout == 1
0074     varargout{1} = [x y];
0075 elseif nargout == 2
0076     varargout{1} = x;
0077     varargout{2} = y;    
0078 end

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