Mercurial > hg > octave-nkf
annotate scripts/geometry/convhull.m @ 20038:9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Try to trim long lines to < 80 chars.
Use '##' for single line comments.
Use '(...)' around tests for if/elseif/switch/while.
Abut cell indexing operator '{' next to variable.
Abut array indexing operator '(' next to variable.
Use space between negation operator '!' and following expression.
Use two newlines between endfunction and start of %!test or %!demo code.
Remove unnecessary parens grouping between short-circuit operators.
Remove stray extra spaces (typos) between variables and assignment operators.
Remove stray extra spaces from ends of lines.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 23 Feb 2015 14:54:39 -0800 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19790
diff
changeset
|
1 ## Copyright (C) 2000-2015 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 |
14267
527ed2a51d54
convhull.m: Allow non-vector arguments, for Matlab compatibility
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14237
diff
changeset
|
23 ## arrays @var{x} and @var{y}. The hull @var{H} is an index vector into |
13746
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 |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
33 ## used. Otherwise, @var{options} replaces the default argument list. |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
34 ## To append user options to the defaults it is necessary to repeat the |
13746
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 | |
14278
fa894f89b18f
convhull.m: Ensure column (not row) vectors
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14267
diff
changeset
|
48 ## convhulln expects column vectors |
fa894f89b18f
convhull.m: Ensure column (not row) vectors
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14267
diff
changeset
|
49 x = x(:); |
fa894f89b18f
convhull.m: Ensure column (not row) vectors
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14267
diff
changeset
|
50 y = y(:); |
14267
527ed2a51d54
convhull.m: Allow non-vector arguments, for Matlab compatibility
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14237
diff
changeset
|
51 |
527ed2a51d54
convhull.m: Allow non-vector arguments, for Matlab compatibility
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14237
diff
changeset
|
52 if (length (x) != length (y)) |
527ed2a51d54
convhull.m: Allow non-vector arguments, for Matlab compatibility
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14237
diff
changeset
|
53 error ("convhull: X and Y must have the same size"); |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
54 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
|
55 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
|
56 endif |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
57 |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
58 if (nargin == 2) |
14267
527ed2a51d54
convhull.m: Allow non-vector arguments, for Matlab compatibility
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14237
diff
changeset
|
59 i = convhulln ([x, y]); |
6823 | 60 else |
14267
527ed2a51d54
convhull.m: Allow non-vector arguments, for Matlab compatibility
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14237
diff
changeset
|
61 i = convhulln ([x, y], options); |
6823 | 62 endif |
63 | |
6826 | 64 n = rows (i); |
6823 | 65 i = i'(:); |
6826 | 66 H = zeros (n + 1, 1); |
6823 | 67 |
68 H(1) = i(1); | |
69 next_i = i(2); | |
70 i(2) = 0; | |
71 for k = 2:n | |
72 next_idx = find (i == next_i); | |
73 | |
74 if (rem (next_idx, 2) == 0) | |
75 H(k) = i(next_idx); | |
76 next_i = i(next_idx - 1); | |
77 i(next_idx - 1) = 0; | |
78 else | |
79 H(k) = i(next_idx); | |
80 next_i = i(next_idx + 1); | |
81 i(next_idx + 1) = 0; | |
82 endif | |
83 endfor | |
84 | |
85 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
|
86 |
6823 | 87 endfunction |
88 | |
89 | |
90 %!demo | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %! clf; |
6823 | 92 %! x = -3:0.05:3; |
93 %! y = abs (sin (x)); | |
94 %! 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
|
95 %! plot (x(k),y(k),"r-;convex hull;", x,y,"b+;points;"); |
6823 | 96 %! 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
|
97 |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
98 %!testif HAVE_QHULL |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
99 %! 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
|
100 %! y = abs (sin (x)); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14278
diff
changeset
|
101 %! assert (convhull (x, y), [1;7;13;12;11;10;4;3;2;1]); |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
102 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
103 ## FIXME: Need input validation tests |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
104 |