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