views:

8582

answers:

6

In python, if you create a circle with: newcircle = circle(center_x, center_y, radius)

How do you test if a given set of x/y coordinates are inside the circle?

+32  A: 

In general, x and y must satisfy (x-center_x)^2 + (y - center_y)^2 < radius^2.

Please note that points that satisfy the above equation with < replaced by == are considered the points on the circle, and the points that satisfy the above equation with < replaced by > are consider the exterior of the circle.

Jason
A: 

Calculate the Distance

D = Math.Sqrt(Math.Pow(center_x - x, 2) + Math.Pow(center_y - y, 2))
return D <= radius

that's in C#...convert for use in python...

Jason Punyon
You can avoid two expensive Sqrt calls by comparing D-squared to radius-squared.
Paul Tomblin
+13  A: 

You can use Pythagoras to measure the distance between your point and the centre and see if it's lower than the radius:

def in_circle(center_x, center_y, radius, x, y):
    dist = math.sqrt((center_x - x) ** 2 + (center_y - y) ** 2)
    return dist <= radius

EDIT (hat tip to Paul)

In practice, squaring is often much cheaper than taking the square root and since we're only interested in an ordering, we can of course forego taking the square root:

def in_circle(center_x, center_y, radius, x, y):
    square_dist = (center_x - x) ** 2 + (center_y - y) ** 2
    return square_dist <= radius ** 2

Also, Jason noted that <= should be replaced by < and depending on usage this may actually make sense even though I believe that it's not true in the strict mathematical sense. I stand corrected.

Konrad Rudolph
Replace dist <= radius by dist < radius to test for the point being inside the circle.
Jason
sqrt is expensive. Avoid it if possible - compare the x^2+y^y to r^2.
Paul Tomblin
Jason: our definitions may disagree but for me, a point that is *on* the circle's circumference is most emphatically also *in* the circle and I am pretty sure that mine is in agreement with the formal, mathematical definition.
Konrad Rudolph
@Paul: That's correct in practice but I wanted to illustrate Pythagoras. Other answers have included this optimization.
Konrad Rudolph
The formal mathematical definition of the interior of a circle is that which I gave in my post. From Wikipedia: In general, the interior of something refers to the space or part inside of it, excluding any kind of wall or boundary around its outside. http://en.wikipedia.org/wiki/Interior_(topology)
Jason
Proof by Wikipedia. ;-)
Konrad Rudolph
+3  A: 

You should check whether the distance from the center of the circle to the point is smaller than the radius, i.e.

if (x-center_x)**2 + (y-center_y)**2 <= radius**2:
    # inside circle
dF
A: 

As said above -- use Euclidean distance.

from math import hypot

def in_radius(c_x, c_y, r, x, y):
    return math.hypot(c_x-x, c_y-y) <= r
A: 

Does anyone also have a formula determining weither a x/y point is inside a pie chart part ? I'm trying to make a piechart on HTML5 canvas with mouseover/mouseclick actions.

Thank you !

Björn

bxlbjorn
Welcome to SO. If you have a new question (which you do), please ask it as a new question here: http://stackoverflow.com/questions/ask
AakashM