function e = triangle_to_edge ( t )

%*****************************************************************************80
%
%% triangle_to_edge() converts a polyiamond description from triangle to edge.
%
%  Discussion:
%
%    The triangle list is a list of the IJK parallelogram triangles
%    that constitute the polyiamond.
%    If k = 0, the "ijk" triangle has vertices
%      (i-1,j-1), (i,j-1), (i-1,j).
%    If k = 1, the "ijk" triangle has vertices
%      (i,j), (i-1,j), (i,j-1).
%
%    The edge list is a list of the IJ coordinates of all the nodes
%    on the edge of the polyiamond shape, listed in counterclockwise order.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer T(TN,3), the IJK parallelogram triangle coordinates of the 
%    triangles making up the polyiamond. 
%
%  Output:
%
%    integer E(EN,2), the IJ coordinates of the triangle nodes that lie
%    on the boundary of the polyiamond, in counterclockwise order.
%
  tn = size ( t, 1 );
%
%  The P array contains the edges that form triangle sides.
%
  pn = 0;
  p = zeros ( pn, 4 );
  for ti = 1 : tn
    i1 = 1 + 3*(ti-1);
    i2 = 3 + 3*(ti-1);
    ij = ijk_to_ij ( t(ti,1:3) );
    pn = pn + 1;
    p(pn,1:4) =  [ ij(1,1:2), ij(2,1:2) ];
    pn = pn + 1;
    p(pn,1:4) =  [ ij(2,1:2), ij(3,1:2) ];
    pn = pn + 1;
    p(pn,1:4) =  [ ij(3,1:2), ij(1,1:2) ];
  end

  p = sortrows ( p );
%
%  The N array contains edges in reverse order.
%
  nn = pn;
  n = zeros ( nn, 4 );
  n(1:nn,1:2) = p(1:pn,3:4);
  n(1:nn,3:4) = p(1:pn,1:2);

  n = sortrows ( n );
%
%  If a row of N equals a row of P, then the corresponding
%  edge occurs in both positive and negative senses, and
%  hence is an interior edge.
%
  ni = 1;
  bn = 0;
  b = zeros ( bn, 4 );

  for pi = 1 : pn
%
%  N and P are sorted, and we are looking for matches.
%
    while ( i4vec_lt ( n(ni,1:4), p(pi,1:4) ) )
      if ( nn <= ni )
        break
      end
      ni = ni + 1;
    end
%
%  If N == P, this edge is eliminated.
%
    if ( all ( n(ni,1:4) == p(pi,1:4) ) )
      continue
    end

    bn = bn + 1;
    b(bn,1:4) = p(pi,1:4);

  end
%
%  From pairs of edges, form a consecutive (counterclockwise) edge list.
%
  en = 0;
  e = zeros ( bn, 2 );
  en = en + 1;
  ib = 1;
  e(en,1:2) = b(ib,1:2);
  for en = 2 : bn
    jb = 0;
    for i = 1 : bn
      if ( all ( b(i,1:2) == b(ib,3:4) ) )
        jb = i;
        break;
      end
    end
    ib = jb;
    e(en,1:2) = b(ib,1:2);
  end

  return
end

