gpt4 book ai didi

javascript - 直 Angular 坐标和球坐标之间的转换

转载 作者:行者123 更新时间:2023-11-29 18:07:25 28 4
gpt4 key购买 nike

我需要在 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);

我用过 Spherical coordinate system Cartesian to Spherical coordinates Calculator 获取我的公式。

但是我不确定我是否正确地将它们翻译成了代码。

最佳答案

有很多错误

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

this.phi = atan2(y, x);

对于 Theta 使用反余弦函数:

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

关于javascript - 直 Angular 坐标和球坐标之间的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30271693/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com