Mercurial > octave-nkf
annotate doc/interpreter/geometryimages.m @ 16994:333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
matrix concatenation is ~80% faster than cstrcat().
* doc/interpreter/strings.txi: Document preference for matrix concatenation
as opposed to alternatives.
* doc/interpreter/geometryimages.m, doc/interpreter/interpimages.m,
doc/interpreter/plotimages.m, doc/interpreter/sparseimages.m,
doc/interpreter/splineimages.m, scripts/general/genvarname.m,
scripts/general/int2str.m, scripts/general/num2str.m,
scripts/help/__makeinfo__.m, scripts/help/help.m,
scripts/miscellaneous/copyfile.m, scripts/miscellaneous/dir.m,
scripts/miscellaneous/edit.m, scripts/miscellaneous/fact.m,
scripts/miscellaneous/fullfile.m, scripts/miscellaneous/mkoctfile.m,
scripts/miscellaneous/movefile.m, scripts/miscellaneous/perl.m,
scripts/miscellaneous/python.m, scripts/miscellaneous/run.m,
scripts/miscellaneous/tempdir.m, scripts/miscellaneous/unpack.m,
scripts/pkg/private/configure_make.m, scripts/pkg/private/create_pkgadddel.m,
scripts/pkg/private/extract_pkg.m, scripts/pkg/private/get_description.m,
scripts/pkg/private/get_forge_pkg.m, scripts/pkg/private/getarch.m,
scripts/pkg/private/getarchprefix.m, scripts/pkg/private/install.m,
scripts/pkg/private/installed_packages.m,
scripts/pkg/private/load_packages_and_dependencies.m,
scripts/pkg/private/rebuild.m, scripts/pkg/private/repackage.m,
scripts/pkg/private/shell.m, scripts/pkg/private/uninstall.m,
scripts/plot/private/__go_draw_axes__.m, scripts/signal/spectral_adf.m,
scripts/signal/spectral_xdf.m, scripts/statistics/tests/z_test.m,
scripts/statistics/tests/z_test_2.m, scripts/strings/mat2str.m,
scripts/strings/strtok.m, scripts/testfun/__run_test_suite__.m,
scripts/testfun/assert.m, scripts/testfun/demo.m, scripts/testfun/speed.m,
scripts/testfun/test.m, test/eval-catch.tst, test/io.tst, test/try.tst: Replace
cstrcat() with matrix concatenation where possible.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 17 Jul 2013 14:02:32 -0700 |
parents | 2a4f83826024 |
children | 8b692d9ea54e |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11576
diff
changeset
|
1 ## Copyright (C) 2007-2012 David Bateman |
7018 | 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) |
11576
8ac9687dbe9f
rename backend to graphics_toolkit
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
20 graphics_toolkit ("gnuplot"); |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
21 set_print_size (); |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
22 hide_output (); |
7257 | 23 if (strcmp (typ, "png")) |
24 set (0, "defaulttextfontname", "*"); | |
25 endif | |
9914
e76d8c767584
Switch to color output for PostScript documentation images
Rik <rdrider0-list@yahoo.com>
parents:
9912
diff
changeset
|
26 if (strcmp (typ, "eps")) |
e76d8c767584
Switch to color output for PostScript documentation images
Rik <rdrider0-list@yahoo.com>
parents:
9912
diff
changeset
|
27 d_typ = "-depsc2"; |
e76d8c767584
Switch to color output for PostScript documentation images
Rik <rdrider0-list@yahoo.com>
parents:
9912
diff
changeset
|
28 else |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
29 d_typ = ["-d" typ]; |
9914
e76d8c767584
Switch to color output for PostScript documentation images
Rik <rdrider0-list@yahoo.com>
parents:
9912
diff
changeset
|
30 endif |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
31 |
16170
2a4f83826024
new way to test for features in Octave scripts
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
32 if (! __have_feature__ ("QHULL") |
6878 | 33 && (strcmp (nm, "voronoi") || strcmp (nm, "griddata") |
11033
d9c8916bb9dd
Untabify a few remaining .m scripts.
Rik <octave@nomad.inbox5.com>
parents:
10010
diff
changeset
|
34 || strcmp (nm, "convhull") || strcmp (nm, "delaunay") |
d9c8916bb9dd
Untabify a few remaining .m scripts.
Rik <octave@nomad.inbox5.com>
parents:
10010
diff
changeset
|
35 || strcmp (nm, "triplot"))) |
6855 | 36 sombreroimage (nm, typ); |
7262 | 37 elseif (strcmp (typ, "txt")) |
38 image_as_txt (nm); | |
6855 | 39 elseif (strcmp (nm, "voronoi")) |
6832 | 40 rand("state",9); |
41 x = rand(10,1); | |
42 y = rand(10,1); | |
43 tri = delaunay (x, y); | |
44 [vx, vy] = voronoi (x, y, tri); | |
45 triplot (tri, x, y, "b"); | |
46 hold on; | |
47 plot (vx, vy, "r"); | |
48 [r, c] = tri2circ (tri(end,:), x, y); | |
49 pc = [-1:0.01:1]; | |
50 xc = r * sin(pi*pc) + c(1); | |
51 yc = r * cos(pi*pc) + c(2); | |
52 plot (xc, yc, "g-", "LineWidth", 3); | |
53 axis([0, 1, 0, 1]); | |
54 legend ("Delaunay Triangulation", "Voronoi Diagram"); | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
55 print ([nm "." typ], d_typ); |
6832 | 56 elseif (strcmp (nm, "triplot")) |
57 rand ("state", 2) | |
58 x = rand (20, 1); | |
59 y = rand (20, 1); | |
60 tri = delaunay (x, y); | |
61 triplot (tri, x, y); | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
62 print ([nm "." typ], d_typ); |
6832 | 63 elseif (strcmp (nm, "griddata")) |
64 rand("state",1); | |
65 x=2*rand(1000,1)-1; | |
66 y=2*rand(size(x))-1; | |
67 z=sin(2*(x.^2+y.^2)); | |
68 [xx,yy]=meshgrid(linspace(-1,1,32)); | |
69 griddata(x,y,z,xx,yy); | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
70 print ([nm "." typ], d_typ); |
6832 | 71 elseif (strcmp (nm, "convhull")) |
72 x = -3:0.05:3; | |
73 y = abs (sin (x)); | |
74 k = convhull (x, y); | |
75 plot (x(k),y(k),'r-',x,y,'b+'); | |
76 axis ([-3.05, 3.05, -0.05, 1.05]); | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
77 print ([nm "." typ], d_typ); |
6832 | 78 elseif (strcmp (nm, "delaunay")) |
79 rand ("state", 1); | |
6847 | 80 x = rand (1, 10); |
81 y = rand (1, 10); | |
6832 | 82 T = delaunay (x, y); |
83 X = [ x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1)) ]; | |
84 Y = [ y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1)) ]; | |
85 axis ([0, 1, 0, 1]); | |
86 plot(X, Y, "b", x, y, "r*"); | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
87 print ([nm "." typ], d_typ); |
6847 | 88 elseif (strcmp (nm, "inpolygon")) |
89 randn ("state", 2); | |
90 x = randn (100, 1); | |
91 y = randn (100, 1); | |
92 vx = cos (pi * [-1 : 0.1: 1]); | |
93 vy = sin (pi * [-1 : 0.1 : 1]); | |
94 in = inpolygon (x, y, vx, vy); | |
95 plot(vx, vy, x(in), y(in), "r+", x(!in), y(!in), "bo"); | |
96 axis ([-2, 2, -2, 2]); | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
97 print ([nm "." typ], d_typ); |
6832 | 98 else |
99 error ("unrecognized plot requested"); | |
100 endif | |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
101 hide_output (); |
6832 | 102 endfunction |
103 | |
104 function [r, c] = tri2circ (tri, xx, yy) | |
105 x = xx(tri); | |
106 y = yy(tri); | |
107 m = (y(1:end-1) - y(2:end)) ./ (x(1:end-1) - x(2:end)); | |
108 xc = (prod(m) .* (y(1) - y(end)) + m(end)*(x(1)+x(2)) - m(1)*(x(2)+x(3))) ... | |
109 ./ (2 * (m(end) - m(1))); | |
110 yc = - (xc - (x(2) + x(3))./2) ./ m(end) + (y(2) + y(3)) / 2; | |
111 c = [xc, yc]; | |
112 r = sqrt ((xc - x(1)).^2 + (yc - y(1)).^2); | |
113 endfunction | |
114 | |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
115 function set_print_size () |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
116 image_size = [5.0, 3.5]; # in inches, 16:9 format |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
117 border = 0; # For postscript use 50/72 |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
118 set (0, "defaultfigurepapertype", "<custom>"); |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
119 set (0, "defaultfigurepaperorientation", "landscape"); |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
120 set (0, "defaultfigurepapersize", image_size + 2*border); |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
121 set (0, "defaultfigurepaperposition", [border, border, image_size]); |
6832 | 122 endfunction |
123 | |
124 ## Use this function before plotting commands and after every call to | |
125 ## print since print() resets output to stdout (unfortunately, gnpulot | |
126 ## can't pop output as it can the terminal type). | |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
127 function hide_output () |
6832 | 128 f = figure (1); |
129 set (f, "visible", "off"); | |
130 endfunction | |
6855 | 131 |
132 function sombreroimage (nm, typ) | |
133 if (strcmp (typ, "txt")) | |
134 fid = fopen (sprintf ("%s.txt", nm), "wt"); | |
135 fputs (fid, "+-----------------------------+\n"); | |
136 fputs (fid, "| Image unavailable because |\n"); | |
137 fputs (fid, "| of a missing QHULL library. |\n"); | |
138 fputs (fid, "+-----------------------------+\n"); | |
139 fclose (fid); | |
140 return; | |
141 else ## if (!strcmp (typ, "txt")) | |
142 | |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
143 hide_output (); |
10010
c5e9931c7ba7
Correctly produce postcript output for geometryimages when QHULL library is not present
Rik <rdrider0-list@yahoo.com>
parents:
9914
diff
changeset
|
144 if (strcmp (typ, "eps")) |
c5e9931c7ba7
Correctly produce postcript output for geometryimages when QHULL library is not present
Rik <rdrider0-list@yahoo.com>
parents:
9914
diff
changeset
|
145 d_typ = "-depsc2"; |
c5e9931c7ba7
Correctly produce postcript output for geometryimages when QHULL library is not present
Rik <rdrider0-list@yahoo.com>
parents:
9914
diff
changeset
|
146 else |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
147 d_typ = ["-d" typ]; |
10010
c5e9931c7ba7
Correctly produce postcript output for geometryimages when QHULL library is not present
Rik <rdrider0-list@yahoo.com>
parents:
9914
diff
changeset
|
148 endif |
6855 | 149 |
150 x = y = linspace (-8, 8, 41)'; | |
151 [xx, yy] = meshgrid (x, y); | |
152 r = sqrt (xx .^ 2 + yy .^ 2) + eps; | |
153 z = sin (r) ./ r; | |
154 unwind_protect | |
155 mesh (x, y, z); | |
156 title ("Sorry, graphics not available because octave was\\ncompiled without the QHULL library."); | |
157 unwind_protect_cleanup | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
158 print ([nm "." typ], d_typ); |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
159 hide_output (); |
6855 | 160 end_unwind_protect |
161 endif | |
162 endfunction | |
7262 | 163 |
164 ## generate something for the texinfo @image command to process | |
165 function image_as_txt(nm) | |
166 fid = fopen (sprintf ("%s.txt", nm), "wt"); | |
167 fputs (fid, "\n"); | |
168 fputs (fid, "+---------------------------------+\n"); | |
169 fputs (fid, "| Image unavailable in text mode. |\n"); | |
170 fputs (fid, "+---------------------------------+\n"); | |
171 fclose (fid); | |
172 endfunction |