Home > General-Functions > Graphics > plot_ellipse.m

plot_ellipse

PURPOSE ^

% plot_ellipse: plot 2D ellipse

SYNOPSIS ^

function h = plot_ellipse(ellipse,fmt)

DESCRIPTION ^

% plot_ellipse: plot 2D ellipse

 usage:
 h = plot_ellipse(p,fmt) using parameters p = [xo,yo,a,b,phi]
 h = plot_ellipse(C,fmt) using 3x3 conic C with det(Chh)!>0, Chh = C(1:2,1:2)
 h = plot_ellipse(Cov,fmt) using Cov.mean and Cov.cov covariance matrix

 arguments:
  p - double 5x1, parameters of ellipse p = [xo,yo,a,b,phi]
                  xo,yo:  centre
                  a, b:  semi axes
                  phi:  direction
  C - double 3x3, conic
  Cov - struct with fields
        .mean - double 2x1
        .cov  - double 2x2 covariance matrix to plot at mean
  fmt:  optional format string

 output:
      h:  handle to line object

 author: J. Meidow

 2004, Oct.: adaptive number of supporting points 
 2005, Mar.: check input args
 2016, Sep.: added conic and covariance matrix as input 
             Susanne Wenzel, wenzel@igg.uni-bonn.de

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %% plot_ellipse: plot 2D ellipse
0002 %
0003 % usage:
0004 % h = plot_ellipse(p,fmt) using parameters p = [xo,yo,a,b,phi]
0005 % h = plot_ellipse(C,fmt) using 3x3 conic C with det(Chh)!>0, Chh = C(1:2,1:2)
0006 % h = plot_ellipse(Cov,fmt) using Cov.mean and Cov.cov covariance matrix
0007 %
0008 % arguments:
0009 %  p - double 5x1, parameters of ellipse p = [xo,yo,a,b,phi]
0010 %                  xo,yo:  centre
0011 %                  a, b:  semi axes
0012 %                  phi:  direction
0013 %  C - double 3x3, conic
0014 %  Cov - struct with fields
0015 %        .mean - double 2x1
0016 %        .cov  - double 2x2 covariance matrix to plot at mean
0017 %  fmt:  optional format string
0018 %
0019 % output:
0020 %      h:  handle to line object
0021 %
0022 % author: J. Meidow
0023 %
0024 % 2004, Oct.: adaptive number of supporting points
0025 % 2005, Mar.: check input args
0026 % 2016, Sep.: added conic and covariance matrix as input
0027 %             Susanne Wenzel, wenzel@igg.uni-bonn.de
0028 function h = plot_ellipse(ellipse,fmt)
0029 
0030 if nargin<1
0031     error('not enough input arguments')
0032 end
0033 
0034 if nargin>1 && ~ischar(fmt)
0035     error('2nd input must be string')
0036 end
0037 
0038 if nargin==1
0039     fmt = 'k-';
0040 end
0041 
0042 if isstruct(ellipse)
0043     p(1) = ellipse.mean(1);       % x0
0044     p(2) = ellipse.mean(2);       % y0
0045     [~,D,R] = svd(ellipse.cov);
0046     p(3) = sqrt(D(1,1));        % a
0047     p(4) = sqrt(D(2,2));        % b
0048     p(5) = atan2(R(2,1),R(1,1));  % phi
0049 elseif ~isvector(ellipse)
0050     p = ellipseConic2param(ellipse);
0051 else
0052     p = ellipse;
0053 end
0054   
0055 xo = p(1);
0056 yo = p(2);
0057 a = p(3);
0058 b = p(4);
0059 phi = p(5);
0060     
0061 
0062 if a<=0.0
0063     warning('plot ellipse: semi-major axis a = %g <=0', a)
0064 end
0065 if b<=0.0
0066     warning('plot ellipse: semi-minor axis b = %g <=0', b)
0067 end
0068 
0069 % ds = 10;              % pixel
0070 % perim = 2*(a+b);        % perimeter
0071 % N = round(perim/ds);  % number of supporting points 16...200
0072 % N = min( N, 200);
0073 % N = max( N, 200);
0074 N = 20000;
0075 
0076 t = linspace( 0.0, 2.0*pi, N );
0077 
0078 u = a*sin(t);
0079 v = b*cos(t);
0080 x = cos(phi)*u -sin(phi)*v +xo;
0081 y = sin(phi)*u +cos(phi)*v +yo;
0082 
0083 h = plot(x,y,fmt);
0084

Generated on Sat 21-Jul-2018 20:56:10 by m2html © 2005