If you have a circle with center (center_x, center_y)
and radius radius
, how do you test if a given point with coordinates (x, y)
is inside the circle?

- 236,483
- 35
- 423
- 525
-
20This question is really language agnostic, I am using the same formula in java, So re-tagging. – Gautam Jun 18 '12 at 11:02
-
It seems that your assuming positive coordinates only. The solutions below don't work with signed coordinates. – cjbarth Sep 26 '15 at 19:35
-
2Most solutions below *do* work with positive and negative coordinates. Just correcting that tidbit for future viewers of this question. – William Morrison Jul 07 '16 at 02:22
17 Answers
In general, x
and y
must satisfy (x - center_x)² + (y - center_y)² < radius²
.
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 considered the outside the circle.
-
8It may help some less math minded folks to see the square root operation used to measure distance compared against the radius. I realize that's not optimal, but as your answer is formatted more like an equation than code perhaps it makes more sense? Just a suggestion. – William Morrison Mar 05 '13 at 18:13
-
47This is the most comprehensible explanation provided in just a simple sentence and a immediately useable equation. Well done. – gchbib Oct 16 '13 at 13:09
-
this is great wish i would of found this resource quicker. Where does the value x come from? – Devin Tripp Apr 01 '15 at 08:07
-
2
-
10This may be obvious, but it should be stated that `<=` will find points inside the circle or on its edge. – Tyler May 09 '17 at 23:16
-
@makinbacon I found it obvious, but the fact that it wasn't mentioned made me wonder if there was something I wasn't thinking of. – scorgn Mar 18 '18 at 23:20
-
Mathematically, Pythagoras is probably a simple method as many have already mentioned.
(x-center_x)^2 + (y - center_y)^2 < radius^2
Computationally, there are quicker ways. Define:
dx = abs(x-center_x)
dy = abs(y-center_y)
R = radius
If a point is more likely to be outside this circle then imagine a square drawn around it such that it's sides are tangents to this circle:
if dx>R then
return false.
if dy>R then
return false.
Now imagine a square diamond drawn inside this circle such that it's vertices touch this circle:
if dx + dy <= R then
return true.
Now we have covered most of our space and only a small area of this circle remains in between our square and diamond to be tested. Here we revert to Pythagoras as above.
if dx^2 + dy^2 <= R^2 then
return true
else
return false.
If a point is more likely to be inside this circle then reverse order of first 3 steps:
if dx + dy <= R then
return true.
if dx > R then
return false.
if dy > R
then return false.
if dx^2 + dy^2 <= R^2 then
return true
else
return false.
Alternate methods imagine a square inside this circle instead of a diamond but this requires slightly more tests and calculations with no computational advantage (inner square and diamonds have identical areas):
k = R/sqrt(2)
if dx <= k and dy <= k then
return true.
Update:
For those interested in performance I implemented this method in c, and compiled with -O3.
I obtained execution times by time ./a.out
I implemented this method, a normal method and a dummy method to determine timing overhead.
Normal: 21.3s
This: 19.1s
Overhead: 16.5s
So, it seems this method is more efficient in this implementation.
// compile gcc -O3 <filename>.c
// run: time ./a.out
#include <stdio.h>
#include <stdlib.h>
#define TRUE (0==0)
#define FALSE (0==1)
#define ABS(x) (((x)<0)?(0-(x)):(x))
int xo, yo, R;
int inline inCircle( int x, int y ){ // 19.1, 19.1, 19.1
int dx = ABS(x-xo);
if ( dx > R ) return FALSE;
int dy = ABS(y-yo);
if ( dy > R ) return FALSE;
if ( dx+dy <= R ) return TRUE;
return ( dx*dx + dy*dy <= R*R );
}
int inline inCircleN( int x, int y ){ // 21.3, 21.1, 21.5
int dx = ABS(x-xo);
int dy = ABS(y-yo);
return ( dx*dx + dy*dy <= R*R );
}
int inline dummy( int x, int y ){ // 16.6, 16.5, 16.4
int dx = ABS(x-xo);
int dy = ABS(y-yo);
return FALSE;
}
#define N 1000000000
int main(){
int x, y;
xo = rand()%1000; yo = rand()%1000; R = 1;
int n = 0;
int c;
for (c=0; c<N; c++){
x = rand()%1000; y = rand()%1000;
// if ( inCircle(x,y) ){
if ( inCircleN(x,y) ){
// if ( dummy(x,y) ){
n++;
}
}
printf( "%d of %d inside circle\n", n, N);
}

