Reputation: 127
Here is a picture to illustrate the problem:
In the picture there are some feature points shown as blue crosses. I know the coordinate (x,y)
for all of the features. Now I want to query which features are inside the circle area (green circle). In practice, there are around 500 features and 300 queries (300 different circles, different centers and radius). I know the parameters (position, radius) of circles. Is there any good algorithm which can do this task?
Current I have two ideas.
Does anyone out there have any better ideas?
Upvotes: 2
Views: 1214
Reputation: 59358
The most common way is to put the points into a K-D tree or similar: https://en.wikipedia.org/wiki/K-d_tree
To find the points within a circle, you get the list of points in order of increasing distance from the center, and stop when the distance exceeds the circle radius.
To get list of points in order of increasing distance, you can use a priority queue that can hold both points and internal nodes of the tree, which lets you remove them in order of distance.
For points (leaves), the distance is just the distance of the point from the center point. For internal nodes, the distance is the smallest distance from the center point to any point that could possibly be in that node.
To search, you just put the root of the tree in the priority queue, and repeat:
This will produce all the points in the tree in order of increasing distance from the center point.
Upvotes: 3