ELLIPSETOPOLYGON Convert an ellipse into a series of points. P = ellipseToPolygon(ELL, N); converts ELL given as [x0 y0 a b] or [x0 y0 a b theta] into a polygon with N edges. The result P is a N-by-2 array containing the coordinates of the N vertices of the polygon. P = ellipseToPolygon(ELL); Use a default number of edges equal to 72. This results in one point for each 5 degrees. [X, Y] = ellipseToPolygon(...); Return the coordinates of vertices in two separate arrays. Example poly = ellipseToPolygon([50 50 40 30 20], 60); figure; hold on; axis equal; axis([0 100 10 90]); drawPolygon(poly, 'b'); drawPoint(poly, 'bo'); See also: ellipses2d, drawEllipse, circleToPolygon, rectToPolygon
0001 function varargout = ellipseToPolygon(ellipse, N) 0002 %ELLIPSETOPOLYGON Convert an ellipse into a series of points. 0003 % 0004 % P = ellipseToPolygon(ELL, N); 0005 % converts ELL given as [x0 y0 a b] or [x0 y0 a b theta] into a polygon 0006 % with N edges. The result P is a N-by-2 array containing the coordinates 0007 % of the N vertices of the polygon. 0008 % 0009 % P = ellipseToPolygon(ELL); 0010 % Use a default number of edges equal to 72. This results in one point 0011 % for each 5 degrees. 0012 % 0013 % [X, Y] = ellipseToPolygon(...); 0014 % Return the coordinates of vertices in two separate arrays. 0015 % 0016 % Example 0017 % poly = ellipseToPolygon([50 50 40 30 20], 60); 0018 % figure; hold on; 0019 % axis equal; axis([0 100 10 90]); 0020 % drawPolygon(poly, 'b'); 0021 % drawPoint(poly, 'bo'); 0022 % 0023 % See also: 0024 % ellipses2d, drawEllipse, circleToPolygon, rectToPolygon 0025 % 0026 0027 % --------- 0028 % author : David Legland 0029 % INRA - TPV URPOI - BIA IMASTE 0030 % created the 06/04/2005. 0031 % 0032 0033 % HISTORY 0034 % 2011-03-30 use angles in degrees, add default value for N 0035 % 2011-12-09 rename to ellipseToPolygon 0036 % 2017-08-31 returns N vertices instead of N+1 0037 0038 % default value for N 0039 if nargin < 2 0040 N = 72; 0041 end 0042 0043 % angle of ellipse 0044 theta = 0; 0045 if size(ellipse, 2) > 4 0046 theta = ellipse(:,5); 0047 end 0048 0049 % get ellipse parameters 0050 xc = ellipse(:,1); 0051 yc = ellipse(:,2); 0052 a = ellipse(:,3); 0053 b = ellipse(:,4); 0054 0055 % create time basis 0056 t = linspace(0, 2*pi, N+1)'; 0057 t(end) = []; 0058 0059 % pre-compute trig functions (angles is in degrees) 0060 cot = cosd(theta); 0061 sit = sind(theta); 0062 0063 % position of points 0064 x = xc + a * cos(t) * cot - b * sin(t) * sit; 0065 y = yc + a * cos(t) * sit + b * sin(t) * cot; 0066 0067 % format output depending on number of a param. 0068 if nargout == 1 0069 varargout = {[x y]}; 0070 elseif nargout == 2 0071 varargout = {x, y}; 0072 end