I am trying to implement a simpler version of this algorithm, but which works better than the quadratic algorithm. My idea is to sort the points only by the x coordinate and try to solve it from there. As soon as I sort my array of points by x coordinate, I want to iterate over the array and basically skip points whose distance is greater than the first two points I took.
For example, my currentminDist = x;
If the two pairs of points I'm looking at have a distance> x (only in its x-coordinate), I ignore the point and move it in the array.
I have an idea, but I'm kind of fixated on how to really implement this (especially part of the state). I have a function that returns me the distance between two points based on their x coordinate.
I am confused about how to actually write my conditions for my loop, since I want to ignore the point if the distance is too far and still fill my array, which will contain answers for the nearest points for each i (i currently i look).
Any advice or guidance is appreciated. I am not very good at coding algorithms, so it is rather disappointing.
Here is part of my code:
for (i = 0; i < numofmypoints; i++)
{
for (int j = i + 1; (j < numpofmypoints) && ((inputpoints[j].x - inputpoints[i].x) < currbest); j++ )
{
currdist = Auxilary.distbyX(inputpoints[i],inputpoints[j]);
if (currdist < bestdist)
{
closest[i] = j;
bestdist = currdist;
}
}
}
distbyX is my function that just returns the distance between two points.
Thank!
source
share