Mercurial > octave-nkf
annotate doc/interpreter/geometryimages.m @ 19731:4197fc428c7d
maint: Update copyright notices for 2015.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 11 Feb 2015 14:19:08 -0500 |
parents | 446c46af4b42 |
children | 1170c849952b |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19627
diff
changeset
|
1 ## Copyright (C) 2007-2015 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") |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
33 && any (strcmp (nm, {"voronoi", "griddata", "convhull", "delaunay", ... |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
34 "triplot"}))) |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
35 sombreroimage (nm, typ, d_typ); |
7262 | 36 elseif (strcmp (typ, "txt")) |
37 image_as_txt (nm); | |
6855 | 38 elseif (strcmp (nm, "voronoi")) |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
39 rand ("state", 9); |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
40 x = rand (10, 1); |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
41 y = rand (10, 1); |
6832 | 42 tri = delaunay (x, y); |
43 [vx, vy] = voronoi (x, y, tri); | |
44 triplot (tri, x, y, "b"); | |
45 hold on; | |
46 plot (vx, vy, "r"); | |
47 [r, c] = tri2circ (tri(end,:), x, y); | |
48 pc = [-1:0.01:1]; | |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
49 xc = r * sin (pi*pc) + c(1); |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
50 yc = r * cos (pi*pc) + c(2); |
6832 | 51 plot (xc, yc, "g-", "LineWidth", 3); |
52 axis([0, 1, 0, 1]); | |
53 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
|
54 print ([nm "." typ], d_typ); |
6832 | 55 elseif (strcmp (nm, "triplot")) |
56 rand ("state", 2) | |
57 x = rand (20, 1); | |
58 y = rand (20, 1); | |
59 tri = delaunay (x, y); | |
60 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
|
61 print ([nm "." typ], d_typ); |
6832 | 62 elseif (strcmp (nm, "griddata")) |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
63 rand ("state", 1); |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
64 x = 2 * rand (1000,1) - 1; |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
65 y = 2 * rand (size (x)) - 1; |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
66 z = sin (2 * (x.^2 + y.^2)); |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
67 [xx,yy] = meshgrid (linspace (-1,1,32)); |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
68 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
|
69 print ([nm "." typ], d_typ); |
6832 | 70 elseif (strcmp (nm, "convhull")) |
71 x = -3:0.05:3; | |
72 y = abs (sin (x)); | |
73 k = convhull (x, y); | |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
74 plot (x(k),y(k),'r-', x,y,'b+'); |
6832 | 75 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
|
76 print ([nm "." typ], d_typ); |
6832 | 77 elseif (strcmp (nm, "delaunay")) |
78 rand ("state", 1); | |
6847 | 79 x = rand (1, 10); |
80 y = rand (1, 10); | |
6832 | 81 T = delaunay (x, y); |
82 X = [ x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1)) ]; | |
83 Y = [ y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1)) ]; | |
84 axis ([0, 1, 0, 1]); | |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
85 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
|
86 print ([nm "." typ], d_typ); |
6847 | 87 elseif (strcmp (nm, "inpolygon")) |
88 randn ("state", 2); | |
89 x = randn (100, 1); | |
90 y = randn (100, 1); | |
91 vx = cos (pi * [-1 : 0.1: 1]); | |
92 vy = sin (pi * [-1 : 0.1 : 1]); | |
93 in = inpolygon (x, y, vx, vy); | |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
94 plot (vx, vy, x(in), y(in), "r+", x(!in), y(!in), "bo"); |
6847 | 95 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
|
96 print ([nm "." typ], d_typ); |
6832 | 97 else |
98 error ("unrecognized plot requested"); | |
99 endif | |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
100 hide_output (); |
6832 | 101 endfunction |
102 | |
103 function [r, c] = tri2circ (tri, xx, yy) | |
104 x = xx(tri); | |
105 y = yy(tri); | |
106 m = (y(1:end-1) - y(2:end)) ./ (x(1:end-1) - x(2:end)); | |
107 xc = (prod(m) .* (y(1) - y(end)) + m(end)*(x(1)+x(2)) - m(1)*(x(2)+x(3))) ... | |
19627
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
108 ./ (2 * (m(end) - m(1))); |
6832 | 109 yc = - (xc - (x(2) + x(3))./2) ./ m(end) + (y(2) + y(3)) / 2; |
110 c = [xc, yc]; | |
111 r = sqrt ((xc - x(1)).^2 + (yc - y(1)).^2); | |
112 endfunction | |
113 | |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
114 function set_print_size () |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
115 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
|
116 border = 0; # For postscript use 50/72 |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
117 set (0, "defaultfigurepapertype", "<custom>"); |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
118 set (0, "defaultfigurepaperorientation", "landscape"); |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
119 set (0, "defaultfigurepapersize", image_size + 2*border); |
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
120 set (0, "defaultfigurepaperposition", [border, border, image_size]); |
6832 | 121 endfunction |
122 | |
123 ## Use this function before plotting commands and after every call to | |
124 ## print since print() resets output to stdout (unfortunately, gnpulot | |
125 ## 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
|
126 function hide_output () |
6832 | 127 f = figure (1); |
128 set (f, "visible", "off"); | |
129 endfunction | |
6855 | 130 |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
131 function sombreroimage (nm, typ, d_typ) |
6855 | 132 if (strcmp (typ, "txt")) |
133 fid = fopen (sprintf ("%s.txt", nm), "wt"); | |
134 fputs (fid, "+-----------------------------+\n"); | |
135 fputs (fid, "| Image unavailable because |\n"); | |
136 fputs (fid, "| of a missing QHULL library. |\n"); | |
137 fputs (fid, "+-----------------------------+\n"); | |
138 fclose (fid); | |
139 return; | |
140 else ## if (!strcmp (typ, "txt")) | |
141 | |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
142 hide_output (); |
6855 | 143 |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
144 [x, y, z] = sombrero (); |
6855 | 145 unwind_protect |
146 mesh (x, y, z); | |
147 title ("Sorry, graphics not available because octave was\\ncompiled without the QHULL library."); | |
148 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
|
149 print ([nm "." typ], d_typ); |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
150 hide_output (); |
6855 | 151 end_unwind_protect |
152 endif | |
153 endfunction | |
7262 | 154 |
155 ## generate something for the texinfo @image command to process | |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
156 function image_as_txt (nm) |
7262 | 157 fid = fopen (sprintf ("%s.txt", nm), "wt"); |
158 fputs (fid, "\n"); | |
159 fputs (fid, "+---------------------------------+\n"); | |
160 fputs (fid, "| Image unavailable in text mode. |\n"); | |
161 fputs (fid, "+---------------------------------+\n"); | |
162 fclose (fid); | |
163 endfunction | |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
164 |