Search code examples
javascriptgraphicsgeometrycomputational-geometrypseudocode

How to compute an angle between 2 vectors in a polygon


I implemented a method in javascript to compute the angle between 2 vectors. But I don't know how to compute the angle that lies in the polygon.

example For example, in this image I want to compute the red angle but in the left one I need to go from AC to AB while in the right one I need to do it from AB to AC.

Thank you for your response


Solution

  • From the question I understand that you need values of red (internal) angles. It is easy to get them knowing order. For CCW order find directed angle between two consecutive edge vectors. In the first case vectors are BA/AC, in the second CA/AB

    To get directed angle in full 2*Pi (360 degrees) range, you can use atan2 function

    Fi_left = atan2 (crossproduct(BA, AC), dotproduct(BA, AC))    
    Fi_right = atan2 (crossproduct(CA, AB), dotproduct(CA, AB))