# include # include # include using namespace std; # include "closest_pair_brute.hpp" //****************************************************************************80 void closest_pair_brute ( int n, double xy[], double &d, int &i, int &j ) //****************************************************************************80 // // Purpose: // // closest_pair_brute() finds closest pair of points in 2d using brute force. // // License: // // This code is distributed under the MIT license. // // Modified: // // 03 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: // // int n: the number of points. // // double xy(n,2): the X and Y coordinates of the points. // // Output: // // double &d: the distance of the closest pair. // // int &i, &j: the indices of the closest pair. // { double d2; int i2; int j2; d = HUGE_VAL; i = -1; j = -1; for ( i2 = 0; i2 < n; i2++ ) { for ( j2 = i2 + 1; j2 < n; j2++ ) { d2 = pow ( xy[i2+0*n] - xy[j2+0*n], 2 ) + pow ( xy[i2+1*n] - xy[j2+1*n], 2 ); d2 = sqrt ( d2 ); if ( d2 < d ) { d = d2; i = i2; j = j2; } } } return; }