Home > matGeom > geom2d > ellipseToPolygon.m

ellipseToPolygon

PURPOSE ^

ELLIPSETOPOLYGON Convert an ellipse into a series of points.

SYNOPSIS ^

function varargout = ellipseToPolygon(ellipse, N)

DESCRIPTION ^

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, ellipsePoint

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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, ellipsePoint
0025 %
0026 
0027 % ------
0028 % Author: David Legland
0029 % E-mail: david.legland@inrae.fr
0030 % Created: 2005-04-06
0031 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE
0032 
0033 % default value for N
0034 if nargin < 2
0035     N = 72;
0036 end
0037 
0038 % angle of ellipse
0039 theta = 0;
0040 if size(ellipse, 2) > 4
0041     theta = ellipse(:,5);
0042 end
0043 
0044 % get ellipse parameters
0045 xc = ellipse(:,1);
0046 yc = ellipse(:,2);
0047 a  = ellipse(:,3);
0048 b  = ellipse(:,4);
0049 
0050 % create time basis
0051 t = linspace(0, 2*pi, N+1)';
0052 t(end) = [];
0053 
0054 % pre-compute trig functions (angles is in degrees)
0055 cot = cosd(theta);
0056 sit = sind(theta);
0057 
0058 % position of points
0059 x = xc + a * cos(t) * cot - b * sin(t) * sit;
0060 y = yc + a * cos(t) * sit + b * sin(t) * cot;
0061 
0062 % format output depending on number of a param.
0063 if nargout == 1
0064     varargout = {[x y]};
0065 elseif nargout == 2
0066     varargout = {x, y};
0067 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022