为了账号安全,请及时绑定邮箱和手机立即绑定

将笛卡尔坐标系转换为多点极坐标系

将笛卡尔坐标系转换为多点极坐标系

慕丝7291255 2022-07-26 21:56:58
我想将许多点从笛卡尔坐标系转换为极坐标系。这是我的代码:import numpy as npx = 1y = 1def 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)]
查看完整描述

3 回答

?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

numpy 可以处理矩阵或 2D 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)

希望这能解决您的第一个和第二个问题。最后一个问题..


func = lambda x : x if x>0 else (2*np.pi + phi) 


phi = func(phi)

print(phi)

请告诉这是否有帮助。


查看完整回答
反对 回复 2022-07-26
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

只需使用复杂的数学


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) 或者您可以使用 2d 数组来执行此操作:


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


查看完整回答
反对 回复 2022-07-26
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

我没有回答最后两个问题,但这是您可以一口气转换所有点的方法。只是一个为 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)


查看完整回答
反对 回复 2022-07-26
  • 3 回答
  • 0 关注
  • 144 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信