[英]Angle between two vectors in the interval [0,360]
我試圖找到兩個向量之間的角度。
以下是我用來評估向量ba
和bc
之間的角度的代碼
import numpy as np
import scipy.linalg as la
a = np.array([6,0])
b = np.array([0,0])
c = np.array([1,1])
ba = a - b
bc = c - b
cosine_angle = np.dot(ba, bc) / (la.norm(ba) * la.norm(bc))
angle = np.arccos(cosine_angle)
print (np.degrees(angle))
我的問題是,
在此代碼中:
對於c = np.array([1,1])
和c = np.array([1,-1])
你得到 45 度作為答案。 我可以從數學的角度理解這一點,因為從點積來看,您始終關注區間[0,180]
的角度。
但在幾何上這是誤導,因為點c
位於[1,1]
和[1,-1]
兩個不同位置。
那么有沒有一種方法可以讓我獲得區間[0,360]
中的角度作為一般起點b = np.array([x,y])
感謝你的幫助
從概念上講,使用點積獲得兩個向量之間的角度是完全沒問題的。 然而,由於兩個向量之間的角度在坐標系平移/旋轉時是不變的,我們可以找到每個向量與 x 軸正方向所對的角度,並從另一個值中減去一個值。
優點是,我們將使用np.arctan2
來查找角度,它返回 [-π,π] 范圍內的角度,因此您可以了解向量所在的象限。
# Syntax: np.arctan2(y, x) - put the y value first!
# Instead of explicitly referring by indices, you can unpack each vector in reverse, like so:
# np.arctan2(*bc[::-1])
angle = np.arctan2(bc[1], bc[0]) - np.arctan2(ba[1], ba[0])
然后您可以適當地轉換以獲得 [0, 2π] 內的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.