function cvt_1_movie ( n, r, ratio, it_max, movie_name )

%*****************************************************************************80
%
%% cvt_1_movie() creates a movie of a CVT generation process.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    16 December 2020
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Qiang Du, Vance Faber, and Max Gunzburger,
%    Centroidal Voronoi Tessellations: Applications and Algorithms,
%    SIAM Review, Volume 41, 1999, pages 637-676.
%
%  Input:
%
%    integer N, the number of generators;
%
%    real R(2,N), the initial locations of the generators.
%
%    integer RATIO, the number of sample points per generator.
%
%    integer IT_MAX, the number of iterations;
%
%    character MOVIE_NAME, the name of the movie file to be created.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'cvt_1_movie:\n' );
  fprintf ( 1, '  Make a movie of a CVT iteration process.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The number of generators N = %d.\n', n );
  fprintf ( 1, '  Sample points per generator RATIO = %d.\n', ratio );
  fprintf ( 1, '  The number of iterations IT_MAX = %d.\n', it_max );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Step    Diff     Energy\n' );
  fprintf ( 1, '\n' );
%
%  Initialize the movie.
%
  colormap ( 'jet' );
  V = VideoWriter ( movie_name, 'Motion JPEG AVI' );
  V.FrameRate = 30;
  open ( V );
  figure ( 1 );

  for it = 0 : it_max

    clf ( );
    hold ( 'on' );
    scatter ( r(1,1:n), r(2,1:n), [], 'r', 'filled' );
    line ( [ 0.0, 1.0, 1.0, 0.0, 0.0 ], [ 0.0, 0.0, 1.0, 1.0, 0.0 ] );
    axis ( [ -0.05, 1.05, -0.05, 1.05 ] );
    axis ( 'equal' );
    grid ( 'on' );
    xlabel ( '<-- X -->', 'FontSize', 16 );
    ylabel ( '<-- Y -->', 'FontSize', 16 );
    label = sprintf ( 'Iteration %d', it );
    title ( label, 'FontSize', 16 );
    hold ( 'off' );

    frame = getframe ( 1 );
    writeVideo ( V, frame );

    [ r, diff, energy ] = cvt_iterate ( n, r, ratio );
    fprintf ( 1, '  %4d  %14e  %14e\n', it, diff, energy );

  end

  close ( V );
  fprintf ( 1, '  Movie saved as "%s"\n', movie_name );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'cvt_1_movie:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
 
  return
end
