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