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