22

Imagine a circle. Imagine a pie. Imagine trying to return a bool that determines whether the provided parameters of X, Y are contained within one of those pie pieces.

What I know about the arc:

I have the CenterX, CenterY, Radius, StartingAngle, EndingAngle, StartingPoint (point on circumference), EndingPoint (point on circumference).

Given a coordinate of X,Y, I'd like to determine if this coordinate is contained anywhere within the pie slide.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
AlishahNovin
  • 1,904
  • 3
  • 20
  • 37
  • 1
    Why are C# and Java tagged? This looks just like a plain Math question. – jzd Jun 07 '11 at 19:51
  • 2
    Whoops. I'd originally had the question as language-specific, but then changed it around to be more generic. Forgot to edit the tags. Apologies. – AlishahNovin Jun 07 '11 at 20:01

4 Answers4

37

Check:

  1. The angle from the centerX,centerY through X,Y should be between start&endangle.
  2. The distance from centerX,centerY to X,Y should be less then the Radius

And you'll have your answer.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • 1
    @Wrikken, how to determine that one angle is between two other angles, using equations? – Demian Oct 25 '16 at 19:47
  • @Demian: This is a bit old, but you can find an answer here: https://math.stackexchange.com/questions/1596513/find-the-bearing-angle-between-two-points-in-a-2d-space – rjcarr Dec 06 '17 at 06:37
18

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

image 1

2) if S > E then there are 2 possible scenarios

image 2

  • if A > S

then the point lies inside the slice

image 3

  • if A < E

then the point lies inside the slice

image 4

In all other cases the point lies outside the slice.

user7048690
  • 196
  • 1
  • 6
  • I was thinking about this exact question, and you are absolutely right! – AlishahNovin Nov 17 '19 at 05:27
  • 1
    @chux-ReinstateMonica You are correct. I didn't see that before – user7048690 Dec 28 '19 at 10:04
  • Why couldn't you just do : angle from start < angle between start and end = is in between ? I seen no reason for all these if else edge cases. – WDUK Feb 02 '20 at 06:43
  • @WDUK, Say you have a slice starting at 350 degrees and ending at 30 degrees. Lets say your point lies at 15 degrees. It is inside the slice but 350<15<30 is not true. – user7048690 Feb 10 '20 at 09:27
  • The problem i have with this is say you want to define an arc, you can't really define some of those images with those degrees. For example the very last image you cannot define an arc as being "between 300 and 35 degrees" since that wouldn't make sense unless it was a clockwise arc. – WDUK Feb 10 '20 at 20:39
  • There is an identity between angles 0 = 360, 35 = 395, 300 = 660... You use screen coordinates (X,Y) to draw the arc in code, not angles directly. When writing code, it's easier to keep the angles in the range 0 - 360, to avoid having to think about when to reduce or when to keep the angle as is - it becomes unnecessarily complicated. – user7048690 Feb 12 '20 at 08:06
  • But say your start angle is at 270 and your end angle is at 45, thats kinda confusing (assuming counter clockwise) in terms of how you travel between the two you couldn't just lerp between them like angle = (start,end,t) with t = [0,1] i feel like it is better to always make sure end angle is greater than the start angle any where on the circle. @user7048690 – WDUK Jun 19 '21 at 03:33
17

Convert X,Y to polar coordinates using this:

Angle = arctan(y/x); Radius = sqrt(x * x + y * y);

Then Angle must be between StartingAngle and EndingAngle, and Radius between 0 and your Radius.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
0

You have to convert atan2() to into 0-360 before making comparisons with starting and ending angles.

(A > 0 ? A : (2PI + A)) * 360 / (2PI)

tada007
  • 31
  • 4