function [ ok, ot, oxy ] = object_elements ( w, p )

%*****************************************************************************80
%
%% object_elements() lists triangle elements in an object.
%
%  Discussion:
%
%    The triangles are described by indices of their nodes and
%    their type (1, 2, 3 or 4).
%    
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 December 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    character W(WN), the boundary word.
%
%    integer P(1,2): the coordinates of the base point.
%
%  Output:
%
%    integer OK(OTN,3), the indices of the nodes forming each triangle.
%
%    integer OT(OTN,1), the type of the triangle.
%
%    integer OXY(OXYN,2), the coordinates of the nodes.
%

%
%  Get the range of the object.
%
  [ imin, imax, jmin, jmax ] = word_range ( w, p );
%
%  Get the vertices of the object.
%
  v = word_to_vertex ( w, p );
%
%  Generate a rectangle that covers the object.
%
  nx = max ( v(:,1) ) - min ( v(:,1) );
  ny = max ( v(:,2) ) - min ( v(:,2) );
%
%  Get the nodes in the rectangle.
%
  rxy = rectangle_nodes ( nx, ny );
  rxyn = size ( rxy, 1 );
%
%  Adjust coordinates to [xmin,xmax], [ymin,ymax];
%
  rxy(:,1) = rxy(:,1) + min ( v(:,1) );
  rxy(:,2) = rxy(:,2) + min ( v(:,2) );
%
%  Get the triangles in the rectangle.
%
  [ rk, rt ] = rectangle_elements ( nx, ny );
  rtn = size ( rk, 1 );
%
%  Get the node index map.
%
  node_map = - ones ( rxyn, 1 );
  oxyi = 0;
  for rxyi = 1 : rxyn
    inside = polygon_contains_point ( v, rxy(rxyi,1:2) );
    if ( inside )
      oxyi = oxyi + 1;
      node_map(rxyi) = oxyi;
    end
  end
  oxyn = oxyi;
%
%  Collect those rectangle triangles that are contained in the object.
%
  otn = 0;
  ok = zeros ( otn, 3 );
  ot = zeros ( otn, 1 );

  oti = 0;
  for rti = 1 : rtn

    cx = 0.0;
    cy = 0.0;
    for j = 1 : 3
      k = rk(rti,j);
      cx = cx + rxy(k,1);
      cy = cy + rxy(k,2);
    end
    cx = cx / 3.0;
    cy = cy / 3.0;
    q = [ cx, cy ];

    inside = polygon_contains_point ( v, q );
%
%  OK needs to index the nodes using an object index, not the rectangle index.
%  The array node_map() handles this renumbering.
%
    if ( inside )
      oti = oti + 1;
      ok(oti,1:3) = node_map ( rk(rti,1:3) );
      ot(oti,1) = rt(rti,1);
    end

  end
  otn = oti;
%
%  Object node coordinates are selected rectangle node coordinates.
%
  oxy = zeros ( oxyn, 2 );
  oxyi = 0;
  for rxyi = 1 : rxyn
    if ( 0 < node_map ( rxyi ) )
      oxyi = oxyi + 1;
      oxy(oxyi,1) = rxy(rxyi,1);
      oxy(oxyi,2) = rxy(rxyi,2);
    end
  end

  return
end

