Python 3.6:将极坐标转换为笛卡尔坐标

时间:2018-09-25 04:18:41

标签: python python-3.x

对于极坐标(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))

谢谢。

2 个答案:

答案 0 :(得分:0)

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的弧度角是弧度。

答案 1 :(得分:0)

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