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?
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- How to get a fixed number of evenly spaced points
- How to determine +/- sign when calculating diagona
相关文章
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Is there an existing solution for these particular
- How to measure complexity of a string?
You can use Pythagoras to measure the distance between your point and the centre and see if it's lower than the 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:
Also, Jason noted that
<=
should be replaced by<
and depending on usage this may actually make senseeven though I believe that it's not true in the strict mathematical sense. I stand corrected.My answer in C# as a complete cut & paste (not optimized) solution:
Usage:
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