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

2024-10-01 02:39:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把很多点从笛卡尔坐标系转换成极坐标系

这是我的代码:

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

但我在数组中有很多点需要转换

  1. 有没有什么方法可以不用一个接一个地同时隐藏所有点
  2. 例如,如果中心不是在(0,0),而是在(50,50)。如何设置编码中的圆心
  3. 度上面的代码的结果是半径(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)]

Tags: to代码importnumpyreturndefasnp
3条回答

用复杂的数学

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

我没有回答最后两个问题,但这就是如何一下子转换所有分数的方法。只需要为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)

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)

请告诉我这是否有用

相关问题 更多 >