Determine if angle lies between 2 other angles

后端 未结 12 1585
予麋鹿
予麋鹿 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:17

    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.

提交回复
热议问题