I have bunch of coordinates stored in a vector as follows
1 2
|
// the first number is for x coordinate and the second one is for y
std::vector< pair<double, double> > xy;
|
there will be at least 1000000 points stored in the vector. Those points will makeup, more or less, a "close area" and my purpose is to verify if a given point is contained in that area or not. For doing that, one way I can think of is to search all points with x within a certain range around the given points first and then check if the range of the resulting y coordinates contains the y component of the given point too. Firstly, I sort the vector on x
1 2
|
bool myfun(pair<double, double> a, pair<double, double> b) {return (a.first<b.first);}
sort(xy.begin(), xy.end(), myfun);
|
the sorting function doesn't take too long. After the vector sorted, now I can search the vector on the x coordinates to locate the range near the given X (says X0)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
double miny = 10000.0;
double maxy = -10000.0;
double X0, Y0;
//X0 and Y0 is given point being verified
for (int j=0; j<xy.size(); j++)
{
x = xy.first;
if (abs(x-THETA)<0.001)
{
y = xy.second;
if (y>maxy) maxy=y;
if (y<miny) miny=y;
}
else if (x-X0>1.0) break; // since x is in ascending order, when diff b/w x and X is larger than 1.0, just stop the loop
}
|
Now, we get miny and maxy, if Y0 is within [miny, maxy] then we consider that the point (X0, Y0) is contained in the bound denotes by all points given in xy.
If I have only one point (X0, Y0) to verify, this algorithm won't be too bad. But in my case, I have sooooo many points to verify and it is really slow to run the above algorithm. Is that any better way to do the searching, maybe with some STL algorithm?