How to determine whether a point (X,Y) is contained within an arc section of a circle (i.e. a Pie slice)?
Solution 1:
Check:
- The angle from the centerX,centerY through X,Y should be between start&endangle.
- The distance from centerX,centerY to X,Y should be less then the Radius
And you'll have your answer.
Solution 2:
I know this question is old but none of the answers consider the placement of the arc on the circle.
This algorithm considers that all angles are between 0 and 360, and the arcs are drawn in positive mathematical direction (counter-clockwise)
First you can transform to polar coordinates: radius (R) and angle (A). Note: use Atan2 function if available. wiki
R = sqrt ((X - CenterX)^2 + (Y - CenterY)^2)
A = atan2 (Y - CenterY, X - CenterX)
Now if R < Radius the point is inside the circle.
To check if the angle is between StartingAngle (S) and EndingAngle (E) you need to consider two possibilities:
1) if S < E then if S < A < E the point lies inside the slice
2) if S > E then there are 2 possible scenarios
- if A > S
then the point lies inside the slice
- if A < E
then the point lies inside the slice
In all other cases the point lies outside the slice.