Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of sldrawellipse
Home > sltoolbox > visualize > sldrawellipse.m

sldrawellipse

PURPOSE ^

SLDRAWELLIPSE Draws an ellipse on current axis

SYNOPSIS ^

function h = sldrawellipse(center, shape, n, varargin)

DESCRIPTION ^

SLDRAWELLIPSE Draws an ellipse on current axis

 $ Syntax $
   - sldrawellipse(center, shape)
   - sldrawellipse(center, shape, n)
   - sldrawellipse(center, shape, n, ...)
   - h = sldrawellipse(...)

 $ Description $
   - sldrawellipse(center, shape) draws an ellipse specified by
     its center and shape on current axis. center is a vector of
     length 2, storing the x and y coordinate of the center. 
     shape have following two forms:
     1. [a, b, theta], here a and b are the half-lengths of two axis,
        theta is the radians of first axis relative to x-axis, in 
        anti-clockwise manner.
     2. C, a 2 x 2 covariance matrix, the ellipse is the set of points
        with mahalanobis distance to the center being equal to 1.
        ellipse eqn: x^T C x = 1
     3. {C, r}, C is a 2 x 2 covariance matrix, the ellipse is the set 
        of points with mahalanobis distance to the center being equal to 
        r. ellipse eqn: x^T C x = r^2.

   - sldrawellipse(center, shape, n) draws the ellipse using specified
     number of samples. If n is not specified or empty, then n takes
     the default value 300.
          
   - sldrawellipse(center, shape, n, ...) draws the ellipse with the 
     plotting properties specified by additional arguments. By default,
     it uses 'b-'.

   - h = sldrawellipse(...) returns the handle to the line
     drawn on the axis.

 $ History $
   - Created by Dahua Lin on Apr 23, 2006
   - Modified by Dahua Lin, on Aug 26, 2006
       - from sldraw_ellipse to sldrawellipse
   - Modified by Dahua Lin, on Sep 10, 2006
       - replace sladd by sladdvec to increase efficiency

CROSS-REFERENCE INFORMATION ^

This function calls:
  • sladdvec SLADDVEC adds a vector to columns or rows of a matrix
  • slsymeig SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function h = sldrawellipse(center, shape, n, varargin)
0002 %SLDRAWELLIPSE Draws an ellipse on current axis
0003 %
0004 % $ Syntax $
0005 %   - sldrawellipse(center, shape)
0006 %   - sldrawellipse(center, shape, n)
0007 %   - sldrawellipse(center, shape, n, ...)
0008 %   - h = sldrawellipse(...)
0009 %
0010 % $ Description $
0011 %   - sldrawellipse(center, shape) draws an ellipse specified by
0012 %     its center and shape on current axis. center is a vector of
0013 %     length 2, storing the x and y coordinate of the center.
0014 %     shape have following two forms:
0015 %     1. [a, b, theta], here a and b are the half-lengths of two axis,
0016 %        theta is the radians of first axis relative to x-axis, in
0017 %        anti-clockwise manner.
0018 %     2. C, a 2 x 2 covariance matrix, the ellipse is the set of points
0019 %        with mahalanobis distance to the center being equal to 1.
0020 %        ellipse eqn: x^T C x = 1
0021 %     3. {C, r}, C is a 2 x 2 covariance matrix, the ellipse is the set
0022 %        of points with mahalanobis distance to the center being equal to
0023 %        r. ellipse eqn: x^T C x = r^2.
0024 %
0025 %   - sldrawellipse(center, shape, n) draws the ellipse using specified
0026 %     number of samples. If n is not specified or empty, then n takes
0027 %     the default value 300.
0028 %
0029 %   - sldrawellipse(center, shape, n, ...) draws the ellipse with the
0030 %     plotting properties specified by additional arguments. By default,
0031 %     it uses 'b-'.
0032 %
0033 %   - h = sldrawellipse(...) returns the handle to the line
0034 %     drawn on the axis.
0035 %
0036 % $ History $
0037 %   - Created by Dahua Lin on Apr 23, 2006
0038 %   - Modified by Dahua Lin, on Aug 26, 2006
0039 %       - from sldraw_ellipse to sldrawellipse
0040 %   - Modified by Dahua Lin, on Sep 10, 2006
0041 %       - replace sladd by sladdvec to increase efficiency
0042 %
0043 
0044 
0045 %% parse and verify arguments
0046 
0047 if nargin < 2
0048     raise_lackinput('sldraw_ellipse', 2);
0049 end
0050 
0051 if length(center) ~= 2
0052     error('sltoolbox:invaliddims', 'center should be a vector of length 2');    
0053 end
0054 center = center(:);
0055 
0056 if nargin < 3 || isempty(n)
0057     n = 300;
0058 end
0059 
0060 %% convert the shape to transform (scale and rotation)
0061 
0062 if isnumeric(shape) && length(shape) == 3 % [a, b, theta] form
0063     
0064     [S, R] = get_transform_from_geo(shape(1), shape(2), shape(3));                
0065     
0066 elseif isnumeric(shape) && isequal(size(shape), [2, 2]) % C form
0067     
0068     [S, R] = get_transform_from_cov(shape);
0069     
0070 elseif iscell(shape) && length(shape) == 2 % {C, r} form
0071     
0072     [S, R] = get_transform_from_cov(shape{1});
0073     S = S * shape{2};
0074     
0075 end
0076 
0077 T = R * S;
0078 
0079 %% make the points forming the ellipse
0080 
0081 % make a circle first
0082 t = linspace(0, 2*pi, n);
0083 X = [cos(t); sin(t)];
0084 
0085 % transform the circle to an ellipse
0086 X = T * X;
0087 X = sladdvec(X, center, 1);
0088 
0089 % draw the circle
0090 hold on;
0091 h = plot(X(1,:), X(2,:), varargin{:});
0092 
0093 
0094 %% The functions for making transforms from shapes
0095 
0096 function [S, R] = get_transform_from_geo(a, b, theta)
0097 
0098 S = [a 0; 0 b];
0099 R = [cos(theta), -sin(theta); sin(theta), cos(theta)];
0100 
0101 function [S, R] = get_transform_from_cov(C)
0102 
0103 [S, R] = slsymeig(C);
0104 S = max(S, 0);  % a zero guard
0105 S = diag(sqrt(S));
0106 
0107 
0108 
0109     
0110     
0111 
0112

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com