19

I am trying to figure out whether a angle lies between 2 other angles. I have been trying to create a simple function to perform this but none of my techniques will work for all possible values of the angles.

Can you help me edit my function to correctly determine if a angle lies between 2 other angles?

enter image description here

In the above picture; I use the green point as the central point, then I determine the angle of each line to the green point. I then calculate the angle of the black point to the green point. I am trying to check if the angle of the black dot is BETWEEN the 2 lines' angles.

NOTE: In my case; an angle(targetAngle) is said to lie between 2 other angles IF the difference between the 2 angles is < 180 degrees AND the targetAngle lies in the cavity made by those 2 angles.

The following code should work but it fails for these(which do lie between the angle):
- is_angle_between(150, 190, 110)
- is_angle_between(3, 41, 345)

bool is_angle_between(int target, int angle1, int angle2) 
{  
  int rAngle1 = ((iTarget - iAngle1) % 360 + 360) % 360;  
  int rAngle2 = ((iAngle2 - iAngle1) % 360 + 360) % 360;  
  return (0 <= rAngle1 && rAngle1 <= rAngle2);  
}  

// Example usage  
is_angle_between(3, 41, 345);  

Another technique I attempted which also doesn't work:

int is_angle_between(int target, int angle1, int angle2)
{
  int dif1  = angle1-angle2;
  int dif2  = angle2-angle1;
  int uDif1 = convert_to_positive_angle( dif1 ); // for eg; convert -15 to 345
  int uDif2 = convert_to_positive_angle( dif2 );

  if (uDif1 <= uDif2) {
    if (dif1 < 0) {
      return (target <= angle1 && target >= angle2);
    }
    else return (in_between_numbers(iTarget, iAngle1, iAngle2));
  }
  else {
    if (dif2 < 0) {
      return (target <= angle1 && target >= angle2);
    }
    else return (in_between_numbers(iTarget, iAngle1, iAngle2));
  }

  return -1;
}
sazr
  • 24,984
  • 66
  • 194
  • 362
  • 2
    Why not treat the two angles and your central point as a triangle and check to see if the black dot falls inside of it? – Nick Savage Jul 10 '12 at 03:36
  • @NickSavage great idea :) Can you name a mathematical formula that can check if a points falls inside a triangle? Or can I do it using simple sin, cos, tan. – sazr Jul 10 '12 at 03:37
  • 1
    A couple of questions. Are all angles >=0 and <360? Also, given your test case of 3, 41 and 345, are you asking if 3 is between 41 and 345 degrees? Which should be false, right? – HeatfanJohn Jul 10 '12 at 03:38
  • @NickSavage How far are you going to allow the two lines defining the triangle? – tmpearce Jul 10 '12 at 03:39
  • Check [this](http://www.blackpawn.com/texts/pointinpoly/default.html) for point inside a triangle – higuaro Jul 10 '12 at 03:39
  • All angles are >=0 and <= 360. For your 2nd question, no it would be true, 3 degrees lies within the smallest cavity made by the angles 41 and 345 degrees – sazr Jul 10 '12 at 03:40
  • Why not to use normalized vectors and check the dot product between normalized vectors generated by the black dot? – higuaro Jul 10 '12 at 03:41
  • @tmpearce He is drawing the lines to from two points to meet a central point form what I see. I assumed those values were known and were represented by the numbers in his diagram. – Nick Savage Jul 10 '12 at 03:41
  • @NickSavage yes all points are known. And the lines angles are calculated by the angle of the last vertex(on the line) to the green point. – sazr Jul 10 '12 at 03:43
  • If this is point inside convex polygon (triangle is one), then you can use CCW test to check. – nhahtdh Jul 10 '12 at 03:51
  • you can find solution in here: https://math.stackexchange.com/questions/1044905/simple-angle-between-two-angles-of-circle/3316065#3316065 – keyvan moazami Aug 07 '19 at 10:04
  • Does this answer your question? [Calculating if an angle is between two angles](https://stackoverflow.com/questions/12234574/calculating-if-an-angle-is-between-two-angles) – iacob Mar 25 '21 at 13:07

11 Answers11

11
bool is_angle_between(int target, int angle1, int angle2) 
{
  // make the angle from angle1 to angle2 to be <= 180 degrees
  int rAngle = ((angle2 - angle1) % 360 + 360) % 360;
  if (rAngle >= 180)
    std::swap(angle1, angle2);

  // check if it passes through zero
  if (angle1 <= angle2)
    return target >= angle1 && target <= angle2;
  else
    return target >= angle1 || target <= angle2;
}  
fdermishin
  • 3,519
  • 3
  • 24
  • 45
  • Please do not post code only answers. Also provide an explanation of what the code is doing and why it works. Your code, for example, doesn't work in the case `is_angle_between(45, 90, 315)` – Jonathan Mee Feb 23 '17 at 21:07
  • 4
    I have just run the code and it returns true as expected, as 45 degrees is between 315 and 90 degrees. The algorithm is pretty simple, so I think that comments are enough to understand it. Which part do you wish to be clarified? – fdermishin Feb 24 '17 at 13:10
  • I stand corrected. I had converted this to `doubles`, and failed to convert `rAngle` to a `double`. Thanks for bothering to look at it again. – Jonathan Mee Feb 24 '17 at 13:34
  • 1
    is_angle_between(450, 45, 135) = false, is_angle_between(90, 405, -225) = false, is_angle_between(45,180,-230) =true. is_angle_between(52, -76, -390) = true. (-360 = -30, 52 is not between -76, and -30) @JonathanMee My code sure it has two additional mods but it gets the right answer. Here's a unit-test fiddle. https://jsfiddle.net/5L5xjnkf/ – Tatarize Mar 07 '17 at 21:53
  • @Tatarize The OP did specify that all input was in the range [0:360] http://stackoverflow.com/questions/11406189/determine-if-angle-lies-between-2-other-angles/11412077?noredirect=1#comment15040614_11406189 so you could probably drop 1 of your mods also. But it's a good clarifying comment as neither the answer nor the question explicitly called that out, just the comment. – Jonathan Mee Mar 08 '17 at 00:02
9

Inspired by a post about Intervals in modular arithmetic:

static bool is_angle_between(int x, int a, int b) {
    b = modN(b - a);
    x = modN(x - a);

    if (b < 180) {
        return x < b;
    } else {
        return b < x;
    }
}

where (in case of checking angles) modN() would be implemented as

// modN(x) is assumed to calculate Euclidean (=non-negative) x % N.
static int modN(int x) {
    const int N = 360;
    int m = x % N;
    if (m < 0) {
        m += N;
    } 
    return m;
}
sschuberth
  • 28,386
  • 6
  • 101
  • 146
  • 1
    I've just translated your code to Python for this question: [create a circular list by using a range of angles python](http://stackoverflow.com/questions/42133572/create-a-circular-list-by-using-a-range-of-angles-python). – PM 2Ring Feb 09 '17 at 10:59
  • 1
    Doesn't work in lots of case, particularly those where `a` is not the leftmost angle in the reflexive angle. For example: `point_in_closed_interval(90, 135, 45)` – Jonathan Mee Feb 23 '17 at 21:14
  • That's how the code is *designed* to work: For `b` > `a` the angle range you describe is "the bigger pie of the pie chart", and 90° is not in that range, it's in "the smaller part of the pie chart". In other words, `a` and `b` are oriented, and their order matters, similar to like the order of vectors matter for the cross-product. – sschuberth Feb 24 '17 at 13:43
  • The problem cannot be generalized to that. This answer is wrong in principle. Around the 0° position everything will be wrong. Getting the positions of where the angles would be in the range [0°, 360°] doesn't tell if that given position is between them (within 180°). Is the difference between the target angle and a less than the difference between b and a in the range 0-360, doesn't answer anything. It's basically meaningless. – Tatarize Mar 07 '17 at 21:42
  • @Tatarize Would you mind giving a concrete example with numbers, including the actual result from the function and your expected result? – sschuberth Mar 08 '17 at 07:35
  • @sschuberth https://jsfiddle.net/8obadd6u/ This will provide a large list of cases where it gets the wrong answer. – Tatarize Mar 08 '17 at 22:59
  • Assuming we're finding whether x is between the reflex angle between a and b, rather than between the angle within the given order. The OP seems to want the former, hence wrong in principle. – Tatarize Mar 08 '17 at 23:03
  • 1
    @Tatarize After re-reading the OP's question and the *NOTE* therein I agree that he / she indeed does not want to deal with oriented angles but always cover the area that is <= 180°. I'll adjust my answer. – sschuberth Mar 09 '17 at 10:14
  • 1
    @Tatarize My updated solution still sometimes returns different results than yours, but this time I'm somewhat confident that my solution is right :-) For examples for `x = 44.191604750688704`, `a = 92.99465847152985`, `b = 43.93893024446933` my solution returns `true` while yours returns `false`. – sschuberth Mar 09 '17 at 11:09
  • 1
    Yeah, my example there uses ^ to validate the sign change. a^b>0, it's built for integers so in ints the xor makes sure there's a sign change. But, in javascript apparently not. Switching it for a * fixed it. Though your solution has a few elegancies over mine (it did even when it didn't work). – Tatarize Mar 09 '17 at 20:50
4
void normalize( float& angle ) 
{
    while ( angle < -180 ) angle += 360;
    while ( angle >  180 ) angle -= 360;
}

bool isWithinRange( float testAngle, float a, float b )
{
    a -= testAngle;
    b -= testAngle;
    normalize( a );
    normalize( b );
    if ( a * b >= 0 )
        return false;
    return fabs( a - b ) < 180;
}
iforce2d
  • 8,194
  • 3
  • 29
  • 40
  • 1
    Been going through and unit testing these code elements. This is the first (other than mine) that actually works for all values. https://jsfiddle.net/9vnncaas/ – Tatarize Mar 07 '17 at 22:08
  • This solution is underappreciated. It solves elegantly all tests I tried. – Yoav Moran Dec 12 '21 at 07:54
2

If angle2 were always 0, and angle1 were always between 0 and 180, this would be easy:

return angle1 < 180 && 0 < target && target < angle1;

if I'm reading the requirements correctly.

But it's not that hard to get there.

int reduced1 = (angle1 - angle2 + 360) % 360; // and imagine reduced2 = 0
if (180 < reduced1) { angle2 = angle1; reduced1 = 360 - reduced1; } // swap if backwards
int reducedTarget = (target - angle2 + 360) % 360;
return reduced1 < 180 && 0 < reducedTarget && reducedTarget < reduced1;
Marc
  • 21
  • 1
  • is angle between (-307, -311, 68) = false. is angle 53, between 49 and 68. Yes, this answer says no. -- And a JSFiddle unit test. https://jsfiddle.net/4agzx30h/ – Tatarize Mar 07 '17 at 21:50
1

I've done this before by comparing angles.

enter image description here

In the sketch above vector AD will be between AB and AC if and only if

angle BAD + angle CAD == angle BAC

Because of floating point inaccuracies I compared the values after rounding them first to say 5 decimal places.

So it comes down to having an angle algorithm between two vectors p and q which is simply put like:

double a = p.DotProduct(q);
double b = p.Length() * q.Length();
return acos(a / b); // radians

I'll leave the vector DotProduct and Length calculations as a google search exercise. And you get vectors simply by subtracting the coordinates of one terminal from the other.

You should of course first check whether AB and AC are parallel or anti-parallel.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
1

Is angle T between angles A and B, there are always two answers: true and false.

We need specify what we mean, and in this case we're looking for the normalized small sweep angles and whether our angle is between those values. Given any two angles, there is a reflex angle between them, is the normalized value of T within that reflex angle?

If we rotate A and B and T such that T = 0 and normalize A and B to within +-hemicircumference (180° or 2PI). Then our answer is whether A and B have different signs, and are within a hemicircumference of each other.

If we subtract the angle from test, then add 180° (so A is relative to T+180). Then we mod by 360 giving us a range between [-360°,360°] we add 360° and mod again (note, you could also just check if it's negative and add 360 if it is), giving us a value that is certain to be [0°,360°]. We subtract 180° giving us a value between [-180°,180°] relative to T+180°-180° aka, T. So T is now angle zero and all angles fall within the normalized range. Now we check to make sure the angles have a sign change and that they are not more than 180° apart, we have our answer.

Because the question asks in C++:

bool isAngleBetweenNormalizedSmallSweepRange(int test, int a, int b) {
    int a_adjust = ((((a - test + 180)) % 360) + 360) % 360 - 180;
    int b_adjust = ((((b - test + 180)) % 360) + 360) % 360 - 180;
    return ((a_adjust ^ b_adjust) < 0) && ((a_adjust - b_adjust) < 180) && ((a_adjust - b_adjust) > -180);
}

We can also do some tricks to simplify out the code and avoid any unneeded modulo ops (see comments below). Normalize will move angle a into the range [-180°,180°] relative to angle t.

int normalized(int a, int test) {
    int n = a - test + 180;
    if ((n > 360) || (n < -360)) n %= 360;
    return (n > 0)? n - 180: n + 180;
}

bool isAngleBetweenNormalizedSmallSweepRange(int test, int a, int b) {
    int a_adjust = normalized(a,test);
    int b_adjust = normalized(b,test);
    return ((a_adjust ^ b_adjust) < 0) &&
            ((a_adjust > b_adjust)? a_adjust-b_adjust: b_adjust-a_adjust) < 180;
}

Also if we can be sure the range is [0,360], we can make do with a simpler if statement

bool isAngleBetweenNormalizedSmallSweepRange(int test, int a, int b) {
    int dA = a - test + 180;
    if (dA > 360) {
        dA -= 360;
    }
    int a_adjust = (dA > 0) ? dA - 180 : dA + 180;
    int dB = b - test + 180;
    if (dB > 360) {
        dB -= 360;
    }
    int b_adjust = (dB > 0) ? dB - 180 : dB + 180;
    return ((a_adjust ^ b_adjust) < 0)
            && ((a_adjust > b_adjust) ? a_adjust - b_adjust : b_adjust - a_adjust) < 180;
}

JS Fiddle test of the code

Tatarize
  • 10,238
  • 4
  • 58
  • 64
  • So at a minimum you have an error in your code where you're using the xor operator (`^`) I don't know what you intended but this doesn't work in most cases, for example `isAngleBetweenNormalizedSmallSweepRange(90, 45, 135)` – Jonathan Mee Feb 23 '17 at 21:52
  • It is intended to do an XOR on the the 32nd bit namely the sign bit. If you do any positive number XOR any negative number then the answer should be negative regardless what it is. And I'm testing for the difference in the signs. So if positive/positive or negative/negative it should be 0 therefore a positive number. Any negative/positive, positive/negative it should result in a negative number and thus fail the vs 0. You could just multiply them tho, should get the same sort of result. – Tatarize Feb 24 '17 at 05:17
  • ((45-90) % 360) - 180 = -45 - 180, (((135 - 90) % 360) 45 - 180, This is -225 and -135 both negative so -225 XOR -135 should be positive 102. Since 102 < 0, it's just flipped. -- Rather than doesn't work in most cases. It doesn't work in any case ever. – Tatarize Feb 24 '17 at 05:27
  • Well it almost always returns `true` now, which isn't a good thing. For example: `isAngleBetweenNormalizedSmallSweepRange(180, 45, 135)` should not be returning `true`. This answer is *massively* simpler than [my own](http://stackoverflow.com/a/42426720/2642059) but having spent some time with the problem I'd be stunned if you could solve it with something as simple as a sign xor. – Jonathan Mee Feb 24 '17 at 12:43
  • You're right that it misses the mark there. But, it isn't a hard problem. It really does only require looping the test value to zero rotating everything accordingly and checking whether the relevant values from that point have a flipped sign within 180°. If you mentally visualize the problem you can see that rotating the test to zero makes it really easy to judge the question. Towards zero is the angle between them obtuse? The error in the code there is that the mod and subtraction is supposed to get it between -180° and 180° and check the sign change but wrongly can return a negative. So +360 – Tatarize Feb 24 '17 at 20:09
  • Ok. Fixed, and tested. – Tatarize Feb 27 '17 at 20:51
  • This is no longer the extremely cheep solution that was originally presented. Instead it's probably the most expensive solution here. Particularly if it need be extended to floating point numbers. That said it does now produce the correct answer in all cases, so I'm offering a +1 for the hard work. – Jonathan Mee Mar 07 '17 at 13:26
  • Four mods which is akin to 4 divides, and some basically free operations has gotta still one of the cheaper answers. Other ones that use while loops or are functionally wrong, gotta count against them in that regard. I can't imagine an fmod takes a notable amount of time. You can just replace the number type and use fmod (or change nothing in Java) and it'll still work fine. – Tatarize Mar 07 '17 at 20:50
  • @Tartarize `fmod` is *not* akin to a divide, it's far more expensive: https://cboard.cprogramming.com/c-programming/105096-fmod-post771305.html#post771305 Simplicity of code may be king in some languages, but this is C++, the only reason anyone writes in C++ is for speed, so solutions using modulo operations can be dismissed in all but the most egregious situations. – Jonathan Mee Mar 07 '17 at 21:14
  • You can do it in a divide, floor, multiply and subtraction. If you wanted you could use a while loop. But, if you look above you can see, all the better rated answers are actually wrong. If you know anything about the values of the numbers you're putting in you can simplify it more. Just do one mod after adding a large value that is a multiple of 360. Or do a while subtracting 360 until you're in range if you think fmod will tank the speed, when it really shouldn't. – Tatarize Mar 07 '17 at 21:58
  • You could also do the math in integers, and quickly return the answer. Then if it happens to be within ±2 degree of 180, fallback to the floating point math. But, again time that sucker, it should be basically instant currently. – Tatarize Mar 07 '17 at 22:01
  • If you think the while loops will be faster (and they might if you fall into range quickly) iforce2d's answer is good and is already set to work with floats. – Tatarize Mar 07 '17 at 22:09
  • Also, here, you can just do an if-check if the value when in range of [-360°,360°] if it's negative, just add 360 to it and don't bother with the second mod, like in sschuberth's answer. – Tatarize Mar 07 '17 at 22:50
  • Added in items to avoid the fmod command as much as possible. https://jsfiddle.net/9yzv4bez/ – Tatarize Mar 07 '17 at 23:24
  • @Tartarize Per the OP the input is in the range [0 : 360] if you could leverage that to provide such a simple answer I'd be interested, though I've already given you my +1 :( – Jonathan Mee Mar 08 '17 at 00:07
  • 1
    The value (a - test + 180), ranges from [-180°,540°] and needs to be in range [-180°,180°], so really if we're assured a range of [0°,360°] for each variable, we can simply roll the range [180°,540°] into range with a single if statement. if (n > 360) n -= 360;, Given the resulting subtraction. Here's test values. Though note anything outside 0,360 range will start to fail. But, it uses zero mods. https://jsfiddle.net/zLnseLjk/ – Tatarize Mar 08 '17 at 23:15
  • https://jsfiddle.net/eugLg02b/ for the inlined 0 mod version. Though, I personally prefer the version that works for any angle possible. – Tatarize Mar 08 '17 at 23:21
  • Nice looking solution. I'm seeing: 9 arithmetic operations, 7 conditional operations, and 1 logical/multiplication. Which is strictly superior than the most up-voted solution's: 2 arithmetic, 4 condition, 2 modulo, and a swap. – Jonathan Mee Mar 09 '17 at 12:49
1

All the top answers here are wrong. As such I feel it is necessary for me to post an answer.

I'm just reposting a portion of an answer which I posted here: https://stackoverflow.com/a/42424631/2642059 That answer also deals with the case where you already know which angle is the lefthand side and righthand side of the reflexive angle. But you also need to determine which side of the angle is which.


1st to find the leftmost angle if either of these statements are true angle1 is your leftmost angle:

  1. angle1 <= angle2 && angle2 - angle1 <= PI
  2. angle1 > angle2 && angle1 - angle2 >= PI

For simplicity let's say that your leftmost angle is l and your rightmost angle is r and you're trying to find if g is between them.

The problem here is the seem. There are essentially 3 positive cases that we're looking for:

  1. l ≤ g ≤ r
  2. l ≤ g ∧ r < l
  3. g ≤ r ∧ r < l

Since you're calculating the lefthand and righthand sides of the angle, you'll notice there is an optimization opportunity here in doing both processes at once. Your function will look like:

if(angle1 <= angle2) {
    if(angle2 - angle1 <= PI) {
        return angle1 <= target && target <= angle2;
    } else {
        return angle2 <= target || target <= angle1;
    }
} else {
    if(angle1 - angle2 <= PI) {
        return angle2 <= target && target <= angle1;
    } else {
        return angle1 <= target || target <= angle2;
    }
}

Or if you need it you could expand into this nightmare condition:

angle1 <= angle2 ?
(angle2 - angle1 <= PI && angle1 <= target && target <= angle2) || (angle2 - angle1 > PI && (angle2 <= target || target <= angle1)) :
(angle1 - angle2 <= PI && angle2 <= target && target <= angle1) || (angle1 - angle2 > PI && (angle1 <= target || target <= angle2))

Note that all this math presumes that your input is in radians and in the range [0 : 2π].

Live Example

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0

I've found this quote from this thread:

if a point P is inside triangle ABC, then

Area PAB+Area PBC +Area PAC=Area ABC

notice that if P is on the edge of AB, BC, or CA, the above hold. But effectively, one of the area PAB, PBC, PAC is 0 (so just make sure you check that).

if P is outside, the above equality does NOT hold...

How to determine area? you have two options: 1) Heron's theorem, involves sqrt, slower 2) the more perferred way is the cross products (or effectively, the half of absolute value of (sum of the down products minus the sum of up products))

for example, if A=(x1,y1) B=(x2,y2), C=(x3,y3) Area= abs(x1*y2+x2*y3+x3*y1-x1*y3-x3*y2-x2*y1)/2

also you might want to be careful about floating point errors... instead of checking for strict inequality, check for abs(b-a)

Hopefully that will help

Nick Savage
  • 856
  • 1
  • 7
  • 18
0

Using a similar style of function as in your question, I have had good luck with the following methods:

    public static bool IsInsideRange(double testAngle, double startAngle, double endAngle)
    {
        var a1 = System.Math.Abs(AngleBetween(startAngle, testAngle));
        var a2 = System.Math.Abs(AngleBetween(testAngle, endAngle));
        var a3 = System.Math.Abs(AngleBetween(startAngle, endAngle));
        return a1 + a2 == a3;
    }

    public static double AngleBetween(double start, double end)
    {
        return (end - start) % 360;
    }
RexCardan
  • 398
  • 2
  • 7
0

I know this post is old, but there doesn't seem to be an accepted answer and I have found the following approach to be quite reliable. Although it might be more than what you need. It supports angle ranges larger than 180 degrees (as well as larger than 360 degrees and negative angles). It also supports decimal accuracy.

The method uses this normalize() helper function to convert angles into the right space:

float normalize( float degrees )
{
  //-- Converts the specified angle to an angle between 0 and 360 degrees
  float circleCount = (degrees / 360.0f);
  degrees -= (int)circleCount * 360;
  if( 0.0f > degrees )
  {
    degrees += 360.0f;
  }
  return degrees;
}

Here's the solution:

bool isWithinRange( float start, float end, float angle )
{
  if( fabsf( end - start ) >= 360.0f )
  {
    //-- Ranges greater or equal to 360 degrees cover everything
    return true;
  }

  //-- Put our angle between 0 and 360 degrees
  float degrees = normalize( angle );

  //-- Resolve degree value for the start angle; make sure it's
  //   smaller than our angle.
  float startDegrees = normalize( start );
  if( startDegrees > degrees )
  {
    startDegrees -= 360.0f;
  }

  //-- Resolve degree value for the end angle to be within the
  //   same 360 degree range as the start angle and make sure it
  //   comes after the start angle.
  float endDegrees = normalize( end );
  if( endDegrees < startDegrees )
  {
    endDegrees += 360.0f;
  }
  else if( (endDegrees - startDegrees) >= 360.0f )
  {
    endDegrees -= 360.0f;
  }

  //-- All that remains is to validate that our angle is between
  //   the start and the end.
  if( (degrees < startDegrees) || (degrees > endDegrees) )
  {
    return false;
  }

  return true;
}

Hope this helps someone.

RymplEffect
  • 151
  • 4
  • https://jsfiddle.net/rebbhxjg/ unit test fiddle isWithinRange(97.71171765113701, 90.92818068907809, -241.47414859955634) = true?, isWithinRange(179.5807788226134, -188.05066205255324, 14.247781098361713) = true? -- I think a lot of this is treating the angles in a non-normalized fashion. Though that doesn't explain things like isWithinRange(177, 162, 355) = true, 355 is not between those two angles. (not the order of the variables shifts her from usual). – Tatarize Mar 07 '17 at 22:28
0

If you have angles $$a$ and $b$, and wan't to see if angle x is between these angles.

You can calculate the angle between a->x and a->b. If ∠a->x is less than ∠a->b, x must be between a and b.

The distance between to angles, a and b

function distanceBetweenAngles(a, b) {
    distance = b - a;
    if (a > b) {
       distance += 2*pi;
    }
    return distance;
}

Then you can do

// Checks if angle 'x' is between angle 'a' and 'b'
function isAngleBetween(x, a, b) {
    return distanceBetweenAngles(a, b) >= distanceBetweenAngles(a, x);
}

This assumes you are using Radians, and not Degrees, as one should. It removes a lot of unnecessary code.

SomeNorwegianGuy
  • 1,494
  • 4
  • 17
  • 43