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

By : Steve
Source: Stackoverflow.com
Question!

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).

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"

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

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.

Thanks, Steve

By : Steve

PsychoPy's `ShapeStim` classes have a `.contains()` method, as per the API: http://psychopy.org/api/visual/shapestim.html#psychopy.visual.ShapeStim.contains

So your code could simply be:

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

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).

I don't think it needs to be that complicated:

``````center=(600,600)
coordinate=(50,70)

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

You can use a `WHERE` clause for this. Convert your INSERT VALUES to an INSERT SELECT and add a `WHERE` clause.

For example,

``````INSERT INTO fields (field_name, control_type_id, needs_approval)
SELECT
'Array Photos', 3, 0
WHERE Condition;
``````

If Condition is true, it will insert the row. If Condition is false, the `SELECT` will return zero rows and thus the INSERT will insert zero rows.