Calculating the angle between two lines without having to calculate the slope? (Java)

后端 未结 8 1223
夕颜
夕颜 2020-11-28 07:27

I have two Lines: L1 and L2. I want to calculate the angle between the two lines. L1 has points: {(x1, y1), (x2, y2)} and L2 has points: {(x3, y3), (x4, y

8条回答
  •  借酒劲吻你
    2020-11-28 07:30

    dx1 = x2-x1;
    dy1 = y2-y1;
    dx2 = x4-x3;
    dy2 = y4-y3;
    
    d = dx1*dx2 + dy1*dy2;   // dot product of the 2 vectors
    l2 = (dx1*dx1+dy1*dy1)*(dx2*dx2+dy2*dy2) // product of the squared lengths
    
    angle = acos(d/sqrt(l2));
    

    The dot product of 2 vectors is equal to the cosine of the angle time the length of both vectors. This computes the dot product, divides by the length of the vectors and uses the inverse cosine function to recover the angle.

提交回复
热议问题