【问题标题】:Converting Between Cartesian Coordinates and Spherical Coordinates笛卡尔坐标和球坐标之间的转换
【发布时间】:2015-05-16 03:59:35
【问题描述】:

我需要在 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 systemCartesian to Spherical coordinates Calculator 来获取我的公式。

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

【问题讨论】:

  • 那么你有没有试过看看你是否得到了正确的结果?
  • 代码原样无效,您需要 Math.* 在 sqrt、tan 和 sin 前面。运行代码看结果是否正确?
  • @K3N 我正在使用可汗学院版的 javascript。我的程序工作没有错误,但数学是错误的。在这里查看我的完整程序:khanacademy.org/computer-programming/…
  • @PoplopZord 啊,好的。我正在添加 khan-acedemy 的标签以使其更清晰。
  • @K3N 是的。我想我应该早点指定论坛的新手。

标签: javascript math


【解决方案1】:

有很多错误

要在整个范围内获得正确的 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);`

【讨论】:

  • 非常感谢。我不得不使用 acos 而不是 arccos,但我认为这是一回事。伙计,我现在感到很幸福。谢谢!
  • 为什么你的代码中没有Math. 对象?为简洁起见被剥离?
  • @Tomáš Zato 因为我不懂 JavaScript,也不知道它的标准库中有什么函数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
相关资源
最近更新 更多