- 4,042
- 3
- 28
- 33
-
2I'm curious to know if you have profiled these optimizations? My gut feeling is that multiple conditionals would be slower than some math and one conditional, but I could be wrong. – yoyo Jun 11 '14 at 17:04
-
3@yoyo, I have preformed no profiling - this question is about a method for any programming language. If someone thinks this might improve performance in their application then they should, as you suggest, demonstrate it is faster in normal scenarios. – philcolbourn Jun 12 '14 at 06:21
-
3In function `inCircleN` you're using unnecessary ABS. Probably without ABS difference between `inCircle` and `inCircleN` would be smaller. – zalogatomek Jun 20 '16 at 10:58
-
1
-
1Removing ABS does improve inCircleN performance but not enough. However, my method was biased towards points more likely outside of circle since R=1. With random radius [0..499], about 25% points were inside circle and inCircleN is faster. – philcolbourn Aug 18 '16 at 05:53
-
Small optimization: when you need to check multiple points in a certain circle of radius r, r*r is constant and can be calculated outside the loop. – Jan Mar 30 '17 at 08:53
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.

- 530,221
- 131
- 937
- 1,214
-
1Replace dist <= radius by dist < radius to test for the point being inside the circle. – jason Jan 26 '09 at 20:17
-
18sqrt is expensive. Avoid it if possible - compare the x^2+y^y to r^2. – Paul Tomblin Jan 26 '09 at 20:20
-
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 Jan 26 '09 at 20:20
-
@Paul: That's correct in practice but I wanted to illustrate Pythagoras. Other answers have included this optimization. – Konrad Rudolph Jan 26 '09 at 20:22
-
3The 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 Jan 26 '09 at 20:29
-
1In pascal, delphi and FPC, both power, and sqrt is to _expensive_, and there's no power-operator EG: `**` or `^`. The fastest way to do it when you just need x^2 or x^3 is to do it "manually": `x*x`. – b0bz Mar 21 '13 at 03:34
-
@JHolta, why do you think that Delphi's (or FPC's on x86) `Sqr` power function is expensive ? `Sqrt` might be, but that's what Konrad mentioned in his update suggested by Paul's comment... – TLama Jul 10 '13 at 08:35
-
@TLama i only said that `power`, and `Sqrt` is expensive. I did not mention `Sqr` at all. But I do believe that there is some overhead if you use `Sqr`, maybe the compiler is smart and solves it (replaces it with x*x to remove the functioncall). – b0bz Jul 10 '13 at 09:14
-
@JHolta, there is no overhead; both `Sqr` and `x*x` results to `imul` instruction ;-) – TLama Jul 10 '13 at 09:22
boolean isInRectangle(double centerX, double centerY, double radius,
double x, double y)
{
return x >= centerX - radius && x <= centerX + radius &&
y >= centerY - radius && y <= centerY + radius;
}
//test if coordinate (x, y) is within a radius from coordinate (center_x, center_y)
public boolean isPointInCircle(double centerX, double centerY,
double radius, double x, double y)
{
if(isInRectangle(centerX, centerY, radius, x, y))
{
double dx = centerX - x;
double dy = centerY - y;
dx *= dx;
dy *= dy;
double distanceSquared = dx + dy;
double radiusSquared = radius * radius;
return distanceSquared <= radiusSquared;
}
return false;
}
This is more efficient, and readable. It avoids the costly square root operation. I also added a check to determine if the point is within the bounding rectangle of the circle.
The rectangle check is unnecessary except with many points or many circles. If most points are inside circles, the bounding rectangle check will actually make things slower!
As always, be sure to consider your use case.

- 343
- 2
- 9

- 10,953
- 2
- 31
- 48
Find the distance between the center of the circle and the points given. If the distance between them is less than the radius then the point is inside the circle. if the distance between them is equal to the radius of the circle then the point is on the circumference of the circle. if the distance is greater than the radius then the point is outside the circle.
int d = r^2 - ((center_x-x)^2 + (center_y-y)^2);
if(d>0)
print("inside");
else if(d==0)
print("on the circumference");
else
print("outside");

- 4,711
- 4
- 37
- 63

- 221
- 2
- 6
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...

- 38,967
- 12
- 96
- 122
-
13You can avoid two expensive Sqrt calls by comparing D-squared to radius-squared. – Paul Tomblin Jan 26 '09 at 20:18
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
The equation below is a expression that tests if a point is within a given circle where xP & yP are the coordinates of the point, xC & yC are the coordinates of the center of the circle and R is the radius of that given circle.
If the above expression is true then the point is within the circle.
Below is a sample implementation in C#:
public static bool IsWithinCircle(PointF pC, Point pP, Single fRadius){
return Distance(pC, pP) <= fRadius;
}
public static Single Distance(PointF p1, PointF p2){
Single dX = p1.X - p2.X;
Single dY = p1.Y - p2.Y;
Single multi = dX * dX + dY * dY;
Single dist = (Single)Math.Round((Single)Math.Sqrt(multi), 3);
return (Single)dist;
}

