在直角坐标系中
P
P
P点坐标可表示为
(
X
,
Y
)
(X,Y)
(X,Y),在极坐标系中
P
P
P点坐标可表示为
(
R
,
θ
)
(R,\theta)
(R,θ)。
直角坐标转极坐标
已知
(
x
,
y
)
(x,y)
(x,y),
R
=
X
2
+
Y
2
R = \sqrt{X^2+Y^2}
R=X2+Y2
θ
=
a
r
c
t
a
n
(
Y
X
)
\theta =arctan(\frac{Y}{X})
θ=arctan(XY)
Python代码为:
import numpy as np
def Rectangular_to_Polar(x, y): # 直角坐标转极坐标,输出的thata为角度值
r = np.sqrt(np.square(x) + np.square(y))
theta = np.degrees(np.arctan(y / x))
return round(r, 2), round(theta, 2)
C++代码为:
#include<iostream>
#include<math.h>
#define PAI acos(-1)
using namespace std;
int main()
{
// double x = 1;
// double y = 1;
double x, y;
cout << "请输入x坐标值:";
cin >> x;
cout << "请输入y坐标值:";
cin >> y;
double r = pow(pow(x, 2) + pow(y, 2), 0.5);
double theta = atan(y / x) * (180 / PAI);
cout << "r:" << r << endl;
cout << "theta:" << theta << "°" << endl;
return 0;
}
极坐标转直角坐标
已知
(
R
,
θ
)
(R,\theta)
(R,θ),
X
=
R
×
c
o
s
(
θ
)
X = R×cos(\theta)
X=R×cos(θ)
Y
=
R
×
s
i
n
(
θ
)
Y =R×sin(\theta)
Y=R×sin(θ)
Python代码为:
import numpy as np
def Polar_to_Rectangular(r, theta): # 极坐标转直角坐标,输入的thata需为角度值
theta = theta * (np.pi / 180)
x = r * np.cos(theta)
y = r * np.sin(theta)
return round(x, 2), round(y, 2)
C++代码为:
#include<iostream>
#include<math.h>
#define PAI acos(-1)
using namespace std;
int main()
{
// double r = 1.41;
// double theta = 45*(PAI/180);
double x_1, y_1, theta_1;
cout << "请输入X:";
cin >> x_1;
cout << "请输入y:";
cin >> y_1;
cout << "请输入Theta(°):";
cin >> theta_1;
double r = pow(pow(x_1, 2) + pow(y_1, 2), 0.5);
double theta = theta_1 * (PAI / 180);
double x = r * cos(theta);
double y = r * sin(theta);
cout << "x:" << x << endl;
cout << "y:" << y << endl;
return 0;
}