1## Copyright (C) 2003, 2007, 2008, 2009 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.
51## contourc (x, y, z, 2:3)
52## @result{} 2.0000 2.0000 1.0000 3.0000 1.5000 2.0000
53## 2.0000 1.0000 2.0000 2.0000 2.0000 1.5000
60## Author: Shai Ayal <shaiay@users.sourceforge.net>
62function [cout, lev] = contourc (varargin)
91 vv = linspace (min (z(:)), max (z(:)), vn+2)(2:end-1);
93 vv = unique (sort (vn));
96 if (isvector (x) && isvector (y))
97 c = __contourc__ (x(:)', y(:)', z, vv);
99 ## Indexes x,y for the purpose of __contourc__.
103 ## Now call __contourc__ for the real work...
104 c = __contourc__ (ii, jj, z, vv);
106 ## Map the contour lines from index space (i,j) back
107 ## to the original grid (x,y)
110 while (i < size (c,2))
112 ind = i + [1 : clen];
117 ## due to rounding errors some elements of ci and cj
118 ## can fall out of the range of ii and jj and interp2 would
119 ## return NA for those values.
120 ## The permitted range is enforced here:
122 ci = max (ci, 1); ci = min (ci, size (z, 2));
123 cj = max (cj, 1); cj = min (cj, size (z, 1));
125 c(1, ind) = interp2 (ii, jj, x, ci, cj);
126 c(2, ind) = interp2 (ii, jj, y, ci, cj);
143%! [c_actual, lev_actual]= contourc (x, y, z, 2:3);
144%! c_expected = [2, 1, 1, 2, 2, 3, 1.5, 2; 4, 2, 2, 1, 1, 2, 2, 1.5];
145%! lev_expected = [2 3];
146%! assert (c_actual, c_expected, eps)
147%! assert (lev_actual, lev_expected, eps)