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