I have a list of points in cartesian plane, and a test point. I want to find list indices of three points nearest to test point. What's the better way to find these indices? Thanks in advance for tour replies.
=== EDIT ===
I've found a solution in C++. At first I create a vector:
typedef struct
{
int iIndex;
double dSqrDistance;
} IndexedDistance;
std::vector<IndexedDistance> xDistanceVector;
and then a function to sort its elements
bool compareIndexedDistance(IndexedDistance xD1, IndexedDistance xD2)
{
return (xD1.dSqrDistance < xD2.dSqrDistance);
}
Then in a loop I calculate all distances, then I sort them and at the end I take first three elements:
IndexedDistance xDistanceElement;
for (int i = 0; i < xPointList.size(); i++)
{
dSqrDistance = xPointList.at(i).sqrDistance(xTestPoint);
xDistanceElement.iIndex = i;
xDistanceElement.dSqrDistance = dSqrDistance;
xDistanceVector.push_back(xDistanceElement);
}
std::sort(xDistanceVector.begin(), xDistanceVector.end(), compareIndexedDistance);
xDistanceVector.resize(3);
In this way, I found what I need. I don't know if it's the best way, but it seems to work.