Mercurial > hg > octave-lyh
annotate scripts/plot/rectangle.m @ 17051:3e1b24a2454a
cylinder.m, ellipsoid.m, rectangle.m, sphere.m: Update to use new __plt_get_axis_arg__.
* scripts/plot/cylinder.m, scripts/plot/ellipsoid.m, scripts/plot/rectangle.m,
scripts/plot/sphere.m: Update to use new __plt_get_axis_arg__.
Rename ax to hax.
author | Pantxo Diribarne <pantxo.diribarne@gmail.com> |
---|---|
date | Tue, 23 Jul 2013 16:06:07 +0200 |
parents | 5d3a684236b0 |
children | eaab03308c0b |
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}) | |
25 ## @deftypefnx {Function File} {@var{h} =} rectangle (@dots{}) | |
26 ## | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
27 ## Draw rectangular patch defined by @var{pos} and @var{curv}. The variable |
12763
f511bfe00d14
maint: Periodic merge of stable to default
Rik <octave@nomad.inbox5.com>
parents:
12761
diff
changeset
|
28 ## @code{@var{pos}(1:2)} defines the lower left-hand corner of the patch |
f511bfe00d14
maint: Periodic merge of stable to default
Rik <octave@nomad.inbox5.com>
parents:
12761
diff
changeset
|
29 ## and @code{@var{pos}(3:4)} defines its width and height. By default, the |
12761
13bcd62824a7
doc: Add documentation for gmres, rectangle to manual
Rik <octave@nomad.inbox5.com>
parents:
12698
diff
changeset
|
30 ## value of @var{pos} is @code{[0, 0, 1, 1]}. |
12697 | 31 ## |
32 ## 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
|
33 ## 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
|
34 ## 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
|
35 ## 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
|
36 ## If @var{curv} is a two-element vector, then the first element is the |
12697 | 37 ## curvature along the x-axis of the patch and the second along y-axis. |
38 ## | |
39 ## If @var{curv} is a scalar, it represents the curvature of the shorter of the | |
40 ## two sides of the rectangle and the curvature of the other side is defined | |
41 ## by | |
42 ## | |
43 ## @example | |
12763
f511bfe00d14
maint: Periodic merge of stable to default
Rik <octave@nomad.inbox5.com>
parents:
12761
diff
changeset
|
44 ## min (pos (1:2)) / max (pos (1:2)) * curv |
12697 | 45 ## @end example |
46 ## | |
14001
5f0bb45e615c
doc: Update documentation for functions returning a graphics handle h (Bug #34761)
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
47 ## Other properties are passed to the underlying patch command. |
5f0bb45e615c
doc: Update documentation for functions returning a graphics handle h (Bug #34761)
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
48 ## |
5f0bb45e615c
doc: Update documentation for functions returning a graphics handle h (Bug #34761)
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
49 ## 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
|
50 ## rectangle object. |
12697 | 51 ## @end deftypefn |
52 ## @seealso{patch} | |
53 | |
54 function h = rectangle (varargin) | |
55 | |
56 [hax, varargin] = __plt_get_axis_arg__ ("rectangle", varargin{:}); | |
57 | |
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
|
58 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
|
59 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
|
60 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
|
61 |
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
|
62 htmp = __rectangle__ (hax, varargin{:}); |
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 |
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_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
|
65 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
|
66 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
|
67 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
|
68 end_unwind_protect |
12697 | 69 |
70 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
|
71 h = htmp; |
12697 | 72 endif |
73 endfunction | |
74 | |
75 function hg = __rectangle__ (hax, varargin) | |
76 | |
77 iarg = 1; | |
78 pos = [0, 0, 1, 1]; | |
79 curv2 = [0, 0]; | |
80 ec = [0, 0, 0]; | |
81 fc = "none"; | |
82 | |
83 while (iarg < length (varargin)) | |
84 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
|
85 if (ischar (arg)) |
12697 | 86 if (strcmpi (arg, "position")) |
87 pos = varargin{iarg+1}; | |
88 varargin(iarg:iarg+1) = []; | |
89 if (!isvector (pos) || numel (pos) != 4) | |
90 error ("rectangle: position must be a 4 element vector"); | |
91 endif | |
92 elseif (strcmpi (arg, "curvature")) | |
93 curv2 = varargin{iarg+1}; | |
94 varargin(iarg:iarg+1) = []; | |
95 if (!isnumeric (curv2) || (numel (curv2) != 1 && numel (curv2) != 2)) | |
96 error ("rectangle: curvature must be a 2 element vector or a scalar"); | |
97 endif | |
98 if (any (curv2 < 0) || any (curv2 > 1)) | |
99 error ("rectangle: curvature values must be between 0 and 1"); | |
100 endif | |
101 elseif (strcmpi (arg, "edgecolor")) | |
102 ec = varargin{iarg+1}; | |
103 varargin(iarg:iarg+1) = []; | |
104 elseif (strcmpi (arg, "facecolor")) | |
105 fc = varargin{iarg+1}; | |
106 varargin(iarg:iarg+1) = []; | |
107 else | |
108 iarg ++; | |
109 endif | |
110 else | |
111 iarg ++; | |
112 endif | |
113 endwhile | |
114 | |
115 if (numel (curv2) == 1) | |
116 [a, ai] = min (pos (3 : 4)); | |
117 [b, bi] = max (pos (3 : 4)); | |
118 if (ai < bi) | |
119 curv = [curv2, curv2 .* a ./ b]; | |
120 else | |
121 curv = [curv2 .* a ./ b, curv2]; | |
122 endif | |
123 else | |
124 curv = curv2; | |
125 endif | |
126 | |
127 if (all (curv) < 0.01) | |
128 ## Special case : no curvature | |
129 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; | |
130 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; | |
131 else | |
132 p = pi / 2 * [0 : 15] / 15; | |
133 c = curv .* pos(3 : 4) / 2; | |
134 cx = c(1) * sin (p) - c(1); | |
135 cy = c(2) * cos (p) - c(2); | |
136 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... | |
137 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; | |
138 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... | |
139 pos(2) + pos(4) + cy, pos(2) + c(2)]; | |
140 endif | |
141 | |
142 hg = hggroup (); | |
143 | |
144 h = patch ("xdata", x(:), "ydata", y(:), "facecolor", fc, "edgecolor", ec, ... | |
145 "parent", hg, varargin{:}); | |
146 | |
147 addproperty ("curvature", hg, "data", curv2); | |
148 addproperty ("position", hg, "data", pos); | |
149 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor")); | |
150 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth")); | |
151 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle")); | |
152 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor")); | |
153 | |
154 addlistener (hg, "curvature", @update_data); | |
155 addlistener (hg, "position", @update_data); | |
156 addlistener (hg, "edgecolor", @update_props); | |
157 addlistener (hg, "linewidth", @update_props); | |
158 addlistener (hg, "linestyle", @update_props); | |
159 addlistener (hg, "facecolor", @update_props); | |
160 endfunction | |
161 | |
162 function update_data (h, d) | |
163 persistent recursion = false; | |
164 | |
165 ## Don't allow recursion | |
166 if (!recursion) | |
167 unwind_protect | |
168 recursion = true; | |
169 | |
170 kids = get (h, "children"); | |
171 pos = get (h, "position"); | |
172 curv2 = get (h, "curvature"); | |
173 | |
174 if (numel (curv2) == 1) | |
175 [a, ai] = min (pos (3 : 4)); | |
176 [b, bi] = max (pos (3 : 4)); | |
177 if (ai < bi) | |
178 curv = [curv2, curv2 .* a ./ b]; | |
179 else | |
180 curv = [curv2 .* a ./ b, curv2]; | |
181 endif | |
182 else | |
183 curv = curv2; | |
184 endif | |
185 | |
186 if (all (curv) < 0.01) | |
187 ## Special case : no curvature | |
188 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; | |
189 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; | |
190 else | |
191 p = pi / 2 * [0 : 15] / 15; | |
192 c = curv .* pos(3 : 4) / 2; | |
193 cx = c(1) * sin (p) - c(1); | |
194 cy = c(2) * cos (p) - c(2); | |
195 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... | |
196 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; | |
197 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... | |
198 pos(2) + pos(4) + cy, pos(2) + c(2)]; | |
199 endif | |
200 | |
201 set (kids, "xdata", x, "ydata", y); | |
202 unwind_protect_cleanup | |
203 recursion = false; | |
204 end_unwind_protect | |
205 endif | |
206 endfunction | |
207 | |
208 function update_props (h, d) | |
209 kids = get (h, "children"); | |
210 set (kids, "edgecolor", get (h, "edgecolor"), | |
211 "linewidth", get (h, "linewidth"), | |
212 "linestyle", get (h, "linestyle"), | |
213 "facecolor", get (h, "facecolor")); | |
214 endfunction | |
215 | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
216 |
12697 | 217 %!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
|
218 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
219 %! axis equal; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
220 %! rectangle ('Position', [0.05, 0.05, 0.9, 0.9], 'Curvature', [0.5, 0.5]); |
12697 | 221 |
222 %!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
|
223 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
224 %! axis equal; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
225 %! rectangle ('Position', [0.05, 0.05, 0.9, 0.4], 'Curvature', 1.0); |
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 %! 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
|
231 %! set (h, 'FaceColor', [0, 1, 0]); |
12697 | 232 |