]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/patch.m
update packages
[CreaPhase.git] / octave_packages / m / plot / patch.m
1 ## Copyright (C) 2005-2012 John W. Eaton
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} {} patch ()
21 ## @deftypefnx {Function File} {} patch (@var{x}, @var{y}, @var{c})
22 ## @deftypefnx {Function File} {} patch (@var{x}, @var{y}, @var{z}, @var{c})
23 ## @deftypefnx {Function File} {} patch (@var{fv})
24 ## @deftypefnx {Function File} {} patch ('Faces', @var{f}, 'Vertices', @var{v}, @dots{})
25 ## @deftypefnx {Function File} {} patch (@dots{}, @var{prop}, @var{val})
26 ## @deftypefnx {Function File} {} patch (@var{h}, @dots{})
27 ## @deftypefnx {Function File} {@var{h} =} patch (@dots{})
28 ## Create patch object from @var{x} and @var{y} with color @var{c} and
29 ## insert in the current axes object.  Return handle to patch object.
30 ##
31 ## For a uniform colored patch, @var{c} can be given as an RGB vector,
32 ## scalar value referring to the current colormap, or string value (for
33 ## example, "r" or "red").
34 ##
35 ## If passed a structure @var{fv} contain the fields "vertices", "faces"
36 ## and optionally "facevertexcdata", create the patch based on these
37 ## properties.
38 ##
39 ## The optional return value @var{h} is a graphics handle to the created patch
40 ## object.
41 ## @seealso{fill}
42 ## @end deftypefn
43
44 ## Author: jwe
45
46 function retval = patch (varargin)
47
48   [h, varargin] = __plt_get_axis_arg__ ("patch", varargin{:});
49
50   [tmp, failed] = __patch__ (h, varargin{:});
51
52   if (failed)
53     print_usage ();
54   endif
55
56   if (nargout > 0)
57     retval = tmp;
58   endif
59
60 endfunction
61
62 %!demo
63 %! ## Patches with same number of vertices
64 %! clf
65 %! t1 = (1/16:1/8:1)'*2*pi;
66 %! t2 = ((1/16:1/8:1)' + 1/32)*2*pi;
67 %! x1 = sin (t1) - 0.8;
68 %! y1 = cos (t1);
69 %! x2 = sin (t2) + 0.8;
70 %! y2 = cos (t2);
71 %! patch([x1,x2],[y1,y2],'r');
72
73 %!demo
74 %! ## Unclosed patch
75 %! clf
76 %! t1 = (1/16:1/8:1)'*2*pi;
77 %! t2 = ((1/16:1/16:1)' + 1/32)*2*pi;
78 %! x1 = sin (t1) - 0.8;
79 %! y1 = cos (t1);
80 %! x2 = sin (t2) + 0.8;
81 %! y2 = cos (t2);
82 %! patch([[x1;NaN(8,1)],x2],[[y1;NaN(8,1)],y2],'r');
83
84 %!demo
85 %! ## Specify vertices and faces separately
86 %! clf
87 %! t1 = (1/16:1/8:1)'*2*pi;
88 %! t2 = ((1/16:1/16:1)' + 1/32)*2*pi;
89 %! x1 = sin (t1) - 0.8;
90 %! y1 = cos (t1);
91 %! x2 = sin (t2) + 0.8;
92 %! y2 = cos (t2);
93 %! vert = [x1, y1; x2, y2];
94 %! fac = [1:8,NaN(1,8);9:24];
95 %! patch('Faces',fac,'Vertices',vert,'FaceColor','r');
96
97 %!demo
98 %! ## Specify vertices and faces separately
99 %! clf
100 %! t1 = (1/16:1/8:1)'*2*pi;
101 %! t2 = ((1/16:1/16:1)' + 1/32)*2*pi;
102 %! x1 = sin (t1) - 0.8;
103 %! y1 = cos (t1);
104 %! x2 = sin (t2) + 0.8;
105 %! y2 = cos (t2);
106 %! vert = [x1, y1; x2, y2];
107 %! fac = [1:8,NaN(1,8);9:24];
108 %! patch('Faces',fac,'Vertices',vert,'FaceVertexCData', [0, 1, 0; 0, 0, 1]);
109
110 %!demo
111 %! ## Property change on multiple patches
112 %! clf
113 %! t1 = (1/16:1/8:1)'*2*pi;
114 %! t2 = ((1/16:1/8:1)' + 1/32)*2*pi;
115 %! x1 = sin (t1) - 0.8;
116 %! y1 = cos (t1);
117 %! x2 = sin (t2) + 0.8;
118 %! y2 = cos (t2);
119 %! h = patch([x1,x2],[y1,y2],cat (3,[0,0],[1,0],[0,1]));
120 %! pause (1);
121 %! set (h, 'FaceColor', 'r');
122
123 %!demo
124 %! clf
125 %! vertices = [0, 0, 0;
126 %!             1, 0, 0;
127 %!             1, 1, 0;
128 %!             0, 1, 0;
129 %!             0.5, 0.5, 1];
130 %! faces = [1, 2, 5;
131 %!          2, 3, 5;
132 %!          3, 4, 5;
133 %!          4, 1, 5];
134 %! patch ('Vertices', vertices, 'Faces', faces, ...
135 %!        'FaceVertexCData', jet(4), 'FaceColor', 'flat');
136 %! view (-37.5, 30);
137
138 %!demo
139 %! clf
140 %! vertices = [0, 0, 0;
141 %!             1, 0, 0;
142 %!             1, 1, 0;
143 %!             0, 1, 0;
144 %!             0.5, 0.5, 1];
145 %! faces = [1, 2, 5;
146 %!          2, 3, 5;
147 %!          3, 4, 5;
148 %!          4, 1, 5];
149 %! patch ('Vertices', vertices, 'Faces', faces, ...
150 %!        'FaceVertexCData', jet(5), 'FaceColor', 'interp');
151 %! view (-37.5, 30);
152
153 %!demo
154 %! clf
155 %! colormap (jet);
156 %! x = [0 1 1 0];
157 %! y = [0 0 1 1];
158 %! subplot (2, 1, 1);
159 %! title ("Blue, Light-Green, and Red Horizontal Bars");
160 %! patch (x, y + 0, 1);
161 %! patch (x, y + 1, 2);
162 %! patch (x, y + 2, 3);
163 %! subplot (2, 1, 2);
164 %! title ("Blue, Light-Green, and Red Vertical Bars");
165 %! patch (x + 0, y, 1 * ones (size (x)));
166 %! patch (x + 1, y, 2 * ones (size (x)));
167 %! patch (x + 2, y, 3 * ones (size (x)));
168
169 %!demo
170 %! clf
171 %! colormap (jet);
172 %! x = [0 1 1 0];
173 %! y = [0 0 1 1];
174 %! subplot (2, 1, 1);
175 %! title ("Blue horizontal bars: Dark to Light");
176 %! patch (x, y + 0, 1, "cdatamapping", "direct");
177 %! patch (x, y + 1, 9, "cdatamapping", "direct");
178 %! patch (x, y + 2, 17, "cdatamapping", "direct");
179 %! subplot (2, 1, 2);
180 %! title ("Blue vertical bars: Dark to Light")
181 %! patch (x + 0, y, 1 * ones (size (x)), "cdatamapping", "direct");
182 %! patch (x + 1, y, 9 * ones (size (x)), "cdatamapping", "direct");
183 %! patch (x + 2, y, 17 * ones (size (x)), "cdatamapping", "direct");
184
185 %!demo
186 %! clf;
187 %! colormap (jet);
188 %! x = [ 0 0; 1 1; 1 0 ];
189 %! y = [ 0 0; 0 1; 1 1 ];
190 %! p = patch (x, y, "facecolor", "b");
191 %! title ("Two blue triangles");
192 %! set (p, "cdatamapping", "direct", "facecolor", "flat", "cdata", [1 32]);
193 %! title ("Direct mapping of colors: Light-Green UL and Blue LR triangles");
194
195 %!demo
196 %! clf;
197 %! colormap (jet);
198 %! x = [ 0 0; 1 1; 1 0 ];
199 %! y = [ 0 0; 0 1; 1 1 ];
200 %! p = patch (x, y, [1 32]);
201 %! title ("Autoscaling of colors: Red UL and Blue LR triangles");
202
203 %!test
204 %! hf = figure ("visible", "off");
205 %! unwind_protect
206 %!   h = patch;
207 %!   assert (findobj (hf, "type", "patch"), h);
208 %!   assert (get (h, "xdata"), [0; 1; 0], eps);
209 %!   assert (get (h, "ydata"), [1; 1; 0], eps);
210 %!   assert (isempty (get (h, "zdata")));
211 %!   assert (isempty (get (h, "cdata")));
212 %!   assert (get (h, "faces"), [1, 2, 3], eps);
213 %!   assert (get (h, "vertices"), [0 1; 1 1; 0 0], eps);
214 %!   assert (get (h, "type"), "patch");
215 %!   assert (get (h, "facecolor"), [0 0 0]);
216 %!   assert (get (h, "linestyle"), get (0, "defaultpatchlinestyle"));
217 %!   assert (get (h, "linewidth"), get (0, "defaultpatchlinewidth"), eps);
218 %!   assert (get (h, "marker"), get (0, "defaultpatchmarker"));
219 %!   assert (get (h, "markersize"), get (0, "defaultpatchmarkersize"));
220 %! unwind_protect_cleanup
221 %!   close (hf);
222 %! end_unwind_protect
223
224 %!test
225 %! hf = figure ("visible", "off");
226 %! c = 0.9;
227 %! unwind_protect
228 %!   h = patch ([0 1 0], [0 1 1], c);
229 %!   assert (get (gca, "clim"), [c - 1, c + 1]);
230 %!   h = patch ([0 1 0], [0 1 1], 2 * c);
231 %!   assert (get (gca, "clim"), [c, 2 * c]);
232 %! unwind_protect_cleanup
233 %!   close (hf);
234 %! end_unwind_protect
235