function [ d, kout, jout ] = center ( z, din )

%*****************************************************************************80
%
%% center() examines the strip of half-width d about the center point.
%
%  License:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    02 April 2024
%
%  Author:
%
%    Original MATLAB code by Cleve Moler.
%    This version by John Burkardt.
%
%  Reference:
%
%    Cleve Moler,
%    https://blogs.mathworks.com/cleve/2024/03/28/closest-pair-of-points-problem/
%    Closest pair of points problem,
%    28 March 2024.
%
%  Input:
%
%    complex z(n): the X and Y coordinates of the points.
%
%    real din: the width of the center strip.
%
%  Output:
%
%    real d: the distance of the closest pair in the strip.
%
%    integer kout, jout: the indices of the closest pair in the strip.
%
  n = length ( z );
  m = floor ( n / 2 );
  xh = real ( z(m) );
  d = din;

  [ ~, p ] = sort ( imag ( z ) );
  z = z(p);

  s = [];
  ks = [];
  ns = 0;
  for k = 1 : n
    if ( abs ( real ( z(k) ) - xh ) < d )
      ns = ns + 1;
      s(ns,1) = z(k);
      ks(ns,1) = k;
    end
  end
%
%  Now check pairs in the strip.
%
  kout = NaN;
  jout = NaN;
  for k = 1 : ns
    for j = k + 1 : ns
      if ( ( imag ( s(j) ) - imag ( s(k) ) ) < d && abs ( s(k) - s(j) ) < d )
        d = abs ( s(k) - s(j) );
        kout = p(ks(k));
        jout = p(ks(j));
      end
    end
  end

  return
end