- 1,193
- 1
- 6
- 21
This is the same solution as mentioned by Jason Punyon, but it contains a pseudo-code example and some more details. I saw his answer after writing this, but I didn't want to remove mine.
I think the most easily understandable way is to first calculate the distance between the circle's center and the point. I would use this formula:
d = sqrt((circle_x - x)^2 + (circle_y - y)^2)
Then, simply compare the result of that formula, the distance (d
), with the radius
. If the distance (d
) is less than or equal to the radius (r
), the point is inside the circle (on the edge of the circle if d
and r
are equal).
Here is a pseudo-code example which can easily be converted to any programming language:
function is_in_circle(circle_x, circle_y, r, x, y)
{
d = sqrt((circle_x - x)^2 + (circle_y - y)^2);
return d <= r;
}
Where circle_x
and circle_y
is the center coordinates of the circle, r
is the radius of the circle, and x
and y
is the coordinates of the point.

- 1
- 1

- 3,032
- 5
- 26
- 51
My answer in C# as a complete cut & paste (not optimized) solution:
public static bool PointIsWithinCircle(double circleRadius, double circleCenterPointX, double circleCenterPointY, double pointToCheckX, double pointToCheckY)
{
return (Math.Pow(pointToCheckX - circleCenterPointX, 2) + Math.Pow(pointToCheckY - circleCenterPointY, 2)) < (Math.Pow(circleRadius, 2));
}
Usage:
if (!PointIsWithinCircle(3, 3, 3, .5, .5)) { }

- 3,341
- 1
- 36
- 46
As stated previously, to show if the point is in the circle we can use the following
if ((x-center_x)^2 + (y - center_y)^2 < radius^2) {
in.circle <- "True"
} else {
in.circle <- "False"
}
To represent it graphically we can use:
plot(x, y, asp = 1, xlim = c(-1, 1), ylim = c(-1, 1), col = ifelse((x-center_x)^2 + (y - center_y)^2 < radius^2,'green','red'))
draw.circle(0, 0, 1, nv = 1000, border = NULL, col = NA, lty = 1, lwd = 1)

- 2,203
- 9
- 41
- 84
Moving into the world of 3D if you want to check if a 3D point is in a Unit Sphere you end up doing something similar. All that is needed to work in 2D is to use 2D vector operations.
public static bool Intersects(Vector3 point, Vector3 center, float radius)
{
Vector3 displacementToCenter = point - center;
float radiusSqr = radius * radius;
bool intersects = displacementToCenter.magnitude < radiusSqr;
return intersects;
}
iOS 15, Accepted Answer written in Swift 5.5
func isInRectangle(center: CGPoint, radius: Double, point: CGPoint) -> Bool
{
return point.x >= center.x - radius && point.x <= center.x + radius &&
point.y >= center.y - radius && point.y <= center.y + radius
}
//test if coordinate (x, y) is within a radius from coordinate (center_x, center_y)
func isPointInCircle(center: CGPoint,
radius:Double, point: CGPoint) -> Bool
{
if(isInRectangle(center: center, radius: radius, point: point))
{
var dx:Double = center.x - point.x
var dy:Double = center.y - point.y
dx *= dx
dy *= dy
let distanceSquared:Double = dx + dy
let radiusSquared:Double = radius * radius
return distanceSquared <= radiusSquared
}
return false
}

- 8,587
- 7
- 46
- 87
-
Why do you never do the sqrt (root) operation? Distance is Math.sqrt(dx * dx + dy * dy) – parsecer Oct 13 '22 at 01:01
I used the code below for beginners like me :).
public class incirkel {
public static void main(String[] args) {
int x;
int y;
int middelx;
int middely;
int straal; {
// Adjust the coordinates of x and y
x = -1;
y = -2;
// Adjust the coordinates of the circle
middelx = 9;
middely = 9;
straal = 10;
{
//When x,y is within the circle the message below will be printed
if ((((middelx - x) * (middelx - x))
+ ((middely - y) * (middely - y)))
< (straal * straal)) {
System.out.println("coordinaten x,y vallen binnen cirkel");
//When x,y is NOT within the circle the error message below will be printed
} else {
System.err.println("x,y coordinaten vallen helaas buiten de cirkel");
}
}
}
}}
Here is the simple java code for solving this problem:
and the math behind it : https://math.stackexchange.com/questions/198764/how-to-know-if-a-point-is-inside-a-circle
boolean insideCircle(int[] point, int[] center, int radius) {
return (float)Math.sqrt((int)Math.pow(point[0]-center[0],2)+(int)Math.pow(point[1]-center[1],2)) <= radius;
}

- 10,183
- 2
- 21
- 41
PHP
if ((($x - $center_x) ** 2 + ($y - $center_y) ** 2) <= $radius **2) {
return true; // Inside
} else {
return false; // Outside
}

- 3,010
- 35
- 36