12697
|
1 ## Copyright (C) 2011 David Bateman |
|
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 -*- |
|
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{}) |
|
26 ## |
|
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)} its width and height. By default the value of |
|
30 ## @var{pos} is @var{[0, 0, 1, 1]}. |
|
31 ## |
|
32 ## The variable @var{curv} defines the curvature of the sides of the rectangle |
|
33 ## and it can be a scalar or two-element vector with values between 0 and 1. |
|
34 ## A value of 0 represents no curvature of the side, where as a value of 1 |
|
35 ## means that the rectangular side is entirely curved into an arc of a circle. |
|
36 ## If curvature 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. |
|
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 |
|
44 ## min (pos (1: 2)) / max (pos (1:2)) * curv |
|
45 ## @end example |
|
46 ## |
|
47 ## Other properties are passed to the underlying patch command. If called |
|
48 ## with an output argument, @code{rectangle} returns the handle to the |
|
49 ## rectangle. |
|
50 ## @end deftypefn |
|
51 ## @seealso{patch} |
|
52 |
|
53 function h = rectangle (varargin) |
|
54 |
|
55 [hax, varargin] = __plt_get_axis_arg__ ("rectangle", varargin{:}); |
|
56 |
|
57 tmp = __rectangle__ (hax, varargin{:}); |
|
58 |
|
59 if (nargout > 0) |
|
60 h = tmp; |
|
61 endif |
|
62 endfunction |
|
63 |
|
64 function hg = __rectangle__ (hax, varargin) |
|
65 |
|
66 iarg = 1; |
|
67 pos = [0, 0, 1, 1]; |
|
68 curv2 = [0, 0]; |
|
69 ec = [0, 0, 0]; |
|
70 fc = "none"; |
|
71 |
|
72 while (iarg < length (varargin)) |
|
73 arg = varargin{iarg}; |
|
74 if (ischar(arg)) |
|
75 if (strcmpi (arg, "position")) |
|
76 pos = varargin{iarg+1}; |
|
77 varargin(iarg:iarg+1) = []; |
|
78 if (!isvector (pos) || numel (pos) != 4) |
|
79 error ("rectangle: position must be a 4 element vector"); |
|
80 endif |
|
81 elseif (strcmpi (arg, "curvature")) |
|
82 curv2 = varargin{iarg+1}; |
|
83 varargin(iarg:iarg+1) = []; |
|
84 if (!isnumeric (curv2) || (numel (curv2) != 1 && numel (curv2) != 2)) |
|
85 error ("rectangle: curvature must be a 2 element vector or a scalar"); |
|
86 endif |
|
87 if (any (curv2 < 0) || any (curv2 > 1)) |
|
88 error ("rectangle: curvature values must be between 0 and 1"); |
|
89 endif |
|
90 elseif (strcmpi (arg, "edgecolor")) |
|
91 ec = varargin{iarg+1}; |
|
92 varargin(iarg:iarg+1) = []; |
|
93 elseif (strcmpi (arg, "facecolor")) |
|
94 fc = varargin{iarg+1}; |
|
95 varargin(iarg:iarg+1) = []; |
|
96 else |
|
97 iarg ++; |
|
98 endif |
|
99 else |
|
100 iarg ++; |
|
101 endif |
|
102 endwhile |
|
103 |
|
104 if (numel (curv2) == 1) |
|
105 [a, ai] = min (pos (3 : 4)); |
|
106 [b, bi] = max (pos (3 : 4)); |
|
107 if (ai < bi) |
|
108 curv = [curv2, curv2 .* a ./ b]; |
|
109 else |
|
110 curv = [curv2 .* a ./ b, curv2]; |
|
111 endif |
|
112 else |
|
113 curv = curv2; |
|
114 endif |
|
115 |
|
116 if (all (curv) < 0.01) |
|
117 ## Special case : no curvature |
|
118 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; |
|
119 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; |
|
120 else |
|
121 p = pi / 2 * [0 : 15] / 15; |
|
122 c = curv .* pos(3 : 4) / 2; |
|
123 cx = c(1) * sin (p) - c(1); |
|
124 cy = c(2) * cos (p) - c(2); |
|
125 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... |
|
126 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; |
|
127 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... |
|
128 pos(2) + pos(4) + cy, pos(2) + c(2)]; |
|
129 endif |
|
130 |
|
131 hg = hggroup (); |
|
132 |
|
133 h = patch ("xdata", x(:), "ydata", y(:), "facecolor", fc, "edgecolor", ec, ... |
|
134 "parent", hg, varargin{:}); |
|
135 |
|
136 addproperty ("curvature", hg, "data", curv2); |
|
137 addproperty ("position", hg, "data", pos); |
|
138 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor")); |
|
139 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth")); |
|
140 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle")); |
|
141 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor")); |
|
142 |
|
143 addlistener (hg, "curvature", @update_data); |
|
144 addlistener (hg, "position", @update_data); |
|
145 addlistener (hg, "edgecolor", @update_props); |
|
146 addlistener (hg, "linewidth", @update_props); |
|
147 addlistener (hg, "linestyle", @update_props); |
|
148 addlistener (hg, "facecolor", @update_props); |
|
149 endfunction |
|
150 |
|
151 function update_data (h, d) |
|
152 persistent recursion = false; |
|
153 |
|
154 ## Don't allow recursion |
|
155 if (!recursion) |
|
156 unwind_protect |
|
157 recursion = true; |
|
158 |
|
159 kids = get (h, "children"); |
|
160 pos = get (h, "position"); |
|
161 curv2 = get (h, "curvature"); |
|
162 |
|
163 if (numel (curv2) == 1) |
|
164 [a, ai] = min (pos (3 : 4)); |
|
165 [b, bi] = max (pos (3 : 4)); |
|
166 if (ai < bi) |
|
167 curv = [curv2, curv2 .* a ./ b]; |
|
168 else |
|
169 curv = [curv2 .* a ./ b, curv2]; |
|
170 endif |
|
171 else |
|
172 curv = curv2; |
|
173 endif |
|
174 |
|
175 if (all (curv) < 0.01) |
|
176 ## Special case : no curvature |
|
177 x = [pos(1), pos(1) + pos(3), pos(1) + pos(3), pos(1), pos(1)]; |
|
178 y = [pos(2), pos(2), pos(2) + pos(4), pos(2) + pos(4), pos(2)]; |
|
179 else |
|
180 p = pi / 2 * [0 : 15] / 15; |
|
181 c = curv .* pos(3 : 4) / 2; |
|
182 cx = c(1) * sin (p) - c(1); |
|
183 cy = c(2) * cos (p) - c(2); |
|
184 x = [pos(1) - fliplr(cx), pos(1) + pos(3) + cx, ... |
|
185 pos(1) + pos(3) + fliplr(cx), pos(1) - cx, pos(1)]; |
|
186 y = [pos(2) - fliplr(cy), pos(2) - cy, pos(2) + pos(4) + fliplr(cy), ... |
|
187 pos(2) + pos(4) + cy, pos(2) + c(2)]; |
|
188 endif |
|
189 |
|
190 set (kids, "xdata", x, "ydata", y); |
|
191 unwind_protect_cleanup |
|
192 recursion = false; |
|
193 end_unwind_protect |
|
194 endif |
|
195 endfunction |
|
196 |
|
197 function update_props (h, d) |
|
198 kids = get (h, "children"); |
|
199 set (kids, "edgecolor", get (h, "edgecolor"), |
|
200 "linewidth", get (h, "linewidth"), |
|
201 "linestyle", get (h, "linestyle"), |
|
202 "facecolor", get (h, "facecolor")); |
|
203 endfunction |
|
204 |
|
205 %!demo |
|
206 %! close all |
|
207 %! axis equal |
|
208 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.9], "Curvature", [0.5, 0.5]); |
|
209 |
|
210 %!demo |
|
211 %! close all |
|
212 %! axis equal |
|
213 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0); |
|
214 |
|
215 %!demo |
|
216 %! close all |
|
217 %! axis equal |
|
218 %! h = rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0); |
|
219 %! set (h, "FaceColor", [0, 1, 0]); |
|
220 |