Python/Psychopy: checking if a point is within a circle
<p>I want to know the most efficient way to check if a given point (an eye coordinate) is within a specific region (in this case a circle).</p>

<p>Code:</p>

<pre><code>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"

&gt;&gt;TypeError: argument of type 'Circle' is not iterable
</code></pre>

<p>My x and y coordinates correspond to one point on the window, I need to check if this point falls within the circle whose radius is 120 pixels. </p>

<p>Thanks,
Steve</p>

<p>PsychoPy's <code>ShapeStim</code> classes have a <code>.contains()</code> method, as per the API:
<a href="http://psychopy.org/api/visual/shapestim.html#psychopy.visual.ShapeStim.contains">http://psychopy.org/api/visual/shapestim.html#psychopy.visual.ShapeStim.contains</a></p>

<p>So your code could simply be:</p>

<pre><code>if shape.contains(coordinate):
    print 'inside'
else:
    print 'outside'
</code></pre>

<p>Using this method has the advantage that it is a general solution (taking into account the shape of the stimulus vertices) and is not just a check on the pythagorean distance from the stimulus centre (which is a special case for circles only).</p>

python psychopy