[英]how to convert a list from polar cordinates to cartesian coordinates in python?
我有兩個清單。 一個包含到中心的距離,另一個包含角度。 現在如何將這兩個列表轉換為笛卡爾並將它們放在一個數組中?
例如:
distance = [10,12,10]
angle= [0,45,90]
我如何將它們轉換為笛卡爾坐標 {x,y} 坐標。 它們應該在列表中作為 [(x,y)]
要轉換為笛卡爾坐標,您需要以下方程:
x = r*cos(phi)
y = r*sin(phi)
有了這個,我們可以使用 python 列表推導來計算新列表:
import math
cart = [(r*math.cos(phi*math.pi/180), r*math.sin(phi*math.pi/180)) for r, phi in zip(distance, angle)]
您需要使用因子pi/180
將角度轉換為弧度。
zip
函數本質上結合了兩個列表。 有關真正的解釋,請參閱文檔
您可以使用 numpy 來計算sine
和cosine
函數。
import numpy as np
import math
distance = [10,12,10]
angle= [0,45,90]
coord = []
for i,j in zip(distance,angle):
coord.append((i*np.cos(math.pi*j/180), i*np.sin(math.pi*j/180)))
print(coord)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.