3 回答
TA貢獻(xiàn)1830條經(jīng)驗 獲得超9個贊
你的 x 向量錯了。它應(yīng)該是
b = [1,0]
假設(shè)第一個坐標(biāo)是 x 軸,第二個坐標(biāo)是 y 軸。如果輸入正確的 b 向量,所有計算都會按預(yù)期進(jìn)行。
TA貢獻(xiàn)1810條經(jīng)驗 獲得超5個贊
定義需要輸入的函數(shù)的一種方法是將兩者保留為單獨的參數(shù)(這也修復(fù)了一些錯誤并簡化了獲取角度值的邏輯):
def angle(x, y):
rad = np.arctan2(y, x)
degrees = np.int(rad*180/np.pi)
if degrees < 0:
degrees = 360 + degrees
return degrees
順便說一句,atan2輸入順序y, x很容易混淆。單獨指定它們的一個優(yōu)點是可以幫助避免這種情況。如果您想將輸入保留為數(shù)組,類似這樣的內(nèi)容可以幫助您驗證長度:
def angle(a):
if len(a) != 2:
raise IndexError("vector a expected to be length 2")
x = a[0]
y = a[1]
rad = np.arctan2(y, x)
degrees = np.int(rad*180/np.pi)
if degrees < 0:
degrees = 360 + degrees
return degrees
TA貢獻(xiàn)1841條經(jīng)驗 獲得超3個贊
我的壞處只是注意到它實際上是 numpy 數(shù)組,在這種情況下:if isinstance(x, np.ndarray) and x.shape[0] == 2
來自評論:x.ndim == 2聽起來更好。
添加回答
舉報
