1########################################################################
2##
3## Copyright (C) 2007-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} triplot (@var{tri}, @var{x}, @var{y})
28## @deftypefnx {} {} triplot (@var{tri}, @var{x}, @var{y}, @var{linespec})
29## @deftypefnx {} {@var{h} =} triplot (@dots{})
30## Plot a 2-D triangular mesh.
31##
32## @var{tri} is typically the output of a Delaunay triangulation over the
33## grid of @var{x}, @var{y}.  Every row of @var{tri} represents one triangle
34## and contains three indices into [@var{x}, @var{y}] which are the
35## vertices of the triangles in the x-y plane.
36##
37## The linestyle to use for the plot can be defined with the argument
38## @var{linespec} of the same format as the @code{plot} command.
39##
40## The optional return value @var{h} is a graphics handle to the created
41## patch object.
42## @seealso{plot, trimesh, trisurf, delaunay}
43## @end deftypefn
44
45function h = triplot (tri, x, y, varargin)
46
47  if (nargin < 3)
48    print_usage ();
49  endif
50
51  idx = tri(:, [1, 2, 3, 1]).';
52  nt = rows (tri);
53  htmp = plot ([x(idx); NaN(1, nt)](:),
54               [y(idx); NaN(1, nt)](:), varargin{:});
55
56  if (nargout > 0)
57    h = htmp;
58  endif
59
60endfunction
61
62
63%!demo
64%! clf;
65%! old_state = rand ("state");
66%! restore_state = onCleanup (@() rand ("state", old_state));
67%! rand ("state", 2);
68%! N = 20;
69%! x = rand (N, 1);
70%! y = rand (N, 1);
71%! tri = delaunay (x, y);
72%! triplot (tri, x, y);
73%! title ("triplot() of random 2-D Delaunay triangulation");
74