I am a beginner in C++ and I'm writing a function that is supposed to find the closest point in a set of points ([x1,...xn]=positions_x,[y1,...yn]=positions_y) to a given point (x0,y0).
Here is my function:
double nearestPoint(double x, double y)
{
double ind_min = 0;
double xi = 0;
double yi = 0;
double dist_mini = 1000000;
int i;
for (i = 0; i < 50000; i=i+1)
xi = positions_x[i];
yi = positions_y[i];
double a = pow(x-xi,2);
double b = pow(y-yi,2);
if (sqrt(a+b)<=dist_mini)
dist_mini = sqrt(a+b);
ind_min = i;
return ind_min;
}
However, this always returns the exact same point. After investigation, it seems that the value of the "dist_mini" variable remains unchanged even if sqrt(a+b) is smaller. As a consequence, ind_min remains unchanged.
If you have any ideas it would really helpful,
Thank you