Python之间的坐标转换

cpc*_*333 41 python coordinate-systems

是否有不同坐标系之间的转换功能?

例如,Matlab具有[rho,phi] = cart2pol(x,y)从笛卡尔坐标到极坐标的转换.好像它应该是numpy或scipy.

小智 71

使用numpy,您可以定义以下内容:

import numpy as np

def cart2pol(x, y):
    rho = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    return(rho, phi)

def pol2cart(rho, phi):
    x = rho * np.cos(phi)
    y = rho * np.sin(phi)
    return(x, y)
Run Code Online (Sandbox Code Playgroud)


P i*_*P i 15

现有答案可以简化:

from numpy import exp, abs, angle

def polar2z(r,theta):
    return r * exp( 1j * theta )

def z2polar(z):
    return ( abs(z), angle(z) )
Run Code Online (Sandbox Code Playgroud)

甚至:

polar2z = lambda r,?: r * exp( 1j * ? )
z2polar = lambda z: ( abs(z), angle(z) )
Run Code Online (Sandbox Code Playgroud)

注意这些也适用于数组!

rS, thetaS = z2polar( [z1,z2,z3] )
zS = polar2z( rS, thetaS )
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个,但是因为问题从x和y(不是z)开始,我会添加简单的行`z = x + 1j*y` (2认同)
  • @johnktejik `z = Polar2z(r,theta)` 然后 `x = np.real(z)` 和 `y = np.imag(z)` 另外,`np.angle()` 有一个 `deg` 选项返回度数而不是弧度。如果你想要度数,你可以通过“z2polar()”传递它。 (2认同)

CR *_*mar 8

您可以使用cmath模块。

如果将数字转换为复杂格式,则仅对数字调用Polar方法会变得更加容易。

import cmath
input_num = complex(1, 2) # stored as 1+2j
r, phi = cmath.polar(input_num)
Run Code Online (Sandbox Code Playgroud)


wwi*_*wii 7

如果你在numpy或scipy中找不到它,这里有几个快速函数和一个点类:

import math

def rect(r, theta):
    """theta in degrees

    returns tuple; (float, float); (x,y)
    """
    x = r * math.cos(math.radians(theta))
    y = r * math.sin(math.radians(theta))
    return x,y

def polar(x, y):
    """returns r, theta(degrees)
    """
    r = (x ** 2 + y ** 2) ** .5
    theta = math.degrees(math.atan2(y,x))
    return r, theta

class Point(object):
    def __init__(self, x=None, y=None, r=None, theta=None):
        """x and y or r and theta(degrees)
        """
        if x and y:
            self.c_polar(x, y)
        elif r and theta:
            self.c_rect(r, theta)
        else:
            raise ValueError('Must specify x and y or r and theta')
    def c_polar(self, x, y, f = polar):
        self._x = x
        self._y = y
        self._r, self._theta = f(self._x, self._y)
        self._theta_radians = math.radians(self._theta)
    def c_rect(self, r, theta, f = rect):
        """theta in degrees
        """
        self._r = r
        self._theta = theta
        self._theta_radians = math.radians(theta)
        self._x, self._y = f(self._r, self._theta)
    def setx(self, x):
        self.c_polar(x, self._y)
    def getx(self):
        return self._x
    x = property(fget = getx, fset = setx)
    def sety(self, y):
        self.c_polar(self._x, y)
    def gety(self):
        return self._y
    y = property(fget = gety, fset = sety)
    def setxy(self, x, y):
        self.c_polar(x, y)
    def getxy(self):
        return self._x, self._y
    xy = property(fget = getxy, fset = setxy)
    def setr(self, r):
        self.c_rect(r, self._theta)
    def getr(self):
        return self._r
    r = property(fget = getr, fset = setr)
    def settheta(self, theta):
        """theta in degrees
        """
        self.c_rect(self._r, theta)
    def gettheta(self):
        return self._theta
    theta = property(fget = gettheta, fset = settheta)
    def set_r_theta(self, r, theta):
        """theta in degrees
        """
        self.c_rect(r, theta)
    def get_r_theta(self):
        return self._r, self._theta
    r_theta = property(fget = get_r_theta, fset = set_r_theta)
    def __str__(self):
        return '({},{})'.format(self._x, self._y)
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您的坐标存储为复数,您可以使用cmath


Kei*_*thB 5

有一种更好的方式来编写polar(),这里是:

def polar(x,y):
  `returns r, theta(degrees)`
  return math.hypot(x,y),math.degrees(math.atan2(y,x))
Run Code Online (Sandbox Code Playgroud)

  • 来自 KeithB 的评论,`return np.hypot(x, y), np.arctan2(y, x)` (3认同)
  • 好的,然后使用np.hypot。关键是您永远不要写sqrt(x ** 2 + y ** 2)。虚伪存在是有原因的。 (2认同)
  • @mLstudent33:显然“极性”将笛卡尔(x,y)转换为极坐标(rho theta),如果将(rho,theta)转换为(x,y)的函数,您需要什么,请参阅复杂方法的其他答案或仅使用简单的 cos/sin 如[这个答案](/sf/answers/1464850481/)。 (2认同)