Mercurial > octave
annotate scripts/plot/draw/rectangle.m @ 23219:3ac9f9ecfae5 stable
maint: Update copyright dates.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 22 Feb 2017 12:39:29 -0500 |
parents | e9a0469dedd9 |
children | 092078913d54 0c6cedafc71e |
rev | line source |
---|---|
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
1 ## Copyright (C) 2012-2017 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 -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
20 ## @deftypefn {} {} rectangle () |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
21 ## @deftypefnx {} {} rectangle (@dots{}, "Position", @var{pos}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
22 ## @deftypefnx {} {} rectangle (@dots{}, "Curvature", @var{curv}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
23 ## @deftypefnx {} {} rectangle (@dots{}, "EdgeColor", @var{ec}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
24 ## @deftypefnx {} {} rectangle (@dots{}, "FaceColor", @var{fc}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
25 ## @deftypefnx {} {} rectangle (@var{hax}, @dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
26 ## @deftypefnx {} {@var{h} =} rectangle (@dots{}) |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
27 ## Draw a rectangular patch defined by @var{pos} and @var{curv}. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
28 ## |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
29 ## 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
|
30 ## 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
|
31 ## default, the value of @var{pos} is @code{[0, 0, 1, 1]}. |
12697 | 32 ## |
33 ## 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
|
34 ## 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
|
35 ## 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
|
36 ## 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
|
37 ## If @var{curv} is a two-element vector, then the first element is the |
12697 | 38 ## curvature along the x-axis of the patch and the second along y-axis. |
39 ## | |
40 ## If @var{curv} is a scalar, it represents the curvature of the shorter of the | |
41 ## two sides of the rectangle and the curvature of the other side is defined | |
42 ## by | |
43 ## | |
44 ## @example | |
17606 | 45 ## min (pos(1:2)) / max (pos(1:2)) * curv |
12697 | 46 ## @end example |
47 ## | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
48 ## Additional property/value pairs are passed to the underlying patch command. |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
49 ## |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
50 ## 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
|
51 ## 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
|
52 ## |
14001
5f0bb45e615c
doc: Update documentation for functions returning a graphics handle h (Bug #34761)
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
53 ## 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
|
54 ## rectangle object. |
12697 | 55 ## @end deftypefn |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17051
diff
changeset
|
56 ## @seealso{patch, line, cylinder, ellipsoid, sphere} |
12697 | 57 |
58 function h = rectangle (varargin) | |
59 | |
60 [hax, varargin] = __plt_get_axis_arg__ ("rectangle", varargin{:}); | |
61 | |
17724
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
62 if (isempty (hax)) |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
63 hax = gca (); |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
64 else |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
65 hax = hax(1); |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
66 endif |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
67 |
17211
87ba70043bfc
Don't use ifelse in plot fcns to avoid unnecessary fcn evaluations.
Rik <rik@octave.org>
parents:
17190
diff
changeset
|
68 oldfig = []; |
17301
68bcac3c043a
Correct inversion accidentally introduced in cset 87ba70043bfc.
Rik <rik@octave.org>
parents:
17211
diff
changeset
|
69 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
|
70 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
|
71 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
|
72 unwind_protect |
17126
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 end_unwind_protect |
12697 | 79 |
80 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
|
81 h = htmp; |
12697 | 82 endif |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
83 |
12697 | 84 endfunction |
85 | |
86 function hg = __rectangle__ (hax, varargin) | |
87 | |
88 iarg = 1; | |
89 pos = [0, 0, 1, 1]; | |
90 curv2 = [0, 0]; | |
91 ec = [0, 0, 0]; | |
92 fc = "none"; | |
17724
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
93 parent = []; |
12697 | 94 |
95 while (iarg < length (varargin)) | |
96 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
|
97 if (ischar (arg)) |
12697 | 98 if (strcmpi (arg, "position")) |
99 pos = varargin{iarg+1}; | |
100 varargin(iarg:iarg+1) = []; | |
17606 | 101 if (! isvector (pos) || numel (pos) != 4) |
12697 | 102 error ("rectangle: position must be a 4 element vector"); |
103 endif | |
104 elseif (strcmpi (arg, "curvature")) | |
105 curv2 = varargin{iarg+1}; | |
106 varargin(iarg:iarg+1) = []; | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
107 if (! isnumeric (curv2) || (numel (curv2) != 1 && numel (curv2) != 2)) |
17606 | 108 error ("rectangle: curvature must be a 2-element vector or a scalar"); |
12697 | 109 endif |
110 if (any (curv2 < 0) || any (curv2 > 1)) | |
111 error ("rectangle: curvature values must be between 0 and 1"); | |
112 endif | |
113 elseif (strcmpi (arg, "edgecolor")) | |
114 ec = varargin{iarg+1}; | |
115 varargin(iarg:iarg+1) = []; | |
116 elseif (strcmpi (arg, "facecolor")) | |
117 fc = varargin{iarg+1}; | |
118 varargin(iarg:iarg+1) = []; | |
17724
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
119 elseif (strcmpi (arg, "parent")) |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
120 parent = varargin{iarg+1}; |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
121 varargin(iarg:iarg+1) = []; |
12697 | 122 else |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20714
diff
changeset
|
123 iarg += 1; |
12697 | 124 endif |
125 else | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20714
diff
changeset
|
126 iarg += 1; |
12697 | 127 endif |
128 endwhile | |
129 | |
130 if (numel (curv2) == 1) | |
17606 | 131 [a, ai] = min (pos(3:4)); |
132 [b, bi] = max (pos(3:4)); | |
12697 | 133 if (ai < bi) |
134 curv = [curv2, curv2 .* a ./ b]; | |
135 else | |
136 curv = [curv2 .* a ./ b, curv2]; | |
137 endif | |
138 else | |
139 curv = curv2; | |
140 endif | |
141 | |
17606 | 142 if (all (curv < 0.01)) |
12697 | 143 ## Special case : no curvature |
144 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; | |
145 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; | |
146 else | |
147 p = pi / 2 * [0 : 15] / 15; | |
17606 | 148 c = curv .* pos(3:4) / 2; |
12697 | 149 cx = c(1) * sin (p) - c(1); |
150 cy = c(2) * cos (p) - c(2); | |
151 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... | |
152 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; | |
153 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... | |
154 pos(2) + pos(4) + cy, pos(2) + c(2)]; | |
155 endif | |
156 | |
17724
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
157 if (! isempty (parent)) |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
158 hg = hggroup ("parent", parent); |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
159 else |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
160 hg = hggroup ("parent", hax); |
f0bc865db55f
rectangle.m: Fix multiple parenting issues (bug #40214, bug #39813).
Rik <rik@octave.org>
parents:
17606
diff
changeset
|
161 endif |
12697 | 162 |
17606 | 163 h = patch ("xdata", x(:), "ydata", y(:), "facecolor", fc, "edgecolor", ec, |
12697 | 164 "parent", hg, varargin{:}); |
165 | |
166 addproperty ("curvature", hg, "data", curv2); | |
167 addproperty ("position", hg, "data", pos); | |
168 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor")); | |
169 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth")); | |
170 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle")); | |
171 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor")); | |
172 | |
173 addlistener (hg, "curvature", @update_data); | |
174 addlistener (hg, "position", @update_data); | |
175 addlistener (hg, "edgecolor", @update_props); | |
176 addlistener (hg, "linewidth", @update_props); | |
177 addlistener (hg, "linestyle", @update_props); | |
178 addlistener (hg, "facecolor", @update_props); | |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
179 |
12697 | 180 endfunction |
181 | |
17606 | 182 function update_data (h, ~) |
12697 | 183 persistent recursion = false; |
184 | |
185 ## Don't allow recursion | |
17606 | 186 if (! recursion) |
12697 | 187 unwind_protect |
188 recursion = true; | |
189 | |
190 kids = get (h, "children"); | |
191 pos = get (h, "position"); | |
192 curv2 = get (h, "curvature"); | |
193 | |
194 if (numel (curv2) == 1) | |
17606 | 195 [a, ai] = min (pos(3:4)); |
196 [b, bi] = max (pos(3:4)); | |
12697 | 197 if (ai < bi) |
198 curv = [curv2, curv2 .* a ./ b]; | |
199 else | |
200 curv = [curv2 .* a ./ b, curv2]; | |
201 endif | |
202 else | |
203 curv = curv2; | |
204 endif | |
205 | |
17606 | 206 if (all (curv < 0.01)) |
12697 | 207 ## Special case : no curvature |
208 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; | |
209 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; | |
210 else | |
211 p = pi / 2 * [0 : 15] / 15; | |
17606 | 212 c = curv .* pos(3:4) / 2; |
12697 | 213 cx = c(1) * sin (p) - c(1); |
214 cy = c(2) * cos (p) - c(2); | |
215 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... | |
216 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; | |
217 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... | |
218 pos(2) + pos(4) + cy, pos(2) + c(2)]; | |
219 endif | |
220 | |
221 set (kids, "xdata", x, "ydata", y); | |
222 unwind_protect_cleanup | |
223 recursion = false; | |
224 end_unwind_protect | |
225 endif | |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
226 |
12697 | 227 endfunction |
228 | |
17606 | 229 function update_props (h, ~) |
12697 | 230 kids = get (h, "children"); |
17606 | 231 set (kids, {"edgecolor", "linewidth", "linestyle", "facecolor"}, |
232 get (h, {"edgecolor", "linewidth", "linestyle", "facecolor"})); | |
12697 | 233 endfunction |
234 | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
235 |
12697 | 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; |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
239 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.9], "Curvature", [0.5, 0.5]); |
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
240 %! title ("rectangle() with corners curved"); |
12697 | 241 |
242 %!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
|
243 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
244 %! axis equal; |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
245 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0); |
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
246 %! title ("rectangle() with sides as complete arcs"); |
12697 | 247 |
248 %!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
|
249 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
250 %! axis equal; |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
251 %! h = rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0); |
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
252 %! set (h, "FaceColor", [0, 1, 0]); |
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
253 %! title ("rectangle() with FaceColor = green"); |