javascript - Angle between 3 points math explain -


i have converted c++ code javascript calculates angle between 3 points. though working not understand math behind it.

    function angle(a, b, c) {          var ab = { x: b.x - a.x, y: b.y - a.y };          var cb = { x: b.x - c.x, y: b.y - c.y };            var dot = (ab.x * cb.x + ab.y * cb.y); // dot product          var cross = (ab.x * cb.y - ab.y * cb.x); // cross product            var alpha = -math.atan2(cross, dot);          if (alpha < 0) alpha += 2 * math.pi;          return alpha;      }

what use of dot , cross product here? how atan2 use cross , dot products calculate angle?

var ab = { x: b.x - a.x, y: b.y - a.y }; var cb = { x: b.x - c.x, y: b.y - c.y }; 

these points represent lines ab , bc. dot product of 2 lines is

dot = |ab|.|bc|.cos(theta) cross = |ab|.|bc|.sin(theta) 

their division get

cross/dot = tan(theta) 

so

theta = atan(cross, dot) 

we know value of dot , cross

dot = (ab.x * cb.x + ab.y * cb.y); cross = (ab.x * cb.y - ab.y * cb.x); 

hence can find angle using above information


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -