Mercurial > hg > octave-lyh
annotate scripts/plot/rectangle.m @ 12761:13bcd62824a7 stable
doc: Add documentation for gmres, rectangle to manual
linear-algebra/module.mk: Add gmres.m to list of functions.
gmres.m: Correct spelling in @seealso reference.
octave.texi: Add reference to new menu for 2D Geometric Shapes.
plot.txi: Add new menu for 2D Geometric Shapes. Add rectangle function.
rectangle.m: Improve DOCSTRING
aspell-octave.en.pws: Add spelling exception for PGMRES
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 20 Jun 2011 10:17:20 -0700 |
parents | fb93b94dfc82 |
children | f511bfe00d14 |
rev | line source |
---|---|
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 -*- | |
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}) | |
25 ## @deftypefnx {Function File} {@var{h} =} rectangle (@dots{}) | |
26 ## | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
27 ## Draw rectangular patch defined by @var{pos} and @var{curv}. The variable |
12697 | 28 ## @code{@var{pos}(1 : 2)} defines the lower left-hand corner of the patch |
12761
13bcd62824a7
doc: Add documentation for gmres, rectangle to manual
Rik <octave@nomad.inbox5.com>
parents:
12698
diff
changeset
|
29 ## and @code{@var{pos}(3 : 4)} defines its width and height. By default, the |
13bcd62824a7
doc: Add documentation for gmres, rectangle to manual
Rik <octave@nomad.inbox5.com>
parents:
12698
diff
changeset
|
30 ## value of @var{pos} is @code{[0, 0, 1, 1]}. |
12697 | 31 ## |
32 ## 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
|
33 ## 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
|
34 ## 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
|
35 ## means that the side is entirely curved into the arc of a circle. |
13bcd62824a7
doc: Add documentation for gmres, rectangle to manual
Rik <octave@nomad.inbox5.com>
parents:
12698
diff
changeset
|
36 ## If @var{curv} is a two-element vector, then the first element is the |
12697 | 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 ## | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
47 ## Other properties are passed to the underlying patch command. If called |
12697 | 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 | |
12698
fb93b94dfc82
Add rectangle to new functions list in NEWS.
Rik <octave@nomad.inbox5.com>
parents:
12697
diff
changeset
|
205 |
12697 | 206 %!demo |
207 %! close all | |
208 %! axis equal | |
209 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.9], "Curvature", [0.5, 0.5]); | |
210 | |
211 %!demo | |
212 %! close all | |
213 %! axis equal | |
214 %! rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0); | |
215 | |
216 %!demo | |
217 %! close all | |
218 %! axis equal | |
219 %! h = rectangle ("Position", [0.05, 0.05, 0.9, 0.4], "Curvature", 1.0); | |
220 %! set (h, "FaceColor", [0, 1, 0]); | |
221 |