Find the angle between two given lines 2D

前端 未结 2 737
傲寒
傲寒 2020-12-22 11:51

I am getting as an input 4 points.

p1=(x1, y1), p2=(x2, y2), p3=(x3, y3), p4=(x4, y4).

Now I have calculated the distances between p1p2, p2p3, p3p4 and p4p1.

2条回答
  •  自闭症患者
    2020-12-22 12:38

    To get the angle between two vectors, you simply use their inner product:

    v1 = p1-p2;
    v2 = p3-p2;
    

    Normalize to unit vectors and take inner product:

    n1 = v1/norm(v1);
    n2 = v2/norm(v2);
    
    cos_p2 = dot(n1,n2);
    

    And the resulting angle is

    acos(cos_p2)
    

提交回复
热议问题