function [ near_index, near_dist ] = closest_point_brute ( m, nr, r, s )

%*****************************************************************************80
%
%% closest_point_brute() finds the nearest R point to an S point.
%
%  Discussion:
%
%    We are given R, a set of NR points in M dimensions.
%
%    We are given S, a point in M dimensions.
%
%    We seek the index J of the point R(J)
%    which is nearest to S over all points in R.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    17 August 2025
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, the spatial dimension.
%
%    integer NR, the number of cell generators.
%
%    real R(nr,m), the cell generators.
%
%    real S(M), the point to be checked.
%
%  Output:
%
%    integer near_index: the index of the nearest cell generator.
%
  near_dist = Inf;
  near_index = -1;

  for jr = 1 : nr

    dist_sq = 0.0;
    for i = 1 : m
      dist_sq = dist_sq + ( r(jr,i) - s(1,i) )^2;
    end

    if ( dist_sq < near_dist )
      near_dist = dist_sq;
      near_index = jr;
    end

  end

  near_dist = sqrt ( near_dist );

  return
end
