【问题标题】:Python 3.6: Converting polar coordinates to Cartesian coordinatesPython 3.6:将极坐标转换为笛卡尔坐标
【发布时间】:2020-05-06 21:38:48
【问题描述】:

对于极坐标 (13, 22.6°),我应该得到 (12,5) 的笛卡尔坐标答案,但我没有。我的代码有什么问题?我知道角度 phi 应该以度数表示,我试图在代码中实现它,但它没有给我正确的答案。

import math

def pol2cart(rho, phi):
    x = rho * math.cos(math.degrees(phi))
    y = rho * math.sin(math.degrees(phi))
    return(x, y)

rho=float(input("Enter the value of rho:"))
phi=float(input("Enter the value of phi in degrees:"))
print("For the polar coordinates x = ", rho, "and y = ", phi, "the cartesian coordinates are = ", pol2cart(rho, phi))

提前致谢。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:
    import math
    
    def pol2cart(rho, phi):
        x = rho * math.cos(math.radians(phi))
        y = rho * math.sin(math.radians(phi))
        return(x, y)
    
    rho=float(input("Enter the value of rho:"))
    phi=float(input("Enter the value of phi in degrees:"))
    
    print("For the polar coordinates x = ", rho, "and y = ", phi, "the cartesian coordinates are = ", pol2cart(rho, phi))
    

    使用 math.radians 将角度从度数转换为弧度。由于您要求输入度数,但 math.cos 采用弧度角。

    【讨论】:

    • 好吧,这是有道理的。谢谢!
    【解决方案2】:
    import math
    import cmath
    
    def pol2cart(rho, phi):
        x = math.radians(phi)
        z = cmath.rect(rho,x)
        return(z)
    
    rho=float(input("Enter the value of rho:"))
    phi=float(input("Enter the value of phi in degrees:"))
    print("For the polar coordinates x = ", rho, "and y = ", phi, "the cartesian coordinates are = ", pol2cart(rho,phi))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多