直角坐标与极坐标互相转换(Python&C++实现)

在这里插入图片描述
在直角坐标系中 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;
}
  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值