1 ## Copyright (C) 2003-2012 Shai Ayal
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} {[@var{c}, @var{lev}] =} contourc (@var{x}, @var{y}, @var{z}, @var{vn})
21 ## Compute isolines (contour lines) of the matrix @var{z}.
22 ## Parameters @var{x}, @var{y} and @var{vn} are optional.
24 ## The return value @var{lev} is a vector of the contour levels.
25 ## The return value @var{c} is a 2 by @var{n} matrix containing the
26 ## contour lines in the following format
30 ## @var{c} = [lev1, x1, x2, @dots{}, levn, x1, x2, ...
31 ## len1, y1, y2, @dots{}, lenn, y1, y2, @dots{}]
36 ## in which contour line @var{n} has a level (height) of @var{levn} and
37 ## length of @var{lenn}.
39 ## If @var{x} and @var{y} are omitted they are taken as the row/column
40 ## index of @var{z}. @var{vn} is either a scalar denoting the number of lines
41 ## to compute or a vector containing the values of the lines. If only one
42 ## value is wanted, set @code{@var{vn} = [val, val]};
43 ## If @var{vn} is omitted it defaults to 10.
52 ## contourc (x, y, z, 2:3)
53 ## @result{} 2.0000 2.0000 1.0000 3.0000 1.5000 2.0000
54 ## 2.0000 1.0000 2.0000 2.0000 2.0000 1.5000
60 ## Author: Shai Ayal <shaiay@users.sourceforge.net>
62 function [cout, lev] = contourc (varargin)
90 if (!ismatrix (z) || isvector (z) || isscalar (z))
91 error ("contourc: Z argument must be a matrix");
95 vv = linspace (min (z(:)), max (z(:)), vn+2)(2:end-1);
97 vv = unique (sort (vn));
100 if (isvector (x) && isvector (y))
101 c = __contourc__ (x(:)', y(:)', z, vv);
103 ## Indexes x,y for the purpose of __contourc__.
107 ## Now call __contourc__ for the real work...
108 c = __contourc__ (ii, jj, z, vv);
110 ## Map the contour lines from index space (i,j) back
111 ## to the original grid (x,y)
114 while (i < size (c,2))
116 ind = i + [1 : clen];
121 ## due to rounding errors some elements of ci and cj
122 ## can fall out of the range of ii and jj and interp2 would
123 ## return NA for those values.
124 ## The permitted range is enforced here:
126 ci = max (ci, 1); ci = min (ci, size (z, 2));
127 cj = max (cj, 1); cj = min (cj, size (z, 1));
129 c(1, ind) = interp2 (ii, jj, x, ci, cj);
130 c(2, ind) = interp2 (ii, jj, y, ci, cj);
147 %! [c_actual, lev_actual]= contourc (x, y, z, 2:3);
148 %! c_expected = [2, 1, 1, 2, 2, 3, 1.5, 2; 4, 2, 2, 1, 1, 2, 2, 1.5];
149 %! lev_expected = [2 3];
150 %! assert (c_actual, c_expected, eps)
151 %! assert (lev_actual, lev_expected, eps)