#include < iostream >
#include < algorithm >
#include < iterator >
#include < iomanip >
#include < cmath >
#include < cstdlib >
struct Pt{
double x,y;
static Pt random_point_100_100() {Pt p; p.x = ::rand() % 100; p.y = ::rand() % 100; return p;}
};
class NearThan
{
Pt pt;
// Обрати внимание, для поиска минимума совсем не обязательно извлекать квадратный корень!
// Потому, что нам не нужно именно само значение расстояния, а нужно просто сравнить два расстояния
// между собой. А это можно сделать и на квадратах. Т. е. исключаем одну лишнюю операцию извлечения
// корня!
double sqDistTo(const Pt& p) const
{
double dx = p.x - pt.x;
double dy = p.y - pt.y;
return dx*dx + dy*dy;
}
public:
NearThan(const Pt& _pt): pt(_pt){ }
bool operator()(const Pt& p1, const Pt& p2) const { return sqDistTo(p1) < sqDistTo(p2); }
};
int main()
{
Pt A[ 10 ];
int N = 10;
Pt B;
std::generate(A,A+N,Pt::random_point_100_100);
std::cout << "A set is: ";
for(int i = 0; i < N; ++i) std::cout << "( " << A[ i ].x << " " << A[ i ].y << " ) ";
std::cout << std::endl;
std::cout << "Enter B point x y coords:" << std::flush; std::cin >> B.x >> B.y;
Pt* nearest = std::min_element(A,A+N,NearThan(B));
std::cout << "Nearest point is ( " << nearest->x << " " << nearest->y << " )" << std::endl;
return 0;
}