[英]Given many circles and a point, how to find which circle contains that point
I recently came across a problem which is something like this我最近遇到了一个类似这样的问题
There are N disjoint (such that they do not touch or intersect) circles given by their center and radius, ie
center = (x_i, y_i), radius = r_i
.有 N 个不相交(使得它们不接触或相交)的圆,由它们的中心和半径给出,即
center = (x_i, y_i), radius = r_i
。 Then we have Q queries where a point(x, y)
is given.然后我们有 Q 个查询,其中给出了一个点
(x, y)
。 For each query we need to find out the indexi
of the circle which contains that given point (-1 if no circle).对于每个查询,我们需要找出包含给定点的圆的索引
i
(如果没有圆,则为 -1)。
The constraints are roughly1 <= N <= 10^5
and1 <= Q <= 10^5
.约束大致为
1 <= N <= 10^5
和1 <= Q <= 10^5
。 So aO(Q * log(N))
might be needed.因此可能需要
O(Q * log(N))
。
Apart from the straight-forward O(Q * N)
solution the only better thing I can think of is keeping the leftmost and rightmost points of the circles as intervals in an array and then doing binary search to find out the intervals which contains the x-coordinate of the point, but more than one intervals may be overlapping and more than one circle may contain the point.除了直接的
O(Q * N)
解决方案之外,我能想到的唯一更好的方法是将圆圈的最左边和最右边的点作为数组中的间隔,然后进行二进制搜索以找出包含 x 的间隔- 点的坐标,但多个间隔可能重叠并且多个圆可能包含该点。 So I'm not sure if that's going to work.所以我不确定这是否行得通。
Any help would be highly appreciated.任何帮助将不胜感激。 Thank you.
谢谢你。
This can be solved as a nearest-neighbour query in N+1 dimensions.这可以作为 N+1 维的最近邻查询来解决。
Imagine a set of balls of a fixed radius in 3d, such that their intersection with the plane z=0 is exactly your set of circles.想象一组在 3d 中具有固定半径的球,这样它们与平面 z=0 的交点就是您的一组圆。 (The balls may intersect, it doesn't matter).
(球可能相交,没关系)。 Now a point that falls into a circle is necessarily closer to the centre of its corresponding ball than to centres of all other balls.
现在,落入圆中的点与相应球的中心的距离必然比所有其他球的中心更近。
The nearest-neighbour problem is well studied.最近邻问题得到了很好的研究。 Space partitioning techniques work well with real life data, though worst-case performance is not so good.
空间分区技术可以很好地处理现实生活中的数据,但最坏情况下的性能并不是那么好。
Edit: since the query point in in the fixed plane z=0 , the problem can be seen as a 2d nearest-neighbour problem with non-Euclidean distance function.编辑:由于查询点在固定平面z=0 中,因此可以将问题视为具有非欧几里得距离函数的 2d 最近邻问题。 The effective distance from a query point to the centre of a circle is
查询点到圆心的有效距离为
D = &sqrt;(d 2+R2 - r2) D = &sqrt;(d 2+R2 - r2)
where d is the real distance, R is the ball radius (conmon for all balls) and r is the circle radius.其中d是实际距离, R是球半径(适用于所有球), r是圆半径。
Another way to solve this is to build a power diagram of the set of circles.解决此问题的另一种方法是构建一组圆的功率图。 A power diagram is a plane subdivision.
功率图是平面细分。 There are ways to efficiently answer queries of the form "which cell of a plane subdivision given point belongs to", for example, using Kirkpatrik's point location data structure .
有一些方法可以有效地回答“给定点属于平面细分的哪个单元格”形式的查询,例如,使用Kirkpatrik 的点位置数据结构。
The two approaches are similar, if not equivalent, because in the power diagram, the power of the point with respect to a circle is the square of D in the formula for distance (with R=0).这两种方法是相似的,如果不是等价的,因为在幂图中,点相对于圆的幂是距离公式中 D 的平方(R=0)。
I know this is a quite old question but for the records.... You can solve it with matplotlib Circle我知道这是一个很老的问题但是为了记录....你可以用 matplotlib Circle 解决它
from matplotlib.patches import Circle as Cr
.................
self.my_mpl_circle = Cr(origin, radius)
......
def match_pos(self, coords):
return self.my_mpl_circle.contains_point(coords)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.