Python/Psychopy: 检查一个点是否在圆内

Python/Psychopy: checking if a point is within a circle

我想知道检查给定点(眼睛坐标)是否在特定区域(在本例中为圆圈)内的最有效方法。

代码:

win = visual.Window([600,600], allowGUI=False)

coordinate = [50,70] #example x and y coordinates 

shape = visual.Circle(win, radius=120, units='pix') #shape to check if coordinates are within it

if coordinate in shape:
    print "inside"
else:
    print "outside"

>>TypeError: argument of type 'Circle' is not iterable

我的x和y坐标对应window上的一个点,我需要检查这个点是否落在半径为120像素的圆内。

谢谢, 史蒂夫

我觉得没必要那么复杂:

center=(600,600)
radius=120
coordinate=(50,70)

if (coordinate[0]-center[0])**2 + (coordinate[1]-center[1])**2 < radius**2:
    print "inside"
else:   
    print "outside"

PsychoPy 的 ShapeStim 类 有一个 .contains() 方法,根据 API: http://psychopy.org/api/visual/shapestim.html#psychopy.visual.ShapeStim.contains

因此您的代码可以简单地是:

if shape.contains(coordinate):
    print 'inside'
else:
    print 'outside'

使用此方法的优点是它是一个通用解决方案(考虑到刺激顶点的形状),而不仅仅是检查距刺激中心的毕达哥拉斯距离(这是圆的特例仅)。