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?
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.
Calculate the Distance
that's in C#...convert for use in python...
I used the code below for beginners like me :).
public class incirkel {
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:
Then, simply compare the result of that formula, the distance (
d
), with theradius
. 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 ifd
andr
are equal).Here is a pseudo-code example which can easily be converted to any programming language:
Where
circle_x
andcircle_y
is the center coordinates of the circle,r
is the radius of the circle, andx
andy
is the coordinates of the point.You should check whether the distance from the center of the circle to the point is smaller than the radius, i.e.
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.