三点定向旋转角的计算

2024-09-29 19:33:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我要做的是找出由三个连续点组成的直线之间的旋转角度。这些都是顺序点,所以旋转方向很重要。 我的输入是一对坐标的序列。在

我想要的输出是每个点的旋转角度,在这里点将充当角度的顶点。这个角度在1到360之间,负数表示向左旋转,正数表示向右旋转。在

我已经为此挣扎了几个星期,但我终于比以往任何时候都更接近于解决方案。我编写了以下脚本,并将其与程序geospatialmodelingtool(GME)的“pathmetrics”函数的输出进行了比较。在

coords=coords=[[283907,971700],[284185,971634],[284287,971507],[284275,971608],[283919,971761],[284311,971648],[284277,971637],[284280,971651],[284174,971649],[283909,971701],[283941,971700],[284294,971518],[284288,971517],[284315,971539],[284250,971505]
print "A"+"\t"+"B"+"\t"+"C"+"\t"+"orientation"+"\t"+"angle"+"\t"+"bearing AB"+"\t"+"bearing BC"
for a in coords:
  A=a
  indexA=coords.index(a)
  B=coords[indexA+1]
  C=coords[indexA+2]
  ##Find the bearings of AB and BC
  AB=[B[0]-A[0],B[1]-A[1]]          #find the extreme of vector AB
  BearAB=math.atan2(AB[0],AB[1])    #use arctan2 to find the angle
  ABBearDeg=math.degrees(BearAB)    #in degrees
  if ABBearDeg<0:                   #if negative, add 360 in order to obtain the angle in a clockwise direction
   ABBearDeg=360+ABBearDeg          #Bearing AB
  BC=[C[0]-B[0],C[1]-B[1]]          #Do the same for points BC
  BearBC=math.atan2(BC[0],BC[1])
  BCBearDeg=math.degrees(BearBC)
  if BCBearDeg<0:
   BCBearDeg=360+BCBearDeg          #Bearing BC
 ##Find the angle between the lines
  alfa=BCBearDeg-ABBearDeg          #Obtain the difference between the bearing angles
  if abs(alfa)>180:                 #If greater than 180
   if alfa<0:                        #and negative
    angle=(360+alfa)                   #Direction of rotation is right and angle is obtained by adding 360
    print format(A)+"\t"+format(B)+"\t"+format(C)+"\t"+"right"+"\t"+format(angle)+"\t"+format(round(ABBearDeg,2))+"\t"+format(round(BCBearDeg,2))
   else:                             #If positive
    angle=alfa-360                      #Direction of rotation is left and angle is obtained by substracting 360
    print format(A)+"\t"+format(B)+"\t"+format(C)+"\t"+"left"+"\t"+format(angle)+"\t"+format(ABBearDeg)+"\t"+format(round(BCBearDeg,2))
  else:                            #If the difference was less than 180, then the rotation angle is equal to it
   angle=alfa
   if angle<0:                     #If negative, left rotation
       print format(A)+"\t"+format(B)+"\t"+format(C)+"\t"+"left"+"\t"+format(angle)+"\t"+format(ABBearDeg)+"\t"+format(round(BCBearDeg,2))
   else:                            #If positive, right rotation
    print format(A)+"\t"+format(B)+"\t"+format(C)+"\t"+"right"+"\t"+format(angle)+"\t"+format(ABBearDeg)+"\t"+format(round(BCBearDeg,2))

虽然许多结果是一致的,但其他的却不一致

^{pr2}$

(抱歉,桌子很乱;我无法按我想要的方式上传图像)

我已经能够在错误发生的地方找到一个点,但我不知道为什么会发生,因为这完全取决于我无法控制的预设公式。 所以,不同之处在于(有时)我对向量方位的计算与GME计算的不同。 奇怪的是,它只是偶尔发生,我不知道是什么触发了它。在

你知道会发生什么吗?在

如果你知道任何其他计算线与运动方向夹角的方法,请告诉我。 任何有用的东西我都可以。在

谢谢!!!!在


Tags: theformatifabiscoordsprintbc
1条回答
网友
1楼 · 发布于 2024-09-29 19:33:08

有A点,B点和C点。根据你的描述,我假设B是旋转点。也就是说,向量BA转化为BC。在

  • 创建向量BA(A-B)和BC(C-B)。在
  • 计算矢量的角度(使其为正)及其差
  • 向量之间的角度差就是旋转角度。在

使用numpy使这变得容易。像这样:

In [1]: import numpy as np

In [2]: A = np.array([283907, 971700])

In [3]: B = np.array([284185, 971634])

In [4]: C = np.array([284287, 971507])

In [5]: BA = A - B

In [6]: BC = C - B

In [7]: BA
Out[7]: array([-278,   66])

In [8]: BC
Out[8]: array([ 102, -127])

In [9]: s = np.arctan2(*BA)

In [10]: if s < 0:
   ....:     s += 2*np.pi
   ....:     

In [11]: s
Out[11]: 4.9454836529138948

In [12]: e = np.arctan2(*BC)

In [13]: if e < 0:
    e += 2*np.pi
   ....:     

In [14]: e
Out[14]: 2.4649341681747883

In [15]: delta = e - s

In [16]: np.degrees(delta)
Out[16]: -142.12501634890182

In [17]: delta
Out[17]: -2.4805494847391065

正角度是逆时针方向。在

相关问题 更多 >

    热门问题