Is angle in between two angles

前端 未结 4 1406
一整个雨季
一整个雨季 2020-12-20 07:08

I have 3 angles a b c

a=315 b=20 c=45

ok so would like to know giving all three if b is in between a and c

i have the long way of doing this adding a

4条回答
  •  [愿得一人]
    2020-12-20 07:38

    1st off, every angle is between 2 other angles, what you're really asking is:

    For given angles: a, b, and g, is g outside the reflex angle between a and b?

    You can just go ahead and define a as the leftmost angle and b as the rightmost angle or you can solve for that, for example if either of these statements are true a is your leftmost angle:

    1. a ≤ b ∧ b - a ≤ π
    2. a > b ∧ a - b ≥ π

    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

    If you're just defining a to be leftmost and b to be rightmost you're done here and your condition will look like:

    a <= g && g <= b ||
    a <= g && b < a ||
    g <= b && b < a
    

    If however you calculated the l and r you'll notice there is an optimization opportunity here in doing both processes at once. Your function will look like:

    if(a <= b) {
        if(b - a <= PI) {
            return a <= g && g <= b;
        } else {
            return b <= g || g <= a;
        }
    } else {
        if(a - b <= PI) {
            return b <= g && g <= a;
        } else {
            return a <= g || g <= b;
        }
    }
    

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

    a <= b ?
    (b - a <= PI && a <= g && g <= b) || (b - a > PI && (b <= g || g <= a)) :
    (a - b <= PI && b <= g && g <= a) || (a - b > PI && (a <= g || g <= b))
    

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

    Live Example

提交回复
热议问题