function v = voronoi_polygon_vertices_2d ( node, neighbor_num, ...
  neighbor_index, node_num, p )

%% VORONOI_POLYGON_VERTICES_2D computes the vertices of a Voronoi polygon in 2D.
%
%  Discussion:
%
%    This routine is only appropriate for Voronoi polygons that are finite.
%
%    The routine is given the indices of the nodes that are neighbors of a 
%    given "center" node.  A node is a neighbor of the center node if the
%    Voronoi polygons of the two nodes share an edge.  The triangles of the 
%    Delaunay triangulation are formed from successive pairs of these neighbor 
%    nodes along with the center node.
%
%    Given only the neighbor node information, it is possible to determine
%    the location of the vertices of the polygonal Voronoi region by computing
%    the circumcenters of the Delaunay triangles.
%
%    It is assumed that this subroutine is being called by a process which has 
%    computed the Voronoi diagram of a large set of nodes, so the arrays X and 
%    Y are dimensioned by NODE_NUM, which may be much greater than the number 
%    of neighbor nodes.
%
%  Modified:
%
%    06 February 2005
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Atsuyuki Okabe, Barry Boots, Kokichi Sugihara, Sung Nok Chiu,
%    Spatial Tesselations: Concepts and Applications of Voronoi Diagrams,
%    Second Edition,
%    Wiley, 2000.
%
%  Parameters:
%
%    Input, integer NODE, the index of the node whose Voronoi
%    polygon is to be analyzed.  1 <= NODE <= NODE_NUM.
%
%    Input, integer NEIGHBOR_NUM, the number of neighbor nodes of
%    the given node.
%
%    Input, integer NEIGHBOR_INDEX(NEIGHBOR_NUM), the indices
%    of the neighbor nodes.  These indices are used to access the
%    X and Y arrays.  The neighbor nodes should be listed in the
%    (counter-clockwise) order in which they occur as one circles 
%    the center node.
%
%    Input, integer NODE_NUM, the total number of nodes.
%
%    Input, real P(2,NODE_NUM), the nodes.
%
%    Output, real V(2,NEIGHBOR_NUM), the coordinates of 
%    the vertices of the Voronoi polygon around node NODE.
%
  ndim = 2;

  if ( node < 1 | node_num < node )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'VORONOI_POLYGON_VERTICES_2D - Fatal error!\n' );
    fprintf ( 1, '  Illegal value of input parameter NODE.\n' );
    error ( 'VORONOI_POLYGON_VERTICES_2D - Fatal error!' );
  end

  t(1:ndim,1) = p(1:ndim,node);

  ip1 = neighbor_index(1);
  t(1:ndim,3) = p(1:ndim,ip1);
 
  for i = 1 : neighbor_num

    t(1:ndim,2) = t(1:ndim,3);

    ip1 = i_wrap ( i+1, 1, neighbor_num );
    ip1 = neighbor_index(ip1);
    t(1:ndim,3) = p(1:ndim,ip1);

    v(1:ndim,i) = triangle_circumcenter_2d ( t );

  end

  return
end
