# include # include # include # include # include "closest_point_brute.h" /******************************************************************************/ void closest_point_brute ( int m, int nr, double r[], double s[], int *near_index, double *near_dist ) /******************************************************************************/ /* Purpose: 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: 14 August 2024 Author: John Burkardt Input: int M, the spatial dimension. int NR, the number of data points. double R[M*NR], the data points. double S[M], the point whose nearest R value is desired. Output: int *near_index: the index of the nearest data point. double *near_dist: the distance of the nearest data point. */ { double dist_sq; int i; int jr; *near_dist = DBL_MAX; *near_index = -1; for ( jr = 0; jr < nr; jr++ ) { dist_sq = 0.0; for ( i = 0; i < m; i++ ) { dist_sq = dist_sq + pow ( r[i+jr*m] - s[i], 2 ); } if ( dist_sq < *near_dist ) { *near_dist = dist_sq; *near_index = jr; } } *near_dist = sqrt ( *near_dist ); return; }