笛卡尔坐标与球坐标的转换

Converting Between Cartesian Coordinates and Spherical Coordinates

本文关键字:坐标 转换 笛卡尔      更新时间:2023-09-26

我需要在JavaScript中转换笛卡尔坐标和球坐标。我在论坛上浏览了一下,并没有找到我要找的东西。

现在我有这个:

this.rho = sqrt((x*x) + (y*y) + (z*z));
this.phi = tan(-1 * (y/x));
this.theta = tan(-1 * ((sqrt((x * x) + (y * y)) / z)));
this.x = this.rho * sin(this.phi) * cos(this.theta);
this.y = this.rho * sin(this.phi) * sin(this.theta);
this.z = this.rho * cos(this.phi);

我已经使用了球坐标系笛卡尔到球坐标计算器来得到我的公式。

然而,我不确定我是否正确地将它们翻译成代码

有很多错误

要在整个范围内得到正确的Phi值,必须使用ArcTan2函数:

this.phi = atan2(y, x);

用反余弦函数

this.theta = arccos(z / this.rho);

反向变换-你交换了Phi和Theta:

this.x = this.rho * sin(this.theta) * cos(this.phi);
this.y = this.rho * sin(this.theta) * sin(this.phi);
this.z = this.rho * cos(this.theta);