]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/ellipse2cov.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / ellipse2cov.m
1 ## Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
15
16 %% -*- texinfo -*-
17 %% @deftypefn {Function File} {@var{K} = } ellipse2cov (@var{elli})
18 %% @deftypefnx {Function File} {@var{K} = } ellipse2cov (@var{ra}, @var{rb})
19 %% @deftypefnx {Function File} {@var{K} = } ellipse2cov (@dots{}, @var{theta})
20 %% Calculates covariance matrix from ellipse.
21 %%
22 %% If only one input is given, @var{elli} must define an ellipse as described in
23 %% @command{ellipses2d}.
24 %% If two inputs are given, @var{ra} and @var{rb} define the half-lenght of the
25 %% axes.
26 %% If a third input is given, @var{theta} must be the angle of rotation of the
27 %% ellipse in radians, and in counter-clockwise direction.
28 %%
29 %% The output @var{K} contains the covariance matrix define by the ellipse.
30 %%
31 %% Run @code{demo ellipse2cov} to see an example.
32 %%
33 %% @seealso{ellipses2d, cov2ellipse, drawEllipse}
34 %% @end deftypefn
35
36 function K = ellipse2cov (elli, varargin);
37
38   ra    = 1;
39   rb    = 1;
40   theta = 0;
41   switch numel (varargin)
42     case 0
43     %% ellipse format
44       if numel (elli) != 5
45         print_usage ();
46       end
47       ra    = elli(1,3);
48       rb    = elli(1,4);
49       theta = elli(1,5)*pi/180;
50
51     case 2
52     %% ra,rb
53       if numel (elli) != 1
54         print_usage ();
55       end
56       ra = elli;
57       rb = varargin{1};
58
59     case 3
60     %% ra,rb, theta
61       if numel (elli) != 1
62         print_usage ();
63       end
64       ra    = elli;
65       rb    = varargin{1};
66       theta = varargin{2};
67
68     otherwise
69       print_usage ();
70   end
71
72   T = createRotation (theta)(1:2,1:2);
73   K = T*diag([ra rb])*T';
74
75 endfunction
76
77 %!demo
78 %! elli = [0 0 1 3 -45];
79 %!
80 %! % Create 2D normal random variables with covarinace defined by elli.
81 %! K = ellipse2cov (elli)
82 %! L = chol(K,'lower');
83 %! u = randn(1e3,2)*L';
84 %!
85 %! Kn = cov (u)
86 %!
87 %! figure(1)
88 %! plot(u(:,1),u(:,2),'.r');
89 %! hold on;
90 %! drawEllipse(elli,'linewidth',2);
91 %! hold off
92 %! axis tight