changelog shortlog tags changeset files revisions annotate raw

scripts/geometry/convhull.m

changeset 10289: 4b124317dc38
parent:ec0a13863eb7
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (46 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2000, 2007, 2008 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 3 of the License, or (at
8## your option) 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, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
20## @deftypefn {Function File} {@var{H} =} convhull (@var{x}, @var{y})
21## @deftypefnx {Function File} {@var{H} =} convhull (@var{x}, @var{y}, @var{opt})
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
32## Author: Kai Habel <kai.habel@gmx.de>
33
34function H = convhull (x, y, opt)
35
36 if (nargin != 2 && nargin != 3)
37 print_usage ();
38 endif
39
40 if (isvector (x) && isvector (y) && length (x) == length (y))
41 if (nargin == 2)
42 i = convhulln ([x(:), y(:)]);
43 elseif (ischar (opt) || iscell (opt))
44 i = convhulln ([x(:), y(:)], opt);
45 else
46 error ("convhull: third argument must be a string or cell array of strings");
47 endif
48 else
49 error ("convhull: first two input arguments must be vectors of same size");
50 endif
51
52 n = rows (i);
53 i = i'(:);
54 H = zeros (n + 1, 1);
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);
74endfunction
75
76%!testif HAVE_QHULL
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]);