1 ## Copyright (C) 2012 David Bateman
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {} rectangle ()
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{})
27 ## Draw rectangular patch defined by @var{pos} and @var{curv}. The variable
28 ## @code{@var{pos}(1:2)} defines the lower left-hand corner of the patch
29 ## and @code{@var{pos}(3:4)} defines its width and height. By default, the
30 ## value of @var{pos} is @code{[0, 0, 1, 1]}.
32 ## The variable @var{curv} defines the curvature of the sides of the rectangle
33 ## and may be a scalar or two-element vector with values between 0 and 1.
34 ## A value of 0 represents no curvature of the side, whereas a value of 1
35 ## means that the side is entirely curved into the arc of a circle.
36 ## If @var{curv} is a two-element vector, then the first element is the
37 ## curvature along the x-axis of the patch and the second along y-axis.
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
44 ## min (pos (1:2)) / max (pos (1:2)) * curv
47 ## Other properties are passed to the underlying patch command.
49 ## The optional return value @var{h} is a graphics handle to the created
54 function h = rectangle (varargin)
56 [hax, varargin] = __plt_get_axis_arg__ ("rectangle", varargin{:});
58 tmp = __rectangle__ (hax, varargin{:});
65 function hg = __rectangle__ (hax, varargin)
73 while (iarg < length (varargin))
76 if (strcmpi (arg, "position"))
77 pos = varargin{iarg+1};
78 varargin(iarg:iarg+1) = [];
79 if (!isvector (pos) || numel (pos) != 4)
80 error ("rectangle: position must be a 4 element vector");
82 elseif (strcmpi (arg, "curvature"))
83 curv2 = varargin{iarg+1};
84 varargin(iarg:iarg+1) = [];
85 if (!isnumeric (curv2) || (numel (curv2) != 1 && numel (curv2) != 2))
86 error ("rectangle: curvature must be a 2 element vector or a scalar");
88 if (any (curv2 < 0) || any (curv2 > 1))
89 error ("rectangle: curvature values must be between 0 and 1");
91 elseif (strcmpi (arg, "edgecolor"))
92 ec = varargin{iarg+1};
93 varargin(iarg:iarg+1) = [];
94 elseif (strcmpi (arg, "facecolor"))
95 fc = varargin{iarg+1};
96 varargin(iarg:iarg+1) = [];
105 if (numel (curv2) == 1)
106 [a, ai] = min (pos (3 : 4));
107 [b, bi] = max (pos (3 : 4));
109 curv = [curv2, curv2 .* a ./ b];
111 curv = [curv2 .* a ./ b, curv2];
117 if (all (curv) < 0.01)
118 ## Special case : no curvature
119 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)];
120 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)];
122 p = pi / 2 * [0 : 15] / 15;
123 c = curv .* pos(3 : 4) / 2;
124 cx = c(1) * sin (p) - c(1);
125 cy = c(2) * cos (p) - c(2);
126 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ...
127 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)];
128 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ...
129 pos(2) + pos(4) + cy, pos(2) + c(2)];
134 h = patch ("xdata", x(:), "ydata", y(:), "facecolor", fc, "edgecolor", ec, ...
135 "parent", hg, varargin{:});
137 addproperty ("curvature", hg, "data", curv2);
138 addproperty ("position", hg, "data", pos);
139 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor"));
140 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth"));
141 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle"));
142 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor"));
144 addlistener (hg, "curvature", @update_data);
145 addlistener (hg, "position", @update_data);
146 addlistener (hg, "edgecolor", @update_props);
147 addlistener (hg, "linewidth", @update_props);
148 addlistener (hg, "linestyle", @update_props);
149 addlistener (hg, "facecolor", @update_props);
152 function update_data (h, d)
153 persistent recursion = false;
155 ## Don't allow recursion
160 kids = get (h, "children");
161 pos = get (h, "position");
162 curv2 = get (h, "curvature");
164 if (numel (curv2) == 1)
165 [a, ai] = min (pos (3 : 4));
166 [b, bi] = max (pos (3 : 4));
168 curv = [curv2, curv2 .* a ./ b];
170 curv = [curv2 .* a ./ b, curv2];
176 if (all (curv) < 0.01)
177 ## Special case : no curvature
178 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)];
179 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)];
181 p = pi / 2 * [0 : 15] / 15;
182 c = curv .* pos(3 : 4) / 2;
183 cx = c(1) * sin (p) - c(1);
184 cy = c(2) * cos (p) - c(2);
185 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ...
186 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)];
187 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ...
188 pos(2) + pos(4) + cy, pos(2) + c(2)];
191 set (kids, "xdata", x, "ydata", y);
192 unwind_protect_cleanup
198 function update_props (h, d)
199 kids = get (h, "children");
200 set (kids, "edgecolor", get (h, "edgecolor"),
201 "linewidth", get (h, "linewidth"),
202 "linestyle", get (h, "linestyle"),
203 "facecolor", get (h, "facecolor"));
210 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.9], "Curvature", [0.5, 0.5]);
215 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0);
220 %! h = rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0);
221 %! set (h, "FaceColor", [0, 1, 0]);