7017
|
1 ## Copyright (C) 1999, 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{tri}=} delaunay (@var{x}, @var{y}) |
|
21 ## @deftypefnx {Function File} {@var{tri}=} delaunay (@var{x}, @var{y}, @var{opt}) |
6823
|
22 ## The return matrix of size [n, 3] contains a set triangles which are |
|
23 ## described by the indices to the data point x and y vector. |
|
24 ## The triangulation satisfies the Delaunay circumcircle criterion. |
|
25 ## No other data point is in the circumcircle of the defining triangle. |
|
26 ## |
|
27 ## A third optional argument, which must be a string, contains extra options |
|
28 ## passed to the underlying qhull command. See the documentation for the |
|
29 ## Qhull library for details. |
|
30 ## |
|
31 ## @example |
|
32 ## @group |
6826
|
33 ## x = rand (1, 10); |
|
34 ## y = rand (size (x)); |
|
35 ## T = delaunay (x, y); |
|
36 ## X = [x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1))]; |
|
37 ## Y = [y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1))]; |
|
38 ## axis ([0,1,0,1]); |
|
39 ## plot (X, Y, "b", x, y, "r*"); |
6823
|
40 ## @end group |
|
41 ## @end example |
|
42 ## @seealso{voronoi, delaunay3, delaunayn} |
|
43 ## @end deftypefn |
|
44 |
6826
|
45 ## Author: Kai Habel <kai.habel@gmx.de> |
6823
|
46 |
6826
|
47 function ret = delaunay (x, y, opt) |
6823
|
48 |
6826
|
49 if (nargin != 2 && nargin != 3) |
6823
|
50 print_usage (); |
|
51 endif |
|
52 |
6826
|
53 if (isvector (x) && isvector (y) && length (x) == length (y)) |
6823
|
54 if (nargin == 2) |
6826
|
55 tri = delaunayn ([x(:), y(:)]); |
|
56 elseif (ischar (opt)) |
|
57 tri = delaunayn ([x(:), y(:)], opt); |
6823
|
58 else |
6826
|
59 error ("delaunay: third argument must be a string"); |
6823
|
60 endif |
|
61 else |
6826
|
62 error ("delaunay: first two input arguments must be vectors of same size"); |
6823
|
63 endif |
|
64 |
6826
|
65 if (nargout == 0) |
|
66 x = x(:).'; |
|
67 y = y(:).'; |
|
68 X = [x(tri(:,1)); x(tri(:,2)); x(tri(:,3)); x(tri(:,1))]; |
|
69 Y = [y(tri(:,1)); y(tri(:,2)); y(tri(:,3)); y(tri(:,1))]; |
6823
|
70 plot(X, Y, 'b', x, y, 'r*'); |
|
71 else |
|
72 ret = tri; |
|
73 endif |
|
74 endfunction |
|
75 |
|
76 %!test |
|
77 %! x = [-1, 0, 1, 0, 0]; |
|
78 %! y = [0, 1, 0, -1, 0]; |
|
79 %! assert (sortrows (sort (delaunay (x, y), 2)), [1,2,5;1,4,5;2,3,5;3,4,5]) |
|
80 |
|
81 %!demo |
|
82 %! rand ('state', 1); |
|
83 %! x = rand(1,10); |
|
84 %! y = rand(size(x)); |
|
85 %! T = delaunay(x,y); |
|
86 %! X = [ x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1)) ]; |
|
87 %! Y = [ y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1)) ]; |
|
88 %! axis([0,1,0,1]); |
|
89 %! plot(X,Y,'b',x,y,'r*'); |