7018
|
1 ## Copyright (C) 2007 David Bateman |
|
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 3 of the License, or (at |
|
8 ## your option) 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, see |
|
17 ## <http://www.gnu.org/licenses/>. |
|
18 |
6832
|
19 function geometryimages (nm, typ) |
|
20 bury_output (); |
6878
|
21 if (isempty (findstr (octave_config_info ("DEFS"), "HAVE_QHULL")) |
|
22 && (strcmp (nm, "voronoi") || strcmp (nm, "griddata") |
|
23 || strcmp (nm, "convhull") || strcmp (nm, "delaunay") |
|
24 || strcmp (nm, "triplot"))) |
6855
|
25 sombreroimage (nm, typ); |
|
26 elseif (strcmp (nm, "voronoi")) |
6832
|
27 rand("state",9); |
|
28 x = rand(10,1); |
|
29 y = rand(10,1); |
|
30 tri = delaunay (x, y); |
|
31 [vx, vy] = voronoi (x, y, tri); |
|
32 triplot (tri, x, y, "b"); |
|
33 hold on; |
|
34 plot (vx, vy, "r"); |
|
35 [r, c] = tri2circ (tri(end,:), x, y); |
|
36 pc = [-1:0.01:1]; |
|
37 xc = r * sin(pi*pc) + c(1); |
|
38 yc = r * cos(pi*pc) + c(2); |
|
39 plot (xc, yc, "g-", "LineWidth", 3); |
|
40 axis([0, 1, 0, 1]); |
|
41 legend ("Delaunay Triangulation", "Voronoi Diagram"); |
|
42 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
43 elseif (strcmp (nm, "triplot")) |
|
44 rand ("state", 2) |
|
45 x = rand (20, 1); |
|
46 y = rand (20, 1); |
|
47 tri = delaunay (x, y); |
|
48 triplot (tri, x, y); |
|
49 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
50 elseif (strcmp (nm, "griddata")) |
|
51 rand("state",1); |
|
52 x=2*rand(1000,1)-1; |
|
53 y=2*rand(size(x))-1; |
|
54 z=sin(2*(x.^2+y.^2)); |
|
55 [xx,yy]=meshgrid(linspace(-1,1,32)); |
|
56 griddata(x,y,z,xx,yy); |
|
57 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
58 elseif (strcmp (nm, "convhull")) |
|
59 x = -3:0.05:3; |
|
60 y = abs (sin (x)); |
|
61 k = convhull (x, y); |
|
62 plot (x(k),y(k),'r-',x,y,'b+'); |
|
63 axis ([-3.05, 3.05, -0.05, 1.05]); |
|
64 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
65 elseif (strcmp (nm, "delaunay")) |
|
66 rand ("state", 1); |
6847
|
67 x = rand (1, 10); |
|
68 y = rand (1, 10); |
6832
|
69 T = delaunay (x, y); |
|
70 X = [ x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1)) ]; |
|
71 Y = [ y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1)) ]; |
|
72 axis ([0, 1, 0, 1]); |
|
73 plot(X, Y, "b", x, y, "r*"); |
|
74 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
6847
|
75 elseif (strcmp (nm, "inpolygon")) |
|
76 randn ("state", 2); |
|
77 x = randn (100, 1); |
|
78 y = randn (100, 1); |
|
79 vx = cos (pi * [-1 : 0.1: 1]); |
|
80 vy = sin (pi * [-1 : 0.1 : 1]); |
|
81 in = inpolygon (x, y, vx, vy); |
|
82 plot(vx, vy, x(in), y(in), "r+", x(!in), y(!in), "bo"); |
|
83 axis ([-2, 2, -2, 2]); |
|
84 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
6832
|
85 else |
|
86 error ("unrecognized plot requested"); |
|
87 endif |
|
88 bury_output (); |
|
89 endfunction |
|
90 |
|
91 function [r, c] = tri2circ (tri, xx, yy) |
|
92 x = xx(tri); |
|
93 y = yy(tri); |
|
94 m = (y(1:end-1) - y(2:end)) ./ (x(1:end-1) - x(2:end)); |
|
95 xc = (prod(m) .* (y(1) - y(end)) + m(end)*(x(1)+x(2)) - m(1)*(x(2)+x(3))) ... |
|
96 ./ (2 * (m(end) - m(1))); |
|
97 yc = - (xc - (x(2) + x(3))./2) ./ m(end) + (y(2) + y(3)) / 2; |
|
98 c = [xc, yc]; |
|
99 r = sqrt ((xc - x(1)).^2 + (yc - y(1)).^2); |
|
100 endfunction |
|
101 |
|
102 ## Use this function before plotting commands and after every call to |
|
103 ## print since print() resets output to stdout (unfortunately, gnpulot |
|
104 ## can't pop output as it can the terminal type). |
|
105 function bury_output () |
|
106 f = figure (1); |
|
107 set (f, "visible", "off"); |
|
108 endfunction |
|
109 function geometryimages (nm, typ) |
|
110 bury_output (); |
|
111 if (strcmp (nm, "voronoi")) |
|
112 rand("state",9); |
|
113 x = rand(10,1); |
|
114 y = rand(10,1); |
|
115 tri = delaunay (x, y); |
|
116 [vx, vy] = voronoi (x, y, tri); |
|
117 triplot (tri, x, y, "b"); |
|
118 hold on; |
|
119 plot (vx, vy, "r"); |
|
120 [r, c] = tri2circ (tri(end,:), x, y); |
|
121 pc = [-1:0.01:1]; |
|
122 xc = r * sin(pi*pc) + c(1); |
|
123 yc = r * cos(pi*pc) + c(2); |
|
124 plot (xc, yc, "g-", "LineWidth", 3); |
|
125 axis([0, 1, 0, 1]); |
|
126 legend ("Delaunay Triangulation", "Voronoi Diagram"); |
|
127 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
128 elseif (strcmp (nm, "triplot")) |
|
129 rand ("state", 2) |
|
130 x = rand (20, 1); |
|
131 y = rand (20, 1); |
|
132 tri = delaunay (x, y); |
|
133 triplot (tri, x, y); |
|
134 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
135 elseif (strcmp (nm, "griddata")) |
|
136 rand("state",1); |
|
137 x=2*rand(1000,1)-1; |
|
138 y=2*rand(size(x))-1; |
|
139 z=sin(2*(x.^2+y.^2)); |
|
140 [xx,yy]=meshgrid(linspace(-1,1,32)); |
|
141 griddata(x,y,z,xx,yy); |
|
142 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
143 else |
|
144 error ("unrecognized plot requested"); |
|
145 endif |
|
146 bury_output (); |
|
147 endfunction |
|
148 |
|
149 function [r, c] = tri2circ (tri, xx, yy) |
|
150 x = xx(tri); |
|
151 y = yy(tri); |
|
152 m = (y(1:end-1) - y(2:end)) ./ (x(1:end-1) - x(2:end)); |
|
153 xc = (prod(m) .* (y(1) - y(end)) + m(end)*(x(1)+x(2)) - m(1)*(x(2)+x(3))) ... |
|
154 ./ (2 * (m(end) - m(1))); |
|
155 yc = - (xc - (x(2) + x(3))./2) ./ m(end) + (y(2) + y(3)) / 2; |
|
156 c = [xc, yc]; |
|
157 r = sqrt ((xc - x(1)).^2 + (yc - y(1)).^2); |
|
158 endfunction |
|
159 |
|
160 ## Use this function before plotting commands and after every call to |
|
161 ## print since print() resets output to stdout (unfortunately, gnpulot |
|
162 ## can't pop output as it can the terminal type). |
|
163 function bury_output () |
|
164 f = figure (1); |
|
165 set (f, "visible", "off"); |
|
166 endfunction |
6855
|
167 |
|
168 function sombreroimage (nm, typ) |
|
169 if (strcmp (typ, "txt")) |
|
170 fid = fopen (sprintf ("%s.txt", nm), "wt"); |
|
171 fputs (fid, "+-----------------------------+\n"); |
|
172 fputs (fid, "| Image unavailable because |\n"); |
|
173 fputs (fid, "| of a missing QHULL library. |\n"); |
|
174 fputs (fid, "+-----------------------------+\n"); |
|
175 fclose (fid); |
|
176 return; |
|
177 else ## if (!strcmp (typ, "txt")) |
|
178 |
|
179 bury_output (); |
|
180 |
|
181 x = y = linspace (-8, 8, 41)'; |
|
182 [xx, yy] = meshgrid (x, y); |
|
183 r = sqrt (xx .^ 2 + yy .^ 2) + eps; |
|
184 z = sin (r) ./ r; |
|
185 unwind_protect |
|
186 mesh (x, y, z); |
|
187 title ("Sorry, graphics not available because octave was\\ncompiled without the QHULL library."); |
|
188 unwind_protect_cleanup |
|
189 print (strcat (nm, ".", typ), strcat ("-d", typ)); |
|
190 bury_output (); |
|
191 end_unwind_protect |
|
192 endif |
|
193 endfunction |