6832
|
1 function geometryimages (nm, typ) |
|
2 bury_output (); |
|
3 if (strcmp (nm, "voronoi")) |
|
4 rand("state",9); |
|
5 x = rand(10,1); |
|
6 y = rand(10,1); |
|
7 tri = delaunay (x, y); |
|
8 [vx, vy] = voronoi (x, y, tri); |
|
9 triplot (tri, x, y, "b"); |
|
10 hold on; |
|
11 plot (vx, vy, "r"); |
|
12 [r, c] = tri2circ (tri(end,:), x, y); |
|
13 pc = [-1:0.01:1]; |
|
14 xc = r * sin(pi*pc) + c(1); |
|
15 yc = r * cos(pi*pc) + c(2); |
|
16 plot (xc, yc, "g-", "LineWidth", 3); |
|
17 axis([0, 1, 0, 1]); |
|
18 legend ("Delaunay Triangulation", "Voronoi Diagram"); |
|
19 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
20 elseif (strcmp (nm, "triplot")) |
|
21 rand ("state", 2) |
|
22 x = rand (20, 1); |
|
23 y = rand (20, 1); |
|
24 tri = delaunay (x, y); |
|
25 triplot (tri, x, y); |
|
26 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
27 elseif (strcmp (nm, "griddata")) |
|
28 rand("state",1); |
|
29 x=2*rand(1000,1)-1; |
|
30 y=2*rand(size(x))-1; |
|
31 z=sin(2*(x.^2+y.^2)); |
|
32 [xx,yy]=meshgrid(linspace(-1,1,32)); |
|
33 griddata(x,y,z,xx,yy); |
|
34 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
35 elseif (strcmp (nm, "convhull")) |
|
36 x = -3:0.05:3; |
|
37 y = abs (sin (x)); |
|
38 k = convhull (x, y); |
|
39 plot (x(k),y(k),'r-',x,y,'b+'); |
|
40 axis ([-3.05, 3.05, -0.05, 1.05]); |
|
41 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
42 elseif (strcmp (nm, "delaunay")) |
|
43 rand ("state", 1); |
6847
|
44 x = rand (1, 10); |
|
45 y = rand (1, 10); |
6832
|
46 T = delaunay (x, y); |
|
47 X = [ x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1)) ]; |
|
48 Y = [ y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1)) ]; |
|
49 axis ([0, 1, 0, 1]); |
|
50 plot(X, Y, "b", x, y, "r*"); |
|
51 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
6847
|
52 elseif (strcmp (nm, "inpolygon")) |
|
53 randn ("state", 2); |
|
54 x = randn (100, 1); |
|
55 y = randn (100, 1); |
|
56 vx = cos (pi * [-1 : 0.1: 1]); |
|
57 vy = sin (pi * [-1 : 0.1 : 1]); |
|
58 in = inpolygon (x, y, vx, vy); |
|
59 plot(vx, vy, x(in), y(in), "r+", x(!in), y(!in), "bo"); |
|
60 axis ([-2, 2, -2, 2]); |
|
61 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
6832
|
62 else |
|
63 error ("unrecognized plot requested"); |
|
64 endif |
|
65 bury_output (); |
|
66 endfunction |
|
67 |
|
68 function [r, c] = tri2circ (tri, xx, yy) |
|
69 x = xx(tri); |
|
70 y = yy(tri); |
|
71 m = (y(1:end-1) - y(2:end)) ./ (x(1:end-1) - x(2:end)); |
|
72 xc = (prod(m) .* (y(1) - y(end)) + m(end)*(x(1)+x(2)) - m(1)*(x(2)+x(3))) ... |
|
73 ./ (2 * (m(end) - m(1))); |
|
74 yc = - (xc - (x(2) + x(3))./2) ./ m(end) + (y(2) + y(3)) / 2; |
|
75 c = [xc, yc]; |
|
76 r = sqrt ((xc - x(1)).^2 + (yc - y(1)).^2); |
|
77 endfunction |
|
78 |
|
79 ## Use this function before plotting commands and after every call to |
|
80 ## print since print() resets output to stdout (unfortunately, gnpulot |
|
81 ## can't pop output as it can the terminal type). |
|
82 function bury_output () |
|
83 f = figure (1); |
|
84 set (f, "visible", "off"); |
|
85 endfunction |
|
86 function geometryimages (nm, typ) |
|
87 bury_output (); |
|
88 if (strcmp (nm, "voronoi")) |
|
89 rand("state",9); |
|
90 x = rand(10,1); |
|
91 y = rand(10,1); |
|
92 tri = delaunay (x, y); |
|
93 [vx, vy] = voronoi (x, y, tri); |
|
94 triplot (tri, x, y, "b"); |
|
95 hold on; |
|
96 plot (vx, vy, "r"); |
|
97 [r, c] = tri2circ (tri(end,:), x, y); |
|
98 pc = [-1:0.01:1]; |
|
99 xc = r * sin(pi*pc) + c(1); |
|
100 yc = r * cos(pi*pc) + c(2); |
|
101 plot (xc, yc, "g-", "LineWidth", 3); |
|
102 axis([0, 1, 0, 1]); |
|
103 legend ("Delaunay Triangulation", "Voronoi Diagram"); |
|
104 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
105 elseif (strcmp (nm, "triplot")) |
|
106 rand ("state", 2) |
|
107 x = rand (20, 1); |
|
108 y = rand (20, 1); |
|
109 tri = delaunay (x, y); |
|
110 triplot (tri, x, y); |
|
111 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
112 elseif (strcmp (nm, "griddata")) |
|
113 rand("state",1); |
|
114 x=2*rand(1000,1)-1; |
|
115 y=2*rand(size(x))-1; |
|
116 z=sin(2*(x.^2+y.^2)); |
|
117 [xx,yy]=meshgrid(linspace(-1,1,32)); |
|
118 griddata(x,y,z,xx,yy); |
|
119 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
120 else |
|
121 error ("unrecognized plot requested"); |
|
122 endif |
|
123 bury_output (); |
|
124 endfunction |
|
125 |
|
126 function [r, c] = tri2circ (tri, xx, yy) |
|
127 x = xx(tri); |
|
128 y = yy(tri); |
|
129 m = (y(1:end-1) - y(2:end)) ./ (x(1:end-1) - x(2:end)); |
|
130 xc = (prod(m) .* (y(1) - y(end)) + m(end)*(x(1)+x(2)) - m(1)*(x(2)+x(3))) ... |
|
131 ./ (2 * (m(end) - m(1))); |
|
132 yc = - (xc - (x(2) + x(3))./2) ./ m(end) + (y(2) + y(3)) / 2; |
|
133 c = [xc, yc]; |
|
134 r = sqrt ((xc - x(1)).^2 + (yc - y(1)).^2); |
|
135 endfunction |
|
136 |
|
137 ## Use this function before plotting commands and after every call to |
|
138 ## print since print() resets output to stdout (unfortunately, gnpulot |
|
139 ## can't pop output as it can the terminal type). |
|
140 function bury_output () |
|
141 f = figure (1); |
|
142 set (f, "visible", "off"); |
|
143 endfunction |