Mercurial > hg > octave-lyh
annotate scripts/plot/rectangle.m @ 17198:df4c4b7708a4
Add titles and clean-up plotting %!demos.
* scripts/plot/area.m, scripts/plot/axis.m, scripts/plot/bar.m,
scripts/plot/barh.m, scripts/plot/clabel.m, scripts/plot/colorbar.m,
scripts/plot/comet.m, scripts/plot/comet3.m, scripts/plot/contour.m,
scripts/plot/contour3.m, scripts/plot/contourf.m, scripts/plot/cylinder.m,
scripts/plot/ellipsoid.m, scripts/plot/errorbar.m, scripts/plot/ezplot.m,
scripts/plot/ezplot3.m, scripts/plot/ezpolar.m, scripts/plot/feather.m,
scripts/plot/fplot.m, scripts/plot/hold.m, scripts/plot/isosurface.m,
scripts/plot/legend.m, scripts/plot/loglog.m, scripts/plot/loglogerr.m,
scripts/plot/mesh.m, scripts/plot/meshc.m, scripts/plot/meshz.m,
scripts/plot/patch.m, scripts/plot/pcolor.m, scripts/plot/pie.m,
scripts/plot/pie3.m, scripts/plot/plot.m, scripts/plot/plot3.m,
scripts/plot/polar.m, scripts/plot/rectangle.m, scripts/plot/ribbon.m,
scripts/plot/rose.m, scripts/plot/scatter.m, scripts/plot/scatter3.m,
scripts/plot/semilogx.m, scripts/plot/semilogxerr.m, scripts/plot/semilogy.m,
scripts/plot/semilogyerr.m, scripts/plot/sombrero.m, scripts/plot/stem3.m,
scripts/plot/surf.m, scripts/plot/surfc.m, scripts/plot/surfl.m,
scripts/plot/title.m, scripts/plot/waterfall.m: Add titles and clean-up
plotting %!demos.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 06 Aug 2013 14:34:20 -0700 |
parents | 26589abbc78d |
children | 87ba70043bfc |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14019
diff
changeset
|
1 ## Copyright (C) 2012 David Bateman |
12697 | 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 | |
19 ## -*- texinfo -*- | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
20 ## @deftypefn {Function File} {} rectangle () |
12697 | 21 ## @deftypefnx {Function File} {} rectangle (@dots{}, "Position", @var{pos}) |
22 ## @deftypefnx {Function File} {} rectangle (@dots{}, "Curvature", @var{curv}) | |
23 ## @deftypefnx {Function File} {} rectangle (@dots{}, "EdgeColor", @var{ec}) | |
24 ## @deftypefnx {Function File} {} rectangle (@dots{}, "FaceColor", @var{fc}) | |
17126
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
25 ## @deftypefnx {Function File} {} rectangle (@var{hax}, @dots{}) |
12697 | 26 ## @deftypefnx {Function File} {@var{h} =} rectangle (@dots{}) |
27 ## | |
17126
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
28 ## Draw a rectangular patch defined by @var{pos} and @var{curv}. |
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
29 ## |
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
30 ## The variable @code{@var{pos}(1:2)} defines the lower left-hand corner of |
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
31 ## the patch and @code{@var{pos}(3:4)} defines its width and height. By |
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
32 ## default, the value of @var{pos} is @code{[0, 0, 1, 1]}. |
12697 | 33 ## |
34 ## The variable @var{curv} defines the curvature of the sides of the rectangle | |
12761
13bcd62824a7
doc: Add documentation for gmres, rectangle to manual
Rik <octave@nomad.inbox5.com>
parents:
12698
diff
changeset
|
35 ## and may be a scalar or two-element vector with values between 0 and 1. |
13bcd62824a7
doc: Add documentation for gmres, rectangle to manual
Rik <octave@nomad.inbox5.com>
parents:
12698
diff
changeset
|
36 ## A value of 0 represents no curvature of the side, whereas a value of 1 |
13bcd62824a7
doc: Add documentation for gmres, rectangle to manual
Rik <octave@nomad.inbox5.com>
parents:
12698
diff
changeset
|
37 ## means that the side is entirely curved into the arc of a circle. |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12763
diff
changeset
|
38 ## If @var{curv} is a two-element vector, then the first element is the |
12697 | 39 ## curvature along the x-axis of the patch and the second along y-axis. |
40 ## | |
41 ## If @var{curv} is a scalar, it represents the curvature of the shorter of the | |
42 ## two sides of the rectangle and the curvature of the other side is defined | |
43 ## by | |
44 ## | |
45 ## @example | |
12763
f511bfe00d14
maint: Periodic merge of stable to default
Rik <octave@nomad.inbox5.com>
parents:
12761
diff
changeset
|
46 ## min (pos (1:2)) / max (pos (1:2)) * curv |
12697 | 47 ## @end example |
48 ## | |
17126
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
49 ## Additional property/value pairs are passed to the underlying patch command. |
14001
5f0bb45e615c
doc: Update documentation for functions returning a graphics handle h (Bug #34761)
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
50 ## |
17126
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
51 ## If the first argument @var{hax} is an axes handle, then plot into this axis, |
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
52 ## rather than the current axes returned by @code{gca}. |
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
53 ## |
14001
5f0bb45e615c
doc: Update documentation for functions returning a graphics handle h (Bug #34761)
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
54 ## The optional return value @var{h} is a graphics handle to the created |
5f0bb45e615c
doc: Update documentation for functions returning a graphics handle h (Bug #34761)
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
55 ## rectangle object. |
12697 | 56 ## @end deftypefn |
17126
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
57 ## @seealso{patch, line, cylinder, ellipsoid, sphere} |
12697 | 58 |
59 function h = rectangle (varargin) | |
60 | |
61 [hax, varargin] = __plt_get_axis_arg__ ("rectangle", varargin{:}); | |
62 | |
17051
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
63 oldfig = ifelse (isempty (hax), [], get (0, "currentfigure")); |
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
64 unwind_protect |
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
65 hax = newplot (hax); |
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
66 |
17130
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17126
diff
changeset
|
67 htmp = __rectangle__ (hax, varargin{:}); |
17051
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
68 unwind_protect_cleanup |
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
69 if (! isempty (oldfig)) |
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
70 set (0, "currentfigure", oldfig); |
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
71 endif |
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
72 end_unwind_protect |
12697 | 73 |
74 if (nargout > 0) | |
17051
3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
75 h = htmp; |
12697 | 76 endif |
77 endfunction | |
78 | |
79 function hg = __rectangle__ (hax, varargin) | |
80 | |
81 iarg = 1; | |
82 pos = [0, 0, 1, 1]; | |
83 curv2 = [0, 0]; | |
84 ec = [0, 0, 0]; | |
85 fc = "none"; | |
86 | |
87 while (iarg < length (varargin)) | |
88 arg = varargin{iarg}; | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14245
diff
changeset
|
89 if (ischar (arg)) |
12697 | 90 if (strcmpi (arg, "position")) |
91 pos = varargin{iarg+1}; | |
92 varargin(iarg:iarg+1) = []; | |
93 if (!isvector (pos) || numel (pos) != 4) | |
94 error ("rectangle: position must be a 4 element vector"); | |
95 endif | |
96 elseif (strcmpi (arg, "curvature")) | |
97 curv2 = varargin{iarg+1}; | |
98 varargin(iarg:iarg+1) = []; | |
99 if (!isnumeric (curv2) || (numel (curv2) != 1 && numel (curv2) != 2)) | |
100 error ("rectangle: curvature must be a 2 element vector or a scalar"); | |
101 endif | |
102 if (any (curv2 < 0) || any (curv2 > 1)) | |
103 error ("rectangle: curvature values must be between 0 and 1"); | |
104 endif | |
105 elseif (strcmpi (arg, "edgecolor")) | |
106 ec = varargin{iarg+1}; | |
107 varargin(iarg:iarg+1) = []; | |
108 elseif (strcmpi (arg, "facecolor")) | |
109 fc = varargin{iarg+1}; | |
110 varargin(iarg:iarg+1) = []; | |
111 else | |
112 iarg ++; | |
113 endif | |
114 else | |
115 iarg ++; | |
116 endif | |
117 endwhile | |
118 | |
119 if (numel (curv2) == 1) | |
120 [a, ai] = min (pos (3 : 4)); | |
121 [b, bi] = max (pos (3 : 4)); | |
122 if (ai < bi) | |
123 curv = [curv2, curv2 .* a ./ b]; | |
124 else | |
125 curv = [curv2 .* a ./ b, curv2]; | |
126 endif | |
127 else | |
128 curv = curv2; | |
129 endif | |
130 | |
131 if (all (curv) < 0.01) | |
132 ## Special case : no curvature | |
133 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; | |
134 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; | |
135 else | |
136 p = pi / 2 * [0 : 15] / 15; | |
137 c = curv .* pos(3 : 4) / 2; | |
138 cx = c(1) * sin (p) - c(1); | |
139 cy = c(2) * cos (p) - c(2); | |
140 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... | |
141 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; | |
142 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... | |
143 pos(2) + pos(4) + cy, pos(2) + c(2)]; | |
144 endif | |
145 | |
146 hg = hggroup (); | |
147 | |
148 h = patch ("xdata", x(:), "ydata", y(:), "facecolor", fc, "edgecolor", ec, ... | |
149 "parent", hg, varargin{:}); | |
150 | |
151 addproperty ("curvature", hg, "data", curv2); | |
152 addproperty ("position", hg, "data", pos); | |
153 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor")); | |
154 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth")); | |
155 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle")); | |
156 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor")); | |
157 | |
158 addlistener (hg, "curvature", @update_data); | |
159 addlistener (hg, "position", @update_data); | |
160 addlistener (hg, "edgecolor", @update_props); | |
161 addlistener (hg, "linewidth", @update_props); | |
162 addlistener (hg, "linestyle", @update_props); | |
163 addlistener (hg, "facecolor", @update_props); | |
164 endfunction | |
165 | |
166 function update_data (h, d) | |
167 persistent recursion = false; | |
168 | |
169 ## Don't allow recursion | |
170 if (!recursion) | |
171 unwind_protect | |
172 recursion = true; | |
173 | |
174 kids = get (h, "children"); | |
175 pos = get (h, "position"); | |
176 curv2 = get (h, "curvature"); | |
177 | |
178 if (numel (curv2) == 1) | |
179 [a, ai] = min (pos (3 : 4)); | |
180 [b, bi] = max (pos (3 : 4)); | |
181 if (ai < bi) | |
182 curv = [curv2, curv2 .* a ./ b]; | |
183 else | |
184 curv = [curv2 .* a ./ b, curv2]; | |
185 endif | |
186 else | |
187 curv = curv2; | |
188 endif | |
189 | |
190 if (all (curv) < 0.01) | |
191 ## Special case : no curvature | |
192 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; | |
193 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; | |
194 else | |
195 p = pi / 2 * [0 : 15] / 15; | |
196 c = curv .* pos(3 : 4) / 2; | |
197 cx = c(1) * sin (p) - c(1); | |
198 cy = c(2) * cos (p) - c(2); | |
199 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... | |
200 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; | |
201 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... | |
202 pos(2) + pos(4) + cy, pos(2) + c(2)]; | |
203 endif | |
204 | |
205 set (kids, "xdata", x, "ydata", y); | |
206 unwind_protect_cleanup | |
207 recursion = false; | |
208 end_unwind_protect | |
209 endif | |
210 endfunction | |
211 | |
212 function update_props (h, d) | |
213 kids = get (h, "children"); | |
214 set (kids, "edgecolor", get (h, "edgecolor"), | |
17130
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17126
diff
changeset
|
215 "linewidth", get (h, "linewidth"), |
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17126
diff
changeset
|
216 "linestyle", get (h, "linestyle"), |
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17126
diff
changeset
|
217 "facecolor", get (h, "facecolor")); |
12697 | 218 endfunction |
219 | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
220 |
12697 | 221 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
222 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
223 %! axis equal; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
224 %! rectangle ('Position', [0.05, 0.05, 0.9, 0.9], 'Curvature', [0.5, 0.5]); |
17198
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17130
diff
changeset
|
225 %! title ('rectangle() with corners curved'); |
12697 | 226 |
227 %!demo | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
228 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
229 %! axis equal; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
230 %! rectangle ('Position', [0.05, 0.05, 0.9, 0.4], 'Curvature', 1.0); |
17198
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17130
diff
changeset
|
231 %! title ('rectangle() with sides as complete arcs'); |
12697 | 232 |
233 %!demo | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
234 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
235 %! axis equal; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
236 %! h = rectangle ('Position', [0.05, 0.05, 0.9, 0.4], 'Curvature', 1.0); |
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
237 %! set (h, 'FaceColor', [0, 1, 0]); |
17198
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17130
diff
changeset
|
238 %! title ('rectangle() with FaceColor = green'); |
12697 | 239 |