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

后端 未结 8 1188
夕颜
夕颜 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:44
    dx1=x2-x1 ; dy1=y2-y1 ; dx2=x4-x3 ;dy2=y4-y3.
    
    Angle(L1,L2)=pi()/2*((1+sign(dx1))* (1-sign(dy1^2))-(1+sign(dx2))*(1-sign(dy2^2)))
               +pi()/4*((2+sign(dx1))*sign(dy1)-(2+sign(dx2))*sign(dy2))
               +sign(dx1*dy1)*atan((abs(dx1)-abs(dy1))/(abs(dx1)+abs(dy1)))
               -sign(dx2*dy2)*atan((abs(dx2)-abs(dy2))/(abs(dx2)+abs(dy2)))
    
    0 讨论(0)
  • 2020-11-28 07:45

    Check this Python code:

    import math
    def angle(x1,y1,x2,y2,x3,y3):
    
      if (x1==x2==x3 or y1==y2==y3):
        return 180
      else:
        dx1 = x2-x1
        dy1 = y2-y1
        dx2 = x3-x2
        dy2 = y3-y2
        if x1==x2:
          a1=90
        else:
          m1=dy1/dx1
          a1=math.degrees(math.atan(m1))
        if x2==x3:
          a2=90
        else:
          m2=dy2/dx2
          a2=math.degrees(math.atan(m2))
        angle = abs(a2-a1)
        return angle
    
    print angle(0,4,0,0,9,-6)
    
    0 讨论(0)
提交回复
热议问题