6823
|
1 ## Copyright (C) 2000 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 2, or (at your option) |
|
8 ## 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, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} voronoi (@var{x}, @var{y}) |
|
22 ## @deftypefnx {Function File} {} voronoi (@var{x}, @var{y}, "plotstyle") |
|
23 ## @deftypefnx {Function File} {} voronoi (@var{x}, @var{y}, "plotstyle", @var{options}) |
|
24 ## @deftypefnx {Function File} {[@var{vx}, @var{vy}] =} voronoi (@dots{}) |
|
25 ## plots voronoi diagram of points @code{(@var{x}, @var{y})}. |
|
26 ## The voronoi facets with points at infinity are not drawn. |
|
27 ## [@var{vx}, @var{vy}] = voronoi(...) returns the vertices instead plotting the |
|
28 ## diagram. plot (@var{vx}, @var{vy}) shows the voronoi diagram. |
|
29 ## |
|
30 ## A fourth optional argument, which must be a string, contains extra options |
|
31 ## passed to the underlying qhull command. See the documentation for the |
|
32 ## Qhull library for details. |
|
33 ## |
|
34 ## @example |
|
35 ## @group |
|
36 ## x = rand(10,1); y = rand(size(x)); |
|
37 ## h = convhull(x,y); |
|
38 ## [vx,vy] = voronoi(x,y); |
|
39 ## plot(vx, vy, "-b", x, y, "o", x(h), y(h), "-g") |
|
40 ## legend("", "points", "hull"); |
|
41 ## @end group |
|
42 ## @end example |
|
43 ## |
|
44 ## @seealso{voronoin, delaunay, convhull} |
|
45 ## @end deftypefn |
|
46 |
|
47 ## Author: Kai Habel <kai.habel@gmx.de> |
|
48 ## First Release: 20/08/2000 |
|
49 |
|
50 ## 2002-01-04 Paul Kienzle <pkienzle@users.sf.net> |
|
51 ## * limit the default graph to the input points rather than the whole diagram |
|
52 ## * provide example |
|
53 ## * use unique(x,"rows") rather than __unique_rows__ |
|
54 |
|
55 ## 2003-12-14 Rafael Laboissiere <rafael@laboissiere.net> |
|
56 ## Added optional fourth argument to pass options to the underlying |
|
57 ## qhull command |
|
58 |
|
59 function [vvx, vvy] = voronoi (varargin) |
|
60 |
|
61 if (nargin < 1) |
|
62 print_usage (); |
|
63 endif |
|
64 |
|
65 narg = 1; |
|
66 if (isscalar (varargin{1}) && ishandle (varargin{1})) |
|
67 handl = varargin{1}; |
|
68 narg++; |
|
69 obj = get (handl); |
|
70 if (! strcmp (obj.type, "axes")) |
|
71 error ("voronoi: expecting first argument to be an axes object"); |
|
72 endif |
|
73 else |
|
74 if (nargout < 2) |
|
75 handl = gca(); |
|
76 endif |
|
77 endif |
|
78 |
|
79 if (nargin < 1 + narg || nargin > 3 + narg) |
|
80 print_usage (); |
|
81 endif |
|
82 |
|
83 x = varargin{narg++}; |
|
84 y = varargin{narg++}; |
|
85 |
|
86 opts = {}; |
|
87 if (narg <= nargin) |
|
88 if (iscell (varargin{narg})) |
|
89 opts = varargin(narg++); |
|
90 elseif (ismatrix (varargin{narg})) |
|
91 ## Accept but ignore the triangulation |
|
92 narg++; |
|
93 endif |
|
94 endif |
|
95 |
|
96 linespec = {"b"}; |
|
97 if (narg <= nargin) |
|
98 if (ischar (varargin{narg})) |
|
99 linespec = varargin(narg); |
|
100 endif |
|
101 endif |
|
102 |
|
103 lx = length (x); |
|
104 ly = length (y); |
|
105 |
|
106 if (lx != ly) |
|
107 error ("voronoi: arguments must be vectors of same length"); |
|
108 endif |
|
109 |
|
110 [p, c, infi] = __voronoi__ ([x(:),y(:)], opts{:}); |
|
111 |
|
112 idx = find (!infi); |
|
113 |
|
114 ll = length (idx); |
|
115 k = 0;r = 1; |
|
116 |
|
117 for i = 1:ll |
|
118 k += length (c{idx(i)}); |
|
119 endfor |
|
120 |
|
121 edges = zeros (2, k); |
|
122 |
|
123 for i=1:ll |
|
124 fac = c {idx(i)}; |
|
125 lf = length(fac); |
|
126 fac = [fac, fac(1)]; |
|
127 fst = fac(1:length(fac) - 1); |
|
128 sec = fac(2:length(fac)); |
|
129 edges(:,r:r+lf-1) = [fst; sec]; |
|
130 r += lf; |
|
131 endfor |
|
132 |
|
133 ## Identify the unique edges of the Voronoi diagram |
|
134 edges = sortrows (sort (edges).').'; |
|
135 edges = edges (:,[(edges(1,1:end-1) != edges(1,2:end) | ... |
|
136 edges(2,1:end-1) != edges(2,2:end)), true]); |
|
137 |
|
138 ## Get points of the diagram |
|
139 vx = reshape(p(edges, 1), size(edges)); |
|
140 vy = reshape(p(edges, 2), size(edges)); |
|
141 |
|
142 if (nargout < 2) |
|
143 lim = [min(x(:)), max(x(:)), min(y(:)), max(y(:))]; |
|
144 h = plot (handl, vx, vy, linespec{:}, x, y, '+'); |
|
145 axis(lim+0.1*[[-1,1]*(lim(2)-lim(1)),[-1,1]*(lim(4)-lim(3))]); |
|
146 if (nargout == 1) |
|
147 vxx = h; |
|
148 endif |
|
149 elseif (nargout == 2) |
|
150 vvx = vx; |
|
151 vvy = vy; |
|
152 else |
|
153 error ("voronoi: only two or zero output arguments supported") |
|
154 endif |
|
155 |
|
156 endfunction |