如何检查圆圈内的哪些点?

时间:2018-01-09 16:06:01

标签: python geometry patch point

我有一个数据框df,其中包含系统中所有点(IDs)之间的距离。所以df如下所示:

df
    radius      ID1     ID2     x1          y1          x2          y2
0   0.454244    100     103     103.668919  1.335309    103.671812  1.332424
1   1.016734    100     123     103.668919  1.335309    103.677598  1.332424
2   0.643200    103     123     103.671812  1.332424    103.677598  1.332424
3   1.605608    100     124     103.668919  1.335309    103.677598  1.346851
4   1.728349    103     124     103.671812  1.332424    103.677598  1.346851

我想计算所有点之间的圆,然后检查那个圆内的女巫点。对于每个点,我在一个单独的数据框coordinates中有坐标。

coordinates
    ID      x           y
0   100     103.668919  1.335309
1   103     103.671812  1.332424
2   124     103.677598  1.346851
3   125     103.677598  1.349737
4   134     103.680491  1.341080
5   135     103.680491  1.343966
6   136     103.680491  1.346851
7   137     103.680491  1.349737
8   138     103.680491  1.352622
9   146     103.683384  1.341080     

这里是代码

from matplotlib.patches import Circle 

for i in df.index:
    x = df.x1[i]
    y = df.y1[i]
    circ = Circle((x, y), radius = df.radius)
    ## it works until here: from now I need to understand what to do 
    ## and in particular I need to find which points are inside the circle

    points = circ.contains_point([coordinates.x, coordinates.y])

返回错误

ValueError: setting an array element with a sequence.

1 个答案:

答案 0 :(得分:3)

当我遇到这样的问题时,我总是做一个小小的理智测试:

from matplotlib.patches import Circle
circ = Circle((0, 0), radius = 1)

print(circ.contains_point([0.5,0.5]))
print(circ.contains_point([2,2]))

我得到(正如预期的那样)

True
False

所以coordinates.xcoordinates.y可能是数组,它解释了这个消息。

contains_points适用于tuplelist个2个标量。

要生成列表,您可以在列表解析中执行循环:

points = [(x,y) for x,y in zip(coordinates.x, coordinates.y) if circ.contains_point(x,y)]