7017
|
1 ## Copyright (C) 2000, 2007 Kai Habel |
6823
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6823
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6823
|
18 |
|
19 ## -*- texinfo -*- |
6846
|
20 ## @deftypefn {Function File} {@var{H} =} convhull (@var{x}, @var{y}) |
|
21 ## @deftypefnx {Function File} {@var{H} =} convhull (@var{x}, @var{y}, @var{opt}) |
6823
|
22 ## Returns the index vector to the points of the enclosing convex hull. The |
|
23 ## data points are defined by the x and y vectors. |
|
24 ## |
|
25 ## A third optional argument, which must be a string, contains extra options |
|
26 ## passed to the underlying qhull command. See the documentation for the |
|
27 ## Qhull library for details. |
|
28 ## |
|
29 ## @seealso{delaunay, convhulln} |
|
30 ## @end deftypefn |
|
31 |
6826
|
32 ## Author: Kai Habel <kai.habel@gmx.de> |
6823
|
33 |
6826
|
34 function H = convhull (x, y, opt) |
6823
|
35 |
6826
|
36 if (nargin != 2 && nargin != 3) |
6823
|
37 print_usage (); |
|
38 endif |
|
39 |
6826
|
40 if (isvector (x) && isvector (y) && length (x) == length (y)) |
6823
|
41 if (nargin == 2) |
6826
|
42 i = convhulln ([x(:), y(:)]); |
|
43 elseif (ischar (opt) || iscell (opt)) |
|
44 i = convhulln ([x(:), y(:)], opt); |
6823
|
45 else |
6826
|
46 error ("convhull: third argument must be a string or cell array of strings"); |
6823
|
47 endif |
|
48 else |
6826
|
49 error ("convhull: first two input arguments must be vectors of same size"); |
6823
|
50 endif |
|
51 |
6826
|
52 n = rows (i); |
6823
|
53 i = i'(:); |
6826
|
54 H = zeros (n + 1, 1); |
6823
|
55 |
|
56 H(1) = i(1); |
|
57 next_i = i(2); |
|
58 i(2) = 0; |
|
59 for k = 2:n |
|
60 next_idx = find (i == next_i); |
|
61 |
|
62 if (rem (next_idx, 2) == 0) |
|
63 H(k) = i(next_idx); |
|
64 next_i = i(next_idx - 1); |
|
65 i(next_idx - 1) = 0; |
|
66 else |
|
67 H(k) = i(next_idx); |
|
68 next_i = i(next_idx + 1); |
|
69 i(next_idx + 1) = 0; |
|
70 endif |
|
71 endfor |
|
72 |
|
73 H(n + 1) = H(1); |
|
74 endfunction |
|
75 |
|
76 %!test |
|
77 %! x = -3:0.5:3; |
|
78 %! y = abs (sin (x)); |
|
79 %! assert (convhull (x, y, {"s","Qci","Tcv","Pp"}), [1;7;13;12;11;10;4;3;2;1]) |
|
80 |
|
81 %!demo |
|
82 %! x = -3:0.05:3; |
|
83 %! y = abs (sin (x)); |
|
84 %! k = convhull (x, y); |
|
85 %! plot (x(k),y(k),'r-',x,y,'b+'); |
|
86 %! axis ([-3.05, 3.05, -0.05, 1.05]); |