37
$\begingroup$

Having a circle with the centre $(x_c, y_c)$ with the radius $r$ how to know whether a point $(x_p, y_p)$ is inside the circle?

$\endgroup$
  • 3
    $\begingroup$ Do you know the formula for the distance between two points, given their coordinates? $\endgroup$ – Brian M. Scott Sep 18 '12 at 22:32
  • $\begingroup$ Well, it would definitely help. $\endgroup$ – Ivan Sep 18 '12 at 22:34
  • 1
    $\begingroup$ Inside the radius? You mean inside the circle? $\endgroup$ – Pedro Tamaroff Sep 18 '12 at 22:35
  • $\begingroup$ Exactly, @PeterTamaroff. $\endgroup$ – Ivan Sep 18 '12 at 22:38
65
$\begingroup$

The distance between $\langle x_c,y_c\rangle$ and $\langle x_p,y_p\rangle$ is given by the Pythagorean theorem as $$d=\sqrt{(x_p-x_c)^2+(y_p-y_c)^2}\;.$$ The point $\langle x_p,y_p\rangle$ is inside the circle if $d<r$, on the circle if $d=r$, and outside the circle if $d>r$. You can save yourself a little work by comparing $d^2$ with $r^2$ instead: the point is inside the circle if $d^2<r^2$, on the circle if $d^2=r^2$, and outside the circle if $d^2>r^2$. Thus, you want to compare the number $(x_p-x_c)^2+(y_p-y_c)^2$ with $r^2$.

$\endgroup$
20
$\begingroup$

The point is inside the circle if the distance from it to the center is at most $r$. Symbolically, this is $$\sqrt{|x_p-x_c|^2+|y_p-y_c|^2}< r.$$

$\endgroup$
  • $\begingroup$ I would not say that it’s inside the circle when the distance is $r$. $\endgroup$ – Brian M. Scott Sep 18 '12 at 22:51
  • 2
    $\begingroup$ It depends on whether you include the boundary. If you don't, replace $\leq$ with $<$. $\endgroup$ – Alex Becker Sep 18 '12 at 22:54
  • 3
    $\begingroup$ I’d say that you’re confusing the (closed) disk with the circle. A point at distance $r$ for the centre is inside the disk but not inside the circle; rather, it’s on the circle. (It’s a fairly minor point, but there are enough occasions for terminological confusion that I like to avoid it when it’s easy to do so.) $\endgroup$ – Brian M. Scott Sep 18 '12 at 22:57
  • $\begingroup$ @BrianM.Scott That's a fair point. $\endgroup$ – Alex Becker Sep 18 '12 at 22:59
5
$\begingroup$

Alex and Brian have answered your question already. In case you're trying to implement this algorithm in some programming language, here's my Haskell implementation:

distance :: Floating a => (a, a) -> (a, a) -> a
distance (x1,y1) (x2,y2) = sqrt((x1-x2)**2 + (y1-y2)**2)


isInsideCircle :: (Ord a, Floating a) => a -> (a, a) -> (a, a) -> Bool
isInsideCircle r (xc,yc) (x,y) | (distance (xc,yc) (x,y) < r) = True
                               | (distance (xc,yc) (x,y) >= r) = False

Suppose you have a circle whose radius is $r = 1$ and whose center is the origin $(0,0)$. You would like to know if $(\frac{1}{2},0)$, $(1,0)$, and $(1,1)$ are inside the circle. The following interactive GHCi session answers the question:

*Main> isInsideCircle 1 (0,0) (0.5,0)
True
*Main> isInsideCircle 1 (0,0) (1,0)
False
*Main> isInsideCircle 1 (0,0) (1,1)
False 
$\endgroup$
  • $\begingroup$ Here is also implementation in Kotlin: fun isInsideCircle(x: Float, y: Float, circleX: Float, circleY: Float, circleRadius: Float): Boolean { val absX = Math.pow(Math.abs(x - circleX).toDouble(), 2.0) val absY = Math.pow(Math.abs(y - circleY).toDouble(), 2.0) return Math.sqrt(absX + absY) < circleRadius } $\endgroup$ – Yuriy Seredyuk Oct 26 '17 at 15:30
  • $\begingroup$ @YuriySeredyuk That Kotlin code is unnecessary inefficient. Using Math.pow is a very inefficient way to multiply two numbers, especially because it takes a double as exponent. Math.abs is not necessary, because squaring a negative numbers will also result in a positive value. The square root is also not necessary when you multiply circleRadius with itself (Squaring both sides of the comparison). The only operations you need are addition, subtraction, and multiplying. $\endgroup$ – Peter Bruins Sep 19 '18 at 14:58
1
$\begingroup$

If you have the equation of the circle, simply plug in the x and y from your point (x,y). After working out the problem, check to see whether your added values are greater than, less than, or equal to the r^2 value. If it is greater, then the point lies outside of the circle. If it is less than, the point is inside the circle. If it is equal, the point is on the circle.

$\endgroup$

protected by Community Sep 8 '16 at 15:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.