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