Mercurial > hg > octave-nkf
annotate scripts/geometry/convhull.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 7ff0bdc3dc4c |
children | 11949c9795a0 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13746
diff
changeset
|
1 ## Copyright (C) 2000-2012 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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
20 ## @deftypefn {Function File} {@var{H} =} convhull (@var{x}, @var{y}) |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{H} =} convhull (@var{x}, @var{y}, @var{options}) |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
22 ## Compute the convex hull of the set of points defined by the |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
23 ## vectors @var{x} and @var{y}. The hull @var{H} is an index vector into |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
24 ## the set of points and specifies which points form the enclosing hull. |
6823 | 25 ## |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
26 ## An optional third argument, which must be a string or cell array of strings, |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
27 ## contains options passed to the underlying qhull command. |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
28 ## See the documentation for the Qhull library for details |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
29 ## @url{http://www.qhull.org/html/qh-quick.htm#options}. |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
30 ## The default option is @code{@{"Qt"@}}. |
6823 | 31 ## |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
32 ## If @var{options} is not present or @code{[]} then the default arguments are |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
33 ## used. Otherwise, @var{options} replaces the default argument list. |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
34 ## To append user options to the defaults it is necessary to repeat the |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
35 ## default arguments in @var{options}. Use a null string to pass no arguments. |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
36 ## |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
37 ## @seealso{convhulln, delaunay, voronoi} |
6823 | 38 ## @end deftypefn |
39 | |
6826 | 40 ## Author: Kai Habel <kai.habel@gmx.de> |
6823 | 41 |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
42 function H = convhull (x, y, options) |
6823 | 43 |
6826 | 44 if (nargin != 2 && nargin != 3) |
6823 | 45 print_usage (); |
46 endif | |
47 | |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
48 if (! (isvector (x) && isvector (y) && length (x) == length (y)) |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
49 && ! size_equal (x, y)) |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
50 error ("convhull: X and Y must be the same size"); |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
51 elseif (nargin == 3 && ! (ischar (options) || iscellstr (options))) |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
52 error ("convhull: OPTIONS must be a string or cell array of strings"); |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
53 endif |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
54 |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
55 if (nargin == 2) |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
56 i = convhulln ([x(:), y(:)]); |
6823 | 57 else |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
58 i = convhulln ([x(:), y(:)], options); |
6823 | 59 endif |
60 | |
6826 | 61 n = rows (i); |
6823 | 62 i = i'(:); |
6826 | 63 H = zeros (n + 1, 1); |
6823 | 64 |
65 H(1) = i(1); | |
66 next_i = i(2); | |
67 i(2) = 0; | |
68 for k = 2:n | |
69 next_idx = find (i == next_i); | |
70 | |
71 if (rem (next_idx, 2) == 0) | |
72 H(k) = i(next_idx); | |
73 next_i = i(next_idx - 1); | |
74 i(next_idx - 1) = 0; | |
75 else | |
76 H(k) = i(next_idx); | |
77 next_i = i(next_idx + 1); | |
78 i(next_idx + 1) = 0; | |
79 endif | |
80 endfor | |
81 | |
82 H(n + 1) = H(1); | |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
83 |
6823 | 84 endfunction |
85 | |
86 | |
87 %!demo | |
88 %! x = -3:0.05:3; | |
89 %! y = abs (sin (x)); | |
90 %! k = convhull (x, y); | |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
91 %! plot (x(k),y(k),"r-;convex hull;", x,y,"b+;points;"); |
6823 | 92 %! axis ([-3.05, 3.05, -0.05, 1.05]); |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
93 |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
94 %!testif HAVE_QHULL |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
95 %! x = -3:0.5:3; |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
96 %! y = abs (sin (x)); |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
97 %! assert (convhull (x, y), [1;7;13;12;11;10;4;3;2;1]) |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
98 |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
99 %% FIXME: Need input validation tests |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
100 |