将笛卡尔坐标系转换为多点极坐标系
Posted
技术标签:
【中文标题】将笛卡尔坐标系转换为多点极坐标系【英文标题】:Convert Cartesian coordinate system to polar coordinate system with multi-points 【发布时间】:2020-02-14 03:22:22 【问题描述】:我想将许多点从笛卡尔坐标系转换为极坐标系。
这是我的代码:
import numpy as np
x = 1
y = 1
def cart_to_pol(x, y):
rho = np.sqrt(x**2 + y**2)
phi = np.arctan2(y, x)
return(rho, phi)
print(cart_to_pol(x, y))
但我在数组中有很多点需要转换。
-
有什么方法可以不一个一个地同时对所有点进行转换吗?
例如,如果中心不在 (0,0),而是在 (50,50)。编码中如何设置圆心?
度数以上代码的结果是半径(0~2pi),但我需要角度度数(0~360)。我试试
phi = np.arctan2(y, x)*180/pi
但结果是错误的。我该如何解决?
谢谢!!!
points = [(10, 43), (10, 44), (10, 45), (10, 46), (10, 47), (10, 48), (10, 49),
(10, 50), (10, 51), (10, 52), (10, 53), (10, 54), (10, 55), (10, 56), (11, 39),
(11, 40), (11, 41), (11, 42), (11, 57), (11, 58), (11, 59), (11, 60), (12, 36),
(12, 37), (12, 38), (12, 61), (12, 62), (12, 63), (13, 33), (13, 34), (13, 35),
(13, 64), (13, 65), (13, 66), (14, 31), (14, 32), (14, 33), (14, 66), (14, 67),
(14, 68), (15, 30), (15, 31), (15, 68), (15, 69), (16, 28), (16, 29), (16, 70),
(16, 71), (17, 27), (17, 72), (18, 25), (18, 26), (18, 73), (18, 74), (19, 24)]
【问题讨论】:
你能在 for 循环或列表理解中运行你的代码吗?您是在使用np.pi
还是将pi
设置为某个值?
【参考方案1】:
只使用复杂的数学
import numpy as np
x = 1
y = 1
def cart_to_pol(x, y, x_c = 0, y_c = 0, deg = True):
complex_format = x - x_c + 1j * (y - y_c)
return np.abs(complex_format), np.angle(complex_format, deg = deg)
print(cart_to_pol(x, y))
(1.4142135623730951, 45.0)
只需将您的中心坐标传递给(x_c, y_c)
或者您可以使用二维数组:
def cart_to_pol(coords, center = [0,0], deg = True):
complex_format = np.array(coords, dtype = float).view(dtype = np.complex) -\
np.array(center, dtype = float).view(dtype = np.complex)
return np.abs(complex_format).squeeze(), np.angle(complex_format, deg = deg).squeeze()
print(cart_to_pol(points))
(array([44.14748011, 45.12205669, 46.09772229, 47.07440918, 48.05205511,
49.03060269, 50.009999 , 50.99019514, 51.97114584, 52.95280918,
53.93514624, 54.91812087, 55.90169944, 56.88585061, 40.52159918,
41.48493703, 42.44997055, 43.41658669, 58.05170109, 59.03388857,
60.01666435, 61. , 37.94733192, 38.89730068, 39.84971769,
62.16912417, 63.15061362, 64.13267498, 35.4682957 , 36.40054945,
37.33630941, 65.30696747, 66.28725368, 67.26812024, 34.0147027 ,
34.92849839, 35.84689666, 67.46851117, 68.44705983, 69.42621983,
33.54101966, 34.43835072, 69.63476143, 70.61161378, 32.24903099,
33.12099032, 71.80529228, 72.78049189, 31.90611227, 73.97972695,
30.8058436 , 31.6227766 , 75.18643495, 76.15773106, 30.61045573]), array([76.90810694, 77.19573393, 77.47119229, 77.73522627, 77.98852161,
78.23171107, 78.46537935, 78.69006753, 78.90627699, 79.11447295,
79.3150876 , 79.50852299, 79.69515353, 79.87532834, 74.24882634,
74.62374875, 74.98163937, 75.32360686, 79.07719528, 79.2611029 ,
79.43898931, 79.61114218, 71.56505118, 72.03086026, 72.47443163,
78.87081071, 79.04593736, 79.21570213, 68.49856568, 69.07549826,
69.62356479, 78.51800865, 78.69006753, 78.85711014, 65.69545073,
66.37062227, 67.0112832 , 78.02386756, 78.19756579, 78.366366 ,
63.43494882, 64.17900803, 77.56043798, 77.73522627, 60.2551187 ,
61.11341823, 77.12499844, 77.30041551, 57.80426607, 76.71513352,
54.24611275, 55.30484647, 76.14858099, 76.32869287, 51.63251462]))
【讨论】:
您好,您的回答非常有帮助,我非常感谢。请问如果要显示0~360之间的角度怎么办?例如,如果我设置 center = [50,50],对于位置 (10,43),结果显示角度为 -170.07375449。我怎样才能让这个显示为 189.9262455?np.angle(complex_format, deg = deg) % 360
?如果您觉得它有帮助,请务必点赞,如果有用,请标记已解决。【参考方案2】:
我没有回答最后两个问题,但这是您可以一举转换所有分数的方法。只是一个为 x,y 定义的 for 循环来获取元组的每个点。祝你好运!
import numpy as np
points = np.array([(10, 43), (10, 44), (10, 45), (10, 46), (10, 47), (10, 48),
(10, 49), (10, 50), (10, 51), (10, 52), (10, 53), (10, 54), (10, 55), (10,
56), (11, 39), (11, 40), (11, 41), (11, 42), (11, 57), (11, 58), (11, 59),
(11, 60), (12, 36), (12, 37), (12, 38), (12, 61), (12, 62),(12, 63), (13, 33),
(13, 34), (13, 35), (13, 64), (13, 65), (13, 66), (14, 31), (14, 32), (14,
33), (14, 66), (14, 67), (14, 68), (15, 30), (15, 31), (15, 68), (15, 69),
(16, 28), (16, 29), (16, 70), (16, 71), (17, 27), (17, 72), (18, 25), (18,
26), (18, 73), (18, 74), (19, 24)])
polar = []
index = 0
for x, y in points:
r = np.sqrt(x**2 + y**2)
phi = np.arctan2(y, x)
polar.append((r, phi))
index += 1
print(polar)
【讨论】:
【参考方案3】:numpy 可以处理矩阵或二维 numpy 数组
import numpy as np
points = np.array([(10, 43), (10, 44), (10, 45), (10, 46), (10, 47), (10, 48),
(10, 49), (10, 50), (10, 51), (10, 52), (10, 53), (10, 54), (10, 55), (10,
56), (11, 39), (11, 40), (11, 41), (11, 42), (11, 57), (11, 58), (11, 59),
(11, 60), (12, 36), (12, 37), (12, 38), (12, 61), (12, 62),(12, 63), (13, 33),
(13, 34), (13, 35), (13, 64), (13, 65), (13, 66), (14, 31), (14, 32), (14,
33), (14, 66), (14, 67), (14, 68), (15, 30), (15, 31), (15, 68), (15, 69),
(16, 28), (16, 29), (16, 70), (16, 71), (17, 27), (17, 72), (18, 25), (18,
26), (18, 73), (18, 74), (19, 24)])
#passing the points[:,0] as x, points[:,1] as y, (a,b) as center
def cart_to_pol(points, a = 0, b = 0):
rho = np.sqrt((points[:,0]-a)**2 + (points[:,1]-b)**2)
phi = np.arctan2((points[:,1]-a), (points[:,0]-b))
return rho, phi
#for center at (0,0)
cart_to_pol(points)
#for center at (1,1)
cart_to_pol(points,1,1)
希望这能解决您的第 1 和第 2 个问题。 最后一个问题..
func = lambda x : x if x>0 else (2*np.pi + phi)
phi = func(phi)
print(phi)
请告诉我这是否有帮助。
【讨论】:
您好,您的回答非常有帮助,我非常感谢。请问如果要显示0~2π之间的角度怎么办?例如,如果我设置 center = [50,50],对于位置 (10,43),结果显示角度(以半径为单位)为 -2.96834699。我怎样才能使这个显示为 3.314838317179586? 你能告诉我应该在哪里插入“func = lambda x....” 我试图将它插入到上面的编码中(你的原始答案),但我得到了错误。谢谢!!!以上是关于将笛卡尔坐标系转换为多点极坐标系的主要内容,如果未能解决你的问题,请参考以下文章
Unity3D之笛卡尔坐标系转换——屏幕坐标转换世界坐标,世界坐标转换相机坐标工具