6823
|
1 ## Copyright (C) 2000 Kai Habel |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Loadable Function} {@var{H} =} convhull (@var{x}, @var{y}) |
|
22 ## @deftypefnx {Loadable Function} {@var{H} =} convhull (@var{x}, @var{y}, @var{opt}) |
|
23 ## Returns the index vector to the points of the enclosing convex hull. The |
|
24 ## data points are defined by the x and y vectors. |
|
25 ## |
|
26 ## A third optional argument, which must be a string, contains extra options |
|
27 ## passed to the underlying qhull command. See the documentation for the |
|
28 ## Qhull library for details. |
|
29 ## |
|
30 ## @seealso{delaunay, convhulln} |
|
31 ## @end deftypefn |
|
32 |
6826
|
33 ## Author: Kai Habel <kai.habel@gmx.de> |
6823
|
34 |
6826
|
35 function H = convhull (x, y, opt) |
6823
|
36 |
6826
|
37 if (nargin != 2 && nargin != 3) |
6823
|
38 print_usage (); |
|
39 endif |
|
40 |
6826
|
41 if (isvector (x) && isvector (y) && length (x) == length (y)) |
6823
|
42 if (nargin == 2) |
6826
|
43 i = convhulln ([x(:), y(:)]); |
|
44 elseif (ischar (opt) || iscell (opt)) |
|
45 i = convhulln ([x(:), y(:)], opt); |
6823
|
46 else |
6826
|
47 error ("convhull: third argument must be a string or cell array of strings"); |
6823
|
48 endif |
|
49 else |
6826
|
50 error ("convhull: first two input arguments must be vectors of same size"); |
6823
|
51 endif |
|
52 |
6826
|
53 n = rows (i); |
6823
|
54 i = i'(:); |
6826
|
55 H = zeros (n + 1, 1); |
6823
|
56 |
|
57 H(1) = i(1); |
|
58 next_i = i(2); |
|
59 i(2) = 0; |
|
60 for k = 2:n |
|
61 next_idx = find (i == next_i); |
|
62 |
|
63 if (rem (next_idx, 2) == 0) |
|
64 H(k) = i(next_idx); |
|
65 next_i = i(next_idx - 1); |
|
66 i(next_idx - 1) = 0; |
|
67 else |
|
68 H(k) = i(next_idx); |
|
69 next_i = i(next_idx + 1); |
|
70 i(next_idx + 1) = 0; |
|
71 endif |
|
72 endfor |
|
73 |
|
74 H(n + 1) = H(1); |
|
75 endfunction |
|
76 |
|
77 %!test |
|
78 %! x = -3:0.5:3; |
|
79 %! y = abs (sin (x)); |
|
80 %! assert (convhull (x, y, {"s","Qci","Tcv","Pp"}), [1;7;13;12;11;10;4;3;2;1]) |
|
81 |
|
82 %!demo |
|
83 %! x = -3:0.05:3; |
|
84 %! y = abs (sin (x)); |
|
85 %! k = convhull (x, y); |
|
86 %! plot (x(k),y(k),'r-',x,y,'b+'); |
|
87 %! axis ([-3.05, 3.05, -0.05, 1.05]); |