Determine if angle lies between 2 other angles

后端 未结 12 1596
予麋鹿
予麋鹿 2020-12-15 05:47

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

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 06:18

    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.

提交回复
热议问题