Mercurial > hg > octave-nkf
annotate scripts/plot/rectangle.m @ 17302:6ba5b1dadd61
plotmatrix.m: Replace ifelse() construction with if/endif.
* scripts/plot/plotmatrix.m: Replace ifelse() construction with if/endif.
Move argument return out of unwind_protect to end of function.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 20 Aug 2013 22:35:08 -0700 |
parents | 68bcac3c043a |
children |
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}) | |
17122
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 ## | |
17122
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 ## | |
17122
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 ## |
17122
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 |
17122
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 | |
17211
87ba70043bfc
Don't use ifelse in plot fcns to avoid unnecessary fcn evaluations.
Rik <rik@octave.org>
parents:
17190
diff
changeset
|
63 oldfig = []; |
17301
68bcac3c043a
Correct inversion accidentally introduced in cset 87ba70043bfc.
Rik <rik@octave.org>
parents:
17211
diff
changeset
|
64 if (! isempty (hax)) |
17211
87ba70043bfc
Don't use ifelse in plot fcns to avoid unnecessary fcn evaluations.
Rik <rik@octave.org>
parents:
17190
diff
changeset
|
65 oldfig = get (0, "currentfigure"); |
87ba70043bfc
Don't use ifelse in plot fcns to avoid unnecessary fcn evaluations.
Rik <rik@octave.org>
parents:
17190
diff
changeset
|
66 endif |
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
|
67 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
|
68 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
|
69 |
17126
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 end_unwind_protect |
12697 | 76 |
77 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
|
78 h = htmp; |
12697 | 79 endif |
80 endfunction | |
81 | |
82 function hg = __rectangle__ (hax, varargin) | |
83 | |
84 iarg = 1; | |
85 pos = [0, 0, 1, 1]; | |
86 curv2 = [0, 0]; | |
87 ec = [0, 0, 0]; | |
88 fc = "none"; | |
89 | |
90 while (iarg < length (varargin)) | |
91 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
|
92 if (ischar (arg)) |
12697 | 93 if (strcmpi (arg, "position")) |
94 pos = varargin{iarg+1}; | |
95 varargin(iarg:iarg+1) = []; | |
96 if (!isvector (pos) || numel (pos) != 4) | |
97 error ("rectangle: position must be a 4 element vector"); | |
98 endif | |
99 elseif (strcmpi (arg, "curvature")) | |
100 curv2 = varargin{iarg+1}; | |
101 varargin(iarg:iarg+1) = []; | |
102 if (!isnumeric (curv2) || (numel (curv2) != 1 && numel (curv2) != 2)) | |
103 error ("rectangle: curvature must be a 2 element vector or a scalar"); | |
104 endif | |
105 if (any (curv2 < 0) || any (curv2 > 1)) | |
106 error ("rectangle: curvature values must be between 0 and 1"); | |
107 endif | |
108 elseif (strcmpi (arg, "edgecolor")) | |
109 ec = varargin{iarg+1}; | |
110 varargin(iarg:iarg+1) = []; | |
111 elseif (strcmpi (arg, "facecolor")) | |
112 fc = varargin{iarg+1}; | |
113 varargin(iarg:iarg+1) = []; | |
114 else | |
115 iarg ++; | |
116 endif | |
117 else | |
118 iarg ++; | |
119 endif | |
120 endwhile | |
121 | |
122 if (numel (curv2) == 1) | |
123 [a, ai] = min (pos (3 : 4)); | |
124 [b, bi] = max (pos (3 : 4)); | |
125 if (ai < bi) | |
126 curv = [curv2, curv2 .* a ./ b]; | |
127 else | |
128 curv = [curv2 .* a ./ b, curv2]; | |
129 endif | |
130 else | |
131 curv = curv2; | |
132 endif | |
133 | |
134 if (all (curv) < 0.01) | |
135 ## Special case : no curvature | |
136 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; | |
137 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; | |
138 else | |
139 p = pi / 2 * [0 : 15] / 15; | |
140 c = curv .* pos(3 : 4) / 2; | |
141 cx = c(1) * sin (p) - c(1); | |
142 cy = c(2) * cos (p) - c(2); | |
143 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... | |
144 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; | |
145 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... | |
146 pos(2) + pos(4) + cy, pos(2) + c(2)]; | |
147 endif | |
148 | |
149 hg = hggroup (); | |
150 | |
151 h = patch ("xdata", x(:), "ydata", y(:), "facecolor", fc, "edgecolor", ec, ... | |
152 "parent", hg, varargin{:}); | |
153 | |
154 addproperty ("curvature", hg, "data", curv2); | |
155 addproperty ("position", hg, "data", pos); | |
156 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor")); | |
157 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth")); | |
158 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle")); | |
159 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor")); | |
160 | |
161 addlistener (hg, "curvature", @update_data); | |
162 addlistener (hg, "position", @update_data); | |
163 addlistener (hg, "edgecolor", @update_props); | |
164 addlistener (hg, "linewidth", @update_props); | |
165 addlistener (hg, "linestyle", @update_props); | |
166 addlistener (hg, "facecolor", @update_props); | |
167 endfunction | |
168 | |
169 function update_data (h, d) | |
170 persistent recursion = false; | |
171 | |
172 ## Don't allow recursion | |
173 if (!recursion) | |
174 unwind_protect | |
175 recursion = true; | |
176 | |
177 kids = get (h, "children"); | |
178 pos = get (h, "position"); | |
179 curv2 = get (h, "curvature"); | |
180 | |
181 if (numel (curv2) == 1) | |
182 [a, ai] = min (pos (3 : 4)); | |
183 [b, bi] = max (pos (3 : 4)); | |
184 if (ai < bi) | |
185 curv = [curv2, curv2 .* a ./ b]; | |
186 else | |
187 curv = [curv2 .* a ./ b, curv2]; | |
188 endif | |
189 else | |
190 curv = curv2; | |
191 endif | |
192 | |
193 if (all (curv) < 0.01) | |
194 ## Special case : no curvature | |
195 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; | |
196 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; | |
197 else | |
198 p = pi / 2 * [0 : 15] / 15; | |
199 c = curv .* pos(3 : 4) / 2; | |
200 cx = c(1) * sin (p) - c(1); | |
201 cy = c(2) * cos (p) - c(2); | |
202 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... | |
203 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; | |
204 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... | |
205 pos(2) + pos(4) + cy, pos(2) + c(2)]; | |
206 endif | |
207 | |
208 set (kids, "xdata", x, "ydata", y); | |
209 unwind_protect_cleanup | |
210 recursion = false; | |
211 end_unwind_protect | |
212 endif | |
213 endfunction | |
214 | |
215 function update_props (h, d) | |
216 kids = get (h, "children"); | |
217 set (kids, "edgecolor", get (h, "edgecolor"), | |
17126
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
218 "linewidth", get (h, "linewidth"), |
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
219 "linestyle", get (h, "linestyle"), |
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
220 "facecolor", get (h, "facecolor")); |
12697 | 221 endfunction |
222 | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
223 |
12697 | 224 %!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
|
225 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
226 %! axis equal; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
227 %! rectangle ('Position', [0.05, 0.05, 0.9, 0.9], 'Curvature', [0.5, 0.5]); |
17190
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17126
diff
changeset
|
228 %! title ('rectangle() with corners curved'); |
12697 | 229 |
230 %!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
|
231 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
232 %! axis equal; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
233 %! rectangle ('Position', [0.05, 0.05, 0.9, 0.4], 'Curvature', 1.0); |
17190
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17126
diff
changeset
|
234 %! title ('rectangle() with sides as complete arcs'); |
12697 | 235 |
236 %!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
|
237 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
238 %! axis equal; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
239 %! 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
|
240 %! set (h, 'FaceColor', [0, 1, 0]); |
17190
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17126
diff
changeset
|
241 %! title ('rectangle() with FaceColor = green'); |
12697 | 242 |