]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/rectangle.m
update packages
[CreaPhase.git] / octave_packages / m / plot / rectangle.m
1 ## Copyright (C) 2012 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)} defines its width and height.  By default, the
30 ## value of @var{pos} is @code{[0, 0, 1, 1]}.
31 ##
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.
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. 
48 ## 
49 ## The optional return value @var{h} is a graphics handle to the created
50 ## rectangle object.
51 ## @end deftypefn
52 ## @seealso{patch}
53
54 function h = rectangle (varargin)
55
56   [hax, varargin] = __plt_get_axis_arg__ ("rectangle", varargin{:});
57
58   tmp =  __rectangle__ (hax, varargin{:});
59
60   if (nargout > 0)
61     h = tmp;
62   endif
63 endfunction
64
65 function hg = __rectangle__ (hax, varargin)
66
67   iarg = 1;
68   pos = [0, 0, 1, 1];
69   curv2 = [0, 0];
70   ec = [0, 0, 0];
71   fc = "none";
72
73   while (iarg < length (varargin))
74     arg = varargin{iarg};
75     if (ischar(arg))
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");
81         endif
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");
87         endif
88         if (any (curv2 < 0) || any (curv2 > 1))
89           error ("rectangle: curvature values must be between 0 and 1");
90         endif
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) = [];
97       else
98         iarg ++;
99       endif
100     else
101       iarg ++;
102     endif
103   endwhile
104
105   if (numel (curv2) == 1)
106     [a, ai] = min (pos (3 : 4));
107     [b, bi] = max (pos (3 : 4));
108     if (ai < bi)
109       curv = [curv2, curv2 .* a ./ b];
110     else
111       curv = [curv2 .* a ./ b, curv2];
112     endif
113   else
114     curv = curv2;
115   endif
116
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)];
121   else
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)];
130   endif
131
132   hg = hggroup ();
133
134   h = patch ("xdata", x(:), "ydata", y(:), "facecolor", fc, "edgecolor", ec, ...
135              "parent", hg, varargin{:});
136
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"));
143
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);
150 endfunction
151
152 function update_data (h, d)
153   persistent recursion = false;
154
155   ## Don't allow recursion
156   if (!recursion)
157     unwind_protect
158       recursion = true;
159
160       kids = get (h, "children");
161       pos = get (h, "position");
162       curv2 = get (h, "curvature");
163
164       if (numel (curv2) == 1)
165         [a, ai] = min (pos (3 : 4));
166         [b, bi] = max (pos (3 : 4));
167         if (ai < bi)
168           curv = [curv2, curv2 .* a ./ b];
169         else
170           curv = [curv2 .* a ./ b, curv2];
171         endif
172       else
173         curv = curv2;
174       endif
175
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)];
180       else
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)];
189       endif
190
191       set (kids, "xdata", x, "ydata", y);
192     unwind_protect_cleanup
193       recursion = false;
194     end_unwind_protect
195   endif
196 endfunction
197
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"));
204 endfunction
205
206
207 %!demo
208 %! clf
209 %! axis equal
210 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.9], "Curvature", [0.5, 0.5]);
211
212 %!demo
213 %! clf
214 %! axis equal
215 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0);
216
217 %!demo
218 %! clf
219 %! axis equal
220 %! h = rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature",  1.0);
221 %! set (h, "FaceColor", [0, 1, 0]);
222