Home > matGeom > geom2d > drawEllipse.m

drawEllipse

PURPOSE ^

DRAWELLIPSE Draw an ellipse on the current axis.

SYNOPSIS ^

function varargout = drawEllipse(varargin)

DESCRIPTION ^

DRAWELLIPSE Draw an ellipse on the current axis.

   drawEllipse(ELLI);
   Draws the ellipse ELLI in the form [XC YC RA RB THETA], with center
   (XC, YC), with main axis of half-length RA and RB, and orientation
   THETA in degrees counted counter-clockwise.

   drawEllipse(XC, YC, RA, RB);
   drawEllipse(XC, YC, RA, RB, THETA);
   Specifies ellipse parameters as separate arguments (old syntax).

   drawEllipse(..., NAME, VALUE);
   Specifies drawing style of ellipse, see the help of plot function.

   H = drawEllipse(...);
   Also returns handles to the created line objects.

   -> Parameters can also be arrays. In this case, all arrays are supposed 
   to have the same size.

   Example:
   % Draw an ellipse centered in [50 50], with semi major axis length of
   % 40, semi minor axis length of 20, and rotated by 30 degrees.
     figure(1); clf; hold on;
     drawEllipse([50 50 40 20 30]);
     axis equal; axis([0 100 10 90])

   % add another ellipse with different orientation and style
     drawEllipse([50 50 40 20 -10], 'linewidth', 2, 'color', 'g');

   See also:
     ellipses2d, drawCircle, drawEllipseArc, ellipseToPolygon

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawEllipse(varargin)
0002 %DRAWELLIPSE Draw an ellipse on the current axis.
0003 %
0004 %   drawEllipse(ELLI);
0005 %   Draws the ellipse ELLI in the form [XC YC RA RB THETA], with center
0006 %   (XC, YC), with main axis of half-length RA and RB, and orientation
0007 %   THETA in degrees counted counter-clockwise.
0008 %
0009 %   drawEllipse(XC, YC, RA, RB);
0010 %   drawEllipse(XC, YC, RA, RB, THETA);
0011 %   Specifies ellipse parameters as separate arguments (old syntax).
0012 %
0013 %   drawEllipse(..., NAME, VALUE);
0014 %   Specifies drawing style of ellipse, see the help of plot function.
0015 %
0016 %   H = drawEllipse(...);
0017 %   Also returns handles to the created line objects.
0018 %
0019 %   -> Parameters can also be arrays. In this case, all arrays are supposed
0020 %   to have the same size.
0021 %
0022 %   Example:
0023 %   % Draw an ellipse centered in [50 50], with semi major axis length of
0024 %   % 40, semi minor axis length of 20, and rotated by 30 degrees.
0025 %     figure(1); clf; hold on;
0026 %     drawEllipse([50 50 40 20 30]);
0027 %     axis equal; axis([0 100 10 90])
0028 %
0029 %   % add another ellipse with different orientation and style
0030 %     drawEllipse([50 50 40 20 -10], 'linewidth', 2, 'color', 'g');
0031 %
0032 %   See also:
0033 %     ellipses2d, drawCircle, drawEllipseArc, ellipseToPolygon
0034 %
0035 
0036 %   ---------
0037 %   author : David Legland
0038 %   e-mail: david.legland@inra.fr
0039 %   INRA - TPV URPOI - BIA IMASTE
0040 %   created the 11/12/2003.
0041 %
0042 
0043 %   HISTORY
0044 %   2004-01-08 returns coord of points when 2 output args are asked
0045 %   2004-01-08 fix bug in extraction of input parameters, theta was not
0046 %       initialized in case of array of size 1*5
0047 %   2005-08-13 uses radians instead of degrees
0048 %   2008-02-21 add support for drawing styles, code cleanup
0049 %   2011-03-30 use degrees instead of radians, remove [x y] = ... format
0050 %   2011-10-11 add support for axis handle
0051 
0052 
0053 %% Extract input arguments
0054 
0055 % extract handle of axis to draw on
0056 if isAxisHandle(varargin{1})
0057     ax = varargin{1};
0058     varargin(1) = [];
0059 else
0060     ax = gca;
0061 end
0062 
0063 % extract dawing style strings
0064 styles = {};
0065 for i = 1:length(varargin)
0066     if ischar(varargin{i})
0067         styles = varargin(i:end);
0068         varargin(i:end) = [];
0069         break;
0070     end
0071 end
0072 
0073 % extract ellipse parameters
0074 if length(varargin) == 1
0075     % ellipse is given in a single array
0076     ellipse = varargin{1};
0077     x0 = ellipse(:, 1);
0078     y0 = ellipse(:, 2);
0079     a  = ellipse(:, 3);
0080     b  = ellipse(:, 4);
0081     if length(ellipse) > 4
0082         theta = ellipse(:, 5);
0083     else
0084         theta = zeros(size(x0));
0085     end
0086     
0087 elseif length(varargin) >= 4
0088     % ellipse parameters given as separate arrays
0089     x0 = varargin{1};
0090     y0 = varargin{2};
0091     a  = varargin{3};
0092     b  = varargin{4};
0093     if length(varargin) > 4
0094         theta = varargin{5};
0095     else
0096         theta = zeros(size(x0));
0097     end
0098     
0099 else
0100     error('drawEllipse: incorrect input arguments');
0101 end
0102 
0103 
0104 %% Process drawing of a set of ellipses
0105 
0106 % angular positions of vertices
0107 t = linspace(0, 2*pi, 145);
0108 
0109 % compute position of points to draw each ellipse
0110 h = zeros(length(x0), 1);
0111 for i = 1:length(x0)
0112     % pre-compute rotation angles (given in degrees)
0113     cot = cosd(theta(i));
0114     sit = sind(theta(i));
0115     
0116     % compute position of points used to draw current ellipse
0117     xt = x0(i) + a(i) * cos(t) * cot - b(i) * sin(t) * sit;
0118     yt = y0(i) + a(i) * cos(t) * sit + b(i) * sin(t) * cot;
0119     
0120     % stores handle to graphic object
0121     h(i) = plot(ax, xt, yt, styles{:});
0122 end
0123 
0124 % return handles if required
0125 if nargout > 0
0126     varargout = {h};
0127 end
0128

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