Mercurial > hg > octave-nkf
annotate scripts/geometry/voronoi.m @ 12297:175127b734a4 release-3-4-x
ChangeLog fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 30 Jan 2011 01:57:40 -0500 |
parents | c792872f8942 |
children | d0b799dafede |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 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:
10791
diff
changeset
|
20 ## @deftypefn {Function File} {} voronoi (@var{x}, @var{y}) |
6823 | 21 ## @deftypefnx {Function File} {} voronoi (@var{x}, @var{y}, "plotstyle") |
22 ## @deftypefnx {Function File} {} voronoi (@var{x}, @var{y}, "plotstyle", @var{options}) | |
23 ## @deftypefnx {Function File} {[@var{vx}, @var{vy}] =} voronoi (@dots{}) | |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
24 ## plots Voronoi diagram of points @code{(@var{x}, @var{y})}. |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
25 ## The Voronoi facets with points at infinity are not drawn. |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
26 ## [@var{vx}, @var{vy}] = voronoi(@dots{}) returns the vertices instead of |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
27 ## plotting the |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
28 ## diagram. plot (@var{vx}, @var{vy}) shows the Voronoi diagram. |
6823 | 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 | |
6826 | 36 ## x = rand (10, 1); |
37 ## y = rand (size (x)); | |
38 ## h = convhull (x, y); | |
39 ## [vx, vy] = voronoi (x, y); | |
40 ## plot (vx, vy, "-b", x, y, "o", x(h), y(h), "-g") | |
41 ## legend ("", "points", "hull"); | |
6823 | 42 ## @end group |
43 ## @end example | |
44 ## | |
45 ## @seealso{voronoin, delaunay, convhull} | |
46 ## @end deftypefn | |
47 | |
48 ## Author: Kai Habel <kai.habel@gmx.de> | |
49 ## First Release: 20/08/2000 | |
50 | |
51 ## 2002-01-04 Paul Kienzle <pkienzle@users.sf.net> | |
52 ## * limit the default graph to the input points rather than the whole diagram | |
53 ## * provide example | |
54 ## * use unique(x,"rows") rather than __unique_rows__ | |
55 | |
56 ## 2003-12-14 Rafael Laboissiere <rafael@laboissiere.net> | |
57 ## Added optional fourth argument to pass options to the underlying | |
58 ## qhull command | |
59 | |
60 function [vvx, vvy] = voronoi (varargin) | |
61 | |
62 if (nargin < 1) | |
63 print_usage (); | |
64 endif | |
65 | |
66 narg = 1; | |
67 if (isscalar (varargin{1}) && ishandle (varargin{1})) | |
68 handl = varargin{1}; | |
69 narg++; | |
6826 | 70 if (! strcmp (get (handl, "type"), "axes")) |
6823 | 71 error ("voronoi: expecting first argument to be an axes object"); |
72 endif | |
73 else | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
74 if (nargout < 2) |
6826 | 75 handl = gca (); |
6823 | 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++}; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
85 |
6823 | 86 opts = {}; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
87 if (narg <= nargin) |
6823 | 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"}; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
97 if (narg <= nargin) |
6823 | 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 | |
8440
e792c736b1ac
Fix for dense grids and speed up for voronoi function
David Bateman <dbateman@free.fr>
parents:
8347
diff
changeset
|
110 ## Add box to approximate rays to infinity. For Voronoi diagrams the |
e792c736b1ac
Fix for dense grids and speed up for voronoi function
David Bateman <dbateman@free.fr>
parents:
8347
diff
changeset
|
111 ## box can (and should) be close to the points themselves. To make the |
e792c736b1ac
Fix for dense grids and speed up for voronoi function
David Bateman <dbateman@free.fr>
parents:
8347
diff
changeset
|
112 ## job of finding the exterior edges it should be at least two times the |
e792c736b1ac
Fix for dense grids and speed up for voronoi function
David Bateman <dbateman@free.fr>
parents:
8347
diff
changeset
|
113 ## delta below however |
6852 | 114 xmax = max (x(:)); |
115 xmin = min (x(:)); | |
116 ymax = max (y(:)); | |
117 ymin = min (y(:)); | |
118 xdelta = xmax - xmin; | |
119 ydelta = ymax - ymin; | |
8440
e792c736b1ac
Fix for dense grids and speed up for voronoi function
David Bateman <dbateman@free.fr>
parents:
8347
diff
changeset
|
120 scale = 2; |
6852 | 121 |
122 xbox = [xmin - scale * xdelta; xmin - scale * xdelta; ... | |
10549 | 123 xmax + scale * xdelta; xmax + scale * xdelta]; |
6852 | 124 ybox = [xmin - scale * xdelta; xmax + scale * xdelta; ... |
10549 | 125 xmax + scale * xdelta; xmin - scale * xdelta]; |
6852 | 126 |
8440
e792c736b1ac
Fix for dense grids and speed up for voronoi function
David Bateman <dbateman@free.fr>
parents:
8347
diff
changeset
|
127 [p, c, infi] = __voronoi__ ([[x(:) ; xbox(:)], [y(:); ybox(:)]], opts{:}); |
6823 | 128 |
129 idx = find (!infi); | |
130 ll = length (idx); | |
8440
e792c736b1ac
Fix for dense grids and speed up for voronoi function
David Bateman <dbateman@free.fr>
parents:
8347
diff
changeset
|
131 c = c(idx).'; |
e792c736b1ac
Fix for dense grids and speed up for voronoi function
David Bateman <dbateman@free.fr>
parents:
8347
diff
changeset
|
132 k = sum (cellfun ('length', c)); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
133 edges = cell2mat(cellfun (@(x) [x ; [x(end), x(1:end-1)]], c, |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
134 "uniformoutput", false)); |
6823 | 135 |
136 ## Identify the unique edges of the Voronoi diagram | |
137 edges = sortrows (sort (edges).').'; | |
6852 | 138 edges = edges (:, [(edges(1, 1: end - 1) != edges(1, 2 : end) | ... |
10549 | 139 edges(2, 1 :end - 1) != edges(2, 2 : end)), true]); |
6852 | 140 |
141 ## Eliminate the edges of the diagram representing the box | |
142 poutside = (1 : rows(p)) ... | |
143 (p (:, 1) < xmin - xdelta | p (:, 1) > xmax + xdelta | ... | |
144 p (:, 2) < ymin - ydelta | p (:, 2) > ymax + ydelta); | |
145 edgeoutside = ismember (edges (1, :), poutside) & ... | |
146 ismember (edges (2, :), poutside); | |
147 edges (:, edgeoutside) = []; | |
6823 | 148 |
149 ## Get points of the diagram | |
6852 | 150 vx = reshape (p (edges, 1), size(edges)); |
151 vy = reshape (p (edges, 2), size(edges)); | |
6823 | 152 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
153 if (nargout < 2) |
6852 | 154 lim = [xmin, xmax, ymin, ymax]; |
6823 | 155 h = plot (handl, vx, vy, linespec{:}, x, y, '+'); |
6852 | 156 axis (lim + 0.1 * [[-1, 1] * (lim (2) - lim (1)), ... |
10549 | 157 [-1, 1] * (lim (4) - lim (3))]); |
6823 | 158 if (nargout == 1) |
159 vxx = h; | |
160 endif | |
161 elseif (nargout == 2) | |
162 vvx = vx; | |
163 vvy = vy; | |
164 else | |
8664 | 165 error ("voronoi: only two or zero output arguments supported"); |
6823 | 166 endif |
167 | |
168 endfunction |