Python之间的坐标转换

时间:2014-01-04 17:15:34

标签: python coordinate-systems

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

例如,Matlab有[rho,phi] = cart2pol(x,y)用于从笛卡尔坐标到极坐标的转换。似乎它应该是numpy或scipy。

8 个答案:

答案 0 :(得分:65)

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

答案 1 :(得分:13)

现有答案可以简化:

from numpy import exp, abs, angle

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

def z2polar(z):
    return ( abs(z), angle(z) )

甚至:

polar2z = lambda r,θ: r * exp( 1j * θ )
z2polar = lambda z: ( abs(z), angle(z) )

注意这些也适用于数组!

rS, thetaS = z2polar( [z1,z2,z3] )
zS = polar2z( rS, thetaS )

答案 2 :(得分: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
    if y == 0:
        theta = 180 if x < 0 else 0
    elif x == 0:
        theta = 90 if y > 0 else 270
    else:
        theta = math.degrees(math.atan(float(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)

答案 3 :(得分:6)

您可以使用cmath模块。

如果将数字转换为复杂格式,则只需在数字上调用极坐标方法就更容易了。

toString()

答案 4 :(得分:5)

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

def polar(x,y):
  `returns r, theta(degrees)`
  return math.hypot(x,y),math.degrees(math.atan2(y,x))

答案 5 :(得分:4)

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

答案 6 :(得分:0)

以上所有适合我的答案的组合:

percents(4)
# > [10.0, 20.0, 30.0, 40.0]

percents(8)
# > [2.7777777777777777,
#    5.555555555555555,
#    8.333333333333332,
#    11.11111111111111,
#    13.88888888888889,
#    16.666666666666664,
#    19.444444444444443,
#    22.22222222222222]

sum(percents(45))
# > 100.0

答案 7 :(得分:-4)

一般来说,我会强烈考虑隐藏精心设计的抽象背后的坐标系。引用鲍勃叔叔和他的书:

class Point(object)
    def setCartesian(self, x, y)
    def setPolar(self, rho, theta)
    def getX(self)
    def getY(self)
    def getRho(self)
    def setTheta(self)

使用Point类的任何用户可以选择方便的表示,不会执行显式转换。所有这些丑陋的正弦,余弦等都将隐藏在一个地方。点类。只有你应该关心计算机内存中使用哪种表示的地方。