Python: Checking If Coordinates Are Within Circle [duplicate]
-
30-06-2021 - |
Question
Possible Duplicate:
How can I make Turtle recognize a circle?
I have a function that draws a few circles and I am going to place dots inside them. Depending if the dot falls within the circle, depends on wether the colour will change or not.
The biggest issue I am finding is how to check whether or not the dot is within the circle. Does anyone have any idea how to calculate coordinates within a circle or is their a method which can check coordinates against the circle?
La solution
Compute (Euclidean) distance to the circle's center,
sqrt((x - center_x) ** 2 + (y - center_y) ** 2)
Check whether the result is less than the radius.
Autres conseils
For a circle with center (x,y)
and radius r
, a point (x1, y1)
is within the circle if
(x1-x)² + (y1-y)² <= r²
To check if a coordinate lies on a circle you can use the equation of the circle
a² + b² = r²
So to check if the point is inside the circle:
a² + b² <= r²