2013-03-15 139 views
0

我想要做的是找到由三個連續點形成的線條之間的旋轉角度。這些是順序點,所以旋轉的方向很重要。 我的輸入是一對座標序列。從3個點計算定向旋轉角度

我想要的輸出是每個點的旋轉角度,其中點將作爲角度的頂點。該角度將介於1和360之間,其中負數表示向左旋轉,正數表示向右旋轉。

我一直在努力掙扎數週,但我終於比以前更接近解決方案。我編寫了以下腳本,並將其與程序地理空間建​​模工具(GME)的「路徑量度」功能的輸出進行了比較。

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)) 

雖然許多結果一致,但其他人卻沒有。

      My results                  GME results  
     A     B     C  orientation  angle  bearing AB bearing BC GMEangle GMEbearing AB GMEbearing BC 
[283907, 971700] [284185, 971634] [284287, 971507] right  37.8750  103.3553 141.2300 37.8750  103.3553  141.2303 
[284185, 971634] [284287, 971507] [284275, 971608] left  -148.0060 141.2303 353.2200 -148.0060 141.2303  353.2243 
[284287, 971507] [284275, 971608] [283919, 971761] left  -59.9675 353.2243 293.2600 -68.9673 353.2243  284.2570 
[284275, 971608] [283919, 971761] [284311, 971648] right  172.8236 293.2600 106.0800 96.6181  284.2570  106.0804 
[283919, 971761] [284311, 971648] [284277, 971637] right  145.9916 106.0804 252.0700 145.9916 106.0804  252.0721 
[284311, 971648] [284277, 971637] [284280, 971651] right  120.0227 252.0700 12.0900  120.0227 252.0721  12.0948 
[284277, 971637] [284280, 971651] [284174, 971649] left  -103.1757 12.0948  268.9200 -103.1757 12.0948   268.9191 
[284280, 971651] [284174, 971649] [283909, 971701] right  12.1828  268.9191 281.1000 -131.4097 268.9191  137.5094 
[284174, 971649] [283909, 971701] [283941, 971700] right  170.6880 281.1000 91.7900  85.2053  137.5094  9.4623 
[283909, 971701] [283941, 971700] [284294, 971518] right  25.4848  91.7899  117.2700 146.4722 9.4623   85.0429 
[283941, 971700] [284294, 971518] [284288, 971517] right  143.2629 117.2748 260.5400 123.0283 85.0429   260.5377 
[284294, 971518] [284288, 971517] [284315, 971539] right  150.2887 260.5400 50.8300  150.2887 260.5377  50.8263 
[284288, 971517] [284315, 971539] [284250, 971505] left  -168.4394 50.8263  242.3900 -147.5925 50.8263   263.2338 

(對不起,凌亂的桌子,我不能上傳圖片,因爲我想)

我已經能夠在與點發生錯誤針點,但我不能弄清楚它爲什麼會發生,因爲它嚴格取決於我無法控制的預先設定的公式。 所以,區別在於(有時候)我對矢量方位的計算與GME計算的不同。 奇怪的部分是,它有時只發生,我不知道是什麼觸發它。

任何想法可能會發生什麼?

如果您知道計算包含運動方向的線之間的角度的其他方式,請告訴我。 任何有用的東西都可以和我在一起。

謝謝!

+0

這可能有助於http://stackoverflow.com/questions/13226038/calculating-angle-between-two-lines-in-python – 2013-03-15 21:04:12

回答

1

你有點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 

正角是逆時針。