Find the angle between two given lines 2D

前端 未结 2 735
傲寒
傲寒 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:35

    Mathematically, the angle is acos(dot(||p2p1||, ||p2p3||)). Try

    u21 = (p1 - p2) / norm(p1 - p2)
    u23 = (p3 - p2) / norm(p3 - p2)
    angle = acos(sum(u21 .* u23))
    

    The prefix u indicates unit vectors.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题