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, drawEllipseAxes
     fitEllipse, ellipseToPolygon, ellipsePoint, transformEllipse

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 %
0033 %   See also
0034 %     ellipses2d, drawCircle, drawEllipseArc, drawEllipseAxes
0035 %     fitEllipse, ellipseToPolygon, ellipsePoint, transformEllipse
0036 %
0037 
0038 % ------
0039 % Author: David Legland
0040 % E-mail: david.legland@inrae.fr
0041 % Created: 2003-12-11
0042 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE
0043 
0044 %% Extract input arguments
0045 
0046 % extract handle of axis to draw on
0047 if isAxisHandle(varargin{1})
0048     ax = varargin{1};
0049     varargin(1) = [];
0050 else
0051     ax = gca;
0052 end
0053 
0054 % extract dawing style strings
0055 styles = {};
0056 for i = 1:length(varargin)
0057     if ischar(varargin{i})
0058         styles = varargin(i:end);
0059         varargin(i:end) = [];
0060         break;
0061     end
0062 end
0063 
0064 % extract ellipse parameters
0065 if isscalar(varargin)
0066     % ellipse is given in a single array
0067     ellipse = varargin{1};
0068     x0 = ellipse(:, 1);
0069     y0 = ellipse(:, 2);
0070     a  = ellipse(:, 3);
0071     b  = ellipse(:, 4);
0072     if length(ellipse) > 4
0073         theta = ellipse(:, 5);
0074     else
0075         theta = zeros(size(x0));
0076     end
0077     
0078 elseif length(varargin) >= 4
0079     % ellipse parameters given as separate arrays
0080     x0 = varargin{1};
0081     y0 = varargin{2};
0082     a  = varargin{3};
0083     b  = varargin{4};
0084     if length(varargin) > 4
0085         theta = varargin{5};
0086     else
0087         theta = zeros(size(x0));
0088     end
0089     
0090 else
0091     error('drawEllipse: incorrect input arguments');
0092 end
0093 
0094 
0095 %% Process drawing of a set of ellipses
0096 
0097 % angular positions of vertices
0098 t = linspace(0, 2*pi, 145);
0099 
0100 % empty array for graphic handles
0101 h = zeros(length(x0), 1);
0102 
0103 % save hold state
0104 holdState = ishold(ax);
0105 hold(ax, 'on');
0106 
0107 
0108 %% Display each ellipse
0109 
0110 for i = 1:length(x0)
0111     % pre-compute rotation angles (given in degrees)
0112     cot = cosd(theta(i));
0113     sit = sind(theta(i));
0114     
0115     % compute position of points used to draw current ellipse
0116     xt = x0(i) + a(i) * cos(t) * cot - b(i) * sin(t) * sit;
0117     yt = y0(i) + a(i) * cos(t) * sit + b(i) * sin(t) * cot;
0118     
0119     % stores handle to graphic object
0120     h(i) = plot(ax, xt, yt, styles{:});
0121 end
0122 
0123 
0124 %% post-processing
0125 
0126 % restore hold state
0127 if ~holdState
0128     hold(ax, 'off');
0129 end
0130 
0131 % return handles if required
0132 if nargout > 0
0133     varargout = {h};
0134 end
0135

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