-6

Possible Duplicate:
How do you test if a point is inside a circle?

I need a way to determine if point P is inside circle C defined by a center point and a radius.

Is there an algorithm for this?

Thanks

Community
  • 1
  • 1
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 1
    This is just a simple calculation. – v01d Oct 17 '11 at 14:51
  • Think about it like this: What is the [distance](http://www.purplemath.com/modules/distform.htm) between C's center and P? Is it more than the radius? – Nick ODell Oct 17 '11 at 14:51
  • this looks like a homework question? – Petrogad Oct 17 '11 at 14:52
  • This has been answered here: http://stackoverflow.com/q/481144/546561 – Eric Oct 17 '11 at 14:52
  • 3
    @tenfour A question being a blatant duplicate does. – vzwick Oct 17 '11 at 14:55
  • @vzwick no, that warrants being closed. I was referring to the -4 votes before a duplicate had even been suggested. – tenfour Oct 17 '11 at 14:56
  • 6
    @tenfour: The question shows no research effort. For example: a simple google for `find if a point is in a circle` yields answers pretty quickly. – amit Oct 17 '11 at 14:59
  • @tenfour Also, a simple question !== a question demonstrating the lack of even the slightest amount of own thinking involved. – vzwick Oct 17 '11 at 14:59
  • 3
    @Milo you've been a member for over two years and have amassed reputation in the top 5% of SO users. The down votes (to me) show the community expects better quality questions from you. – Sam Miller Oct 17 '11 at 15:53
  • @amit To be fair, I see questions here all the time that show no research effort, have already been asked, and still get upvoted. This one, for example: http://stackoverflow.com/questions/16792841/detect-if-user-clicks-inside-a-circle – arkon Jul 17 '16 at 20:55

3 Answers3

6

Yes, and the algorithm is quite simple. Just check if the distance from the point P to the center of the circle C is less than the radius of the circle.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
2

Of course there is:

The point is inside if the distance from the center to the point is less than the circle's radius.

As a silly optimization, if you need to do this a lot and the circles are more or less constant, compare to the square of the circle's radius to shave some time from the computation (since computing the distance involves computing the square root, which is more expensive than not doing so).

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Compute the distance between P and the center of the circle. If the distance is less than the radius, your point is inside the circle. Sounds easy!

tibur
  • 11,531
  • 2
  • 37
  • 